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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IMesInsertBatchDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.recyclablepackage.MesRecyclablePackageBindingLog;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * @Description : 批量新增数据拼接
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/10/18 17:38
 * @Modify:
 **/
@Service
@Slf4j
public class MesInsertBatchDaoImpl implements IMesInsertBatchDao {

    @Value("${mes.sharding.append.org:}")
    public String mesShardingAppendOrg;

    @Autowired
    private SnowflakeIdMaker snowflakeIdMaker;

    @Autowired
    private EntityManager entityManager;

    @Override
    public int insertMesRecyclablePackageBindingLog(List<MesRecyclablePackageBindingLog> logList, String organizeCode) {
        //批量导入
        int toNum;
        for (int i = 0, len = (logList.size() / MesPcnExtConstWords.FIVE_HUNDRED + 1); i < len; i++) {
            toNum = (i + 1) * MesPcnExtConstWords.FIVE_HUNDRED;
            if ((i + 1) * MesPcnExtConstWords.FIVE_HUNDRED > logList.size()) {
                toNum = logList.size();
            }
            insertMesRecyclablePackageBindingLogLimit(logList.subList(i * MesPcnExtConstWords.FIVE_HUNDRED, toNum), organizeCode);
        }
        return logList.size();
    }


    public void insertMesRecyclablePackageBindingLogLimit(List<MesRecyclablePackageBindingLog> logList, String organizeCode) {
        String fieldStr = "id,create_date_time,create_user,description,is_deleted,is_valid,organize_code,remark,system_sync_date_time,system_sync_status,hanger_code,hanger_package_sn,hanger_rule_code,lot_number,modify_type,order_code,package_sn,part_name,part_no,product_sn,qty,status,total_qty,type_code";
        //拼接字段
        StringBuilder fieldBuilder = new StringBuilder();
        String[] splitField = fieldStr.split(MesPcnExtConstWords.COMMA);
        for (String field : splitField) {
            fieldBuilder.append(MesPcnExtConstWords.COLON).append(field).append(MesPcnExtConstWords.E_UNDERLINE).append("%s , ");
        }
        //新增语句
        StringBuilder insertStr = new StringBuilder(" insert into mes_recyclable_package_binding_log");
        if (!StringUtils.isEmpty(mesShardingAppendOrg) && mesShardingAppendOrg.toUpperCase().equals(CommonEnumUtil.TRUE_OR_FALSE.TRUE.name())) insertStr.append(MesPcnExtConstWords.E_UNDERLINE).append(organizeCode.toLowerCase());
        insertStr.append("(").append(fieldStr).append(") VALUES( ");
        //sql组装
        for (int i = MesPcnExtConstWords.ZERO; i < logList.size(); i++) {
            Map<String, Object> parameterMap = new LinkedHashMap<>();
            int j = MesPcnExtConstWords.ZERO;
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), snowflakeIdMaker.nextId());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getCreateDatetime());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getCreateUser());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getDescription());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getIsDeleted());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getIsValid());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getOrganizeCode());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getRemark());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getSystemSyncDatetime());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getSystemSyncStatus());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getHangerCode());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getHangerPackageSn());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getHangerRuleCode());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getLotNumber());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getModifyType());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getOrderCode());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getPackageSn());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getPartName());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getPartNo());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getProductSn());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getQty());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getStatus());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j++]).add(String.valueOf(i)).toString(), logList.get(i).getTotalQty());
            parameterMap.put(new StringJoiner(MesPcnExtConstWords.E_UNDERLINE).add(splitField[j]).add(String.valueOf(i)).toString(), logList.get(i).getTypeCode());
            Query queryObject = entityManager.createNativeQuery(insertStr + fieldBuilder.substring(MesPcnExtConstWords.ZERO, fieldBuilder.lastIndexOf(MesPcnExtConstWords.COMMA) - MesPcnExtConstWords.ONE).replaceAll("%s", String.valueOf(i)) + " ); ");
            //数据填充
            for (String key : parameterMap.keySet()) {
                queryObject.setParameter(key, parameterMap.get(key));
            }
            queryObject.executeUpdate();
        }
    }
}
