package com.sifang.modules.changes.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.changes.entity.CrmMaterialInformationChangeApplyDetail;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CrmMaterialInformationChangeApplyDetailDao extends BaseDao<CrmMaterialInformationChangeApplyDetail> {

    /**
     * 创建申请单详情
     */
    public CrmMaterialInformationChangeApplyDetail createApplyDetail(String applyId,CrmMaterialInformationChangeApplyDetail applyDetail,String type) {
        CrmMaterialInformationChangeApplyDetail detail = new CrmMaterialInformationChangeApplyDetail();
        detail.setApplyId(applyId);
        detail.setType(type);
        detail.setMaterialId(applyDetail.getMaterialId());
        detail.setMaterialCode(applyDetail.getMaterialCode());
        detail.setMaterialName(applyDetail.getMaterialName());
        detail.setMaterialSpec(applyDetail.getMaterialSpec());
        detail.setMaterialBrand(applyDetail.getMaterialBrand());
        detail.setBarcode(applyDetail.getBarcode());
        detail.setNewBarcode(applyDetail.getNewBarcode());
        detail.setSn(applyDetail.getSn());
        detail.setNewSn(applyDetail.getNewSn());
        detail.setNbqSn(applyDetail.getNbqSn());
        detail.setNewNbqSn(applyDetail.getNewNbqSn());
        detail.setYzCode(applyDetail.getYzCode());
        detail.setNewYzCode(applyDetail.getNewYzCode());
        save(detail);
        return detail;
    }
    public CrmMaterialInformationChangeApplyDetail createApplyDetail(String applyId,CrmMaterialInformationChangeApplyDetail applyDetail,String type,String projectId) {
        CrmMaterialInformationChangeApplyDetail detail = new CrmMaterialInformationChangeApplyDetail();
        detail.setApplyId(applyId);
        detail.setProjectId(projectId);
        detail.setType(type);
        detail.setMaterialId(applyDetail.getMaterialId());
        detail.setMaterialCode(applyDetail.getMaterialCode());
        detail.setMaterialName(applyDetail.getMaterialName());
        detail.setMaterialSpec(applyDetail.getMaterialSpec());
        detail.setMaterialBrand(applyDetail.getMaterialBrand());
        detail.setBarcode(applyDetail.getBarcode());
        detail.setNewBarcode(applyDetail.getNewBarcode());
        detail.setSn(applyDetail.getSn());
        detail.setNewSn(applyDetail.getNewSn());
        detail.setNbqSn(applyDetail.getNbqSn());
        detail.setNewNbqSn(applyDetail.getNewNbqSn());
        detail.setYzCode(applyDetail.getYzCode());
        detail.setNewYzCode(applyDetail.getNewYzCode());
        save(detail);
        return detail;
    }

    /**
     * 根据申请单id查找物料详情
     */
    public List<Record> findMaterialDetailsByApplyId(String applyId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" select * from  crm_material_information_change_apply_detail  a ")
                .line(" where a.apply_id = ?  order by a.project_id  asc  ",applyId);
        return Db.find(builder.build(),builder.params());
    }
}
