package com.sifang.modules.q7link.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.q7link.dao.CrmSupplyMaterialProjectPriceDao;
import com.sifang.modules.q7link.entity.CrmSupplyMaterialProjectPrice;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CrmSupplyMaterialProjectPriceService extends BaseService {

    @Resource
    private CrmSupplyMaterialProjectPriceDao dao;

    public Long selectOne(CrmSupplyMaterialProjectPrice obj){
        String sql="select count(1) from crm_supply_material_project_price where status='0' and qq_project_code='"+obj.getQqProjectCode()+"' and qq_material_code='"+obj.getQqMaterialCode()+"' limit 1";

        return Db.queryLong(sql);
    }

    public String save(CrmSupplyMaterialProjectPrice obj){
        dao.save(obj);
        return obj.getId();
    }

    public int delete(CrmSupplyMaterialProjectPrice obj){
        String sql="update crm_supply_material_project_price set status='1' where qq_project_code='"+obj.getQqProjectCode()+"' and qq_material_code='"+obj.getQqMaterialCode()+"'";
        return Db.update(sql)>0?1:0;
    }

    public Record findPriceByCodes(String qqProjectCode, String qqMaterialCode){
        SqlBuilder builder = new SqlBuilder();
        builder.line(" select * from  crm_supply_material_project_price a ")
                .line(" where a.status = '0'  and  a.qq_project_code = ?",qqProjectCode)
                .line(" and a.qq_material_code = ? ",qqMaterialCode);
        return Db.findFirst(builder.build(), builder.params());
    }


    public List<Record> findPriceByQqMaterialCode(String qqMaterialCode){
        SqlBuilder builder = new SqlBuilder();
        builder.line(" select * from  crm_supply_material_project_price a ")
                .line(" where a.status = '0'  and a.qq_material_code = ? ",qqMaterialCode);
        return Db.find(builder.build(), builder.params());
    }
}
