package cn.estsh.i3plus.ext.mes.apiservice.daoimpl;

import cn.estsh.i3plus.ext.mes.apiservice.dao.IMesInsertBatchDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessBom;
import cn.estsh.i3plus.platform.common.util.WmsConstWords;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * @Description : 批量写入数据
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2023/8/11 11:46
 * @Modify:
 **/
@Service
public class MesInsertBatchDaoImpl implements IMesInsertBatchDao {

    @Autowired
    private SnowflakeIdMaker snowflakeIdMaker;

    @Autowired
    private EntityManager entityManager;

    @Override
    public int insertMesProcessBomBatch(List<MesProcessBom> processBomList) {
        StringBuffer insertString = new StringBuffer(" INSERT INTO mes_process_bom (id, create_date_time, " +
                "create_user, is_deleted, is_valid, modify_date_time, modify_user, organize_code, " +
                "item_part_name, item_part_no, match_rule, part_name_rdd, part_no, process_code, process_name, " +
                "qty, is_check, is_repeat, is_bind_key, seq) VALUES ");
        for (MesProcessBom processBom : processBomList) {
            insertString.append("('").append(snowflakeIdMaker.nextId()).append("',")
                    .append("'").append(processBom.getCreateDatetime()).append("',")
                    .append("'").append(processBom.getCreateUser()).append("',")
                    .append("'").append(processBom.getIsDeleted()).append("',")
                    .append("'").append(processBom.getIsValid()).append("',")
                    .append("'").append(processBom.getModifyDatetime()).append("',")
                    .append("'").append(processBom.getModifyUser()).append("',")
                    .append("'").append(processBom.getOrganizeCode()).append("',")
                    .append("'").append(processBom.getItemPartName()).append("',")
                    .append("'").append(processBom.getItemPartNo()).append("',")
                    .append("'").append(processBom.getMatchRule()).append("',")
                    .append("'").append(processBom.getPartNameRdd()).append("',")
                    .append("'").append(processBom.getPartNo()).append("',")
                    .append("'").append(processBom.getProcessCode()).append("',")
                    .append("'").append(processBom.getProcessName()).append("',")
                    .append("'").append(processBom.getQty()).append("',")
                    .append("'").append(processBom.getIsCheck()).append("',")
                    .append("'").append(processBom.getIsRepeat()).append("',")
                    .append("'").append(processBom.getIsBindKey()).append("',")
                    .append("'").append(processBom.getSeq()).append("'),");
        }
        Query queryObject = entityManager.createNativeQuery(insertString.substring(WmsConstWords.ZERO, insertString.length() - 1).replaceAll("'null'", "null"));
        return queryObject.executeUpdate();
    }
}
