package com.sifang.modules.changes.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.changes.entity.CrmCustomerEntryChangeDesignComponent;
import com.sifang.modules.changes.vo.DesignChangeAuditVo;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class CustomerEntryChangeDesignComponentDao extends BaseDao<CrmCustomerEntryChangeDesignComponent> {

    public int audit(DesignChangeAuditVo req) {
        SqlBuilder sb = new SqlBuilder()
                .line("update crm_customer_entry_change_design")
                .line("set audit_by=:auditBy,auditor=:auditor,audit_time=:auditTime,audit_remark=:remark,status=:status")
                .line("where id=:id and status='0'");

        SQLQuery query = getSession().createSQLQuery(sb.build());
        query.setString("auditBy", req.getUserId());
        query.setString("auditor", req.getUserName());
        query.setParameter("auditTime", new Date());
        query.setString("remark", req.getMsg());
        query.setString("status", req.getValue());
        query.setString("id", req.getChangeId());
        return query.executeUpdate();
    }

    public List<Record> findByDesignId(String designId) {
        SqlBuilder sb = new SqlBuilder()
                .line("select c.*,concat(ifnull(e.material_model,''),'|',e.model) as material_model from crm_customer_entry_change_design_component c left join crm_electric_set e on c.item_id = e.id ")
                .line("where c.design_change_id=? order by c.item_type desc ", designId);
        return Db.find(sb.build(), sb.params());
    }

    public List<Record> findByDesignId1(String designId) {
        SqlBuilder sb = new SqlBuilder()
                .line("select c.*,e.material_model from crm_customer_entry_change_design_component c left join crm_electric_set e on c.item_id = e.id ")
                .line("where c.design_change_id=? and flag = '1' ", designId);
        return Db.find(sb.build(), sb.params());
    }
}
