package com.sifang.modules.supply.order.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialApplyGather;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author hongzq
 */
@Repository
public class MaterialApplyGatherDao extends BaseDao<CrmSupplyMaterialApplyGather> {

    public void deleteByApplyId(String applyId) {
        update("delete from CrmSupplyMaterialApplyGather where applyId=:p1", new Parameter(applyId));
    }

    public List<Record> listGatherByApplyId(String id) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select d.id, d.apply_id, d.type, d.target_id, d.amount")
                .line(", ifnull(t.name, b.materiel_type) as materialTypeName")
                .line(", ifnull(m.remarks, b.remarks) as remarks")
                .line(", ifnull(m.code, b.number) as code")
                .line("from crm_supply_material_apply_gather d")
                .line("left join crm_supply_material_bom b on d.target_id=b.id")
                .line("left join crm_supply_material m on d.target_id=m.id")
                .line("left join crm_supply_material_type t on m.material_type_id=t.id")

                .line("where d.apply_id=?", id)
                .line("order by d.sort");
        return Db.find(builder.build(), builder.params());
    }
}
