package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.bu;

import cn.estsh.i3plus.ext.mes.api.base.bu.IMesTravelMenuFieldConfigService;
import cn.estsh.i3plus.ext.mes.api.busi.bu.IBuRecordChangeDataService;
import cn.estsh.i3plus.ext.mes.apiservice.util.BusiExtUtil;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesTravelMenuFieldConfig;
import cn.estsh.i3plus.ext.mes.pojo.bean.SxMesDataTravel;
import cn.estsh.i3plus.ext.mes.pojo.bean.SxMesDataTravelWithProcessBom;
import cn.estsh.i3plus.ext.mes.pojo.repository.SxMesDataTravelRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.SxMesDataTravelWithProcessBomRepository;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-11-01 15:42
 */
@Service
public class BuRecordChangeDataService implements IBuRecordChangeDataService {

    @Autowired
    private IMesTravelMenuFieldConfigService travelMenuFieldConfigService;

    @Autowired
    private SxMesDataTravelRepository dataTravelRepository;

    @Autowired
    private SxMesDataTravelWithProcessBomRepository dataTravelWithProcessBomRepository;

    @Override
    public <T> void doRecordChangeData(Integer menuType , T sourceData, T newData , Class clazz, String partNo, String partName, String organizeCode, String modifyDateTime) {
        List<MesTravelMenuFieldConfig> travelMenuFieldConfigList = travelMenuFieldConfigService.getTravelMenuFieldConfigList(organizeCode, menuType);
        if(!CollectionUtils.isEmpty(travelMenuFieldConfigList)){
            List<String> fields = travelMenuFieldConfigList.stream().map(MesTravelMenuFieldConfig::getFieldCode).distinct().collect(Collectors.toList());
            Map<String, List<String>> fieldCompareResultMap = BusiExtUtil.checkParamValue(sourceData, newData, clazz, fields);
            for (String field : fieldCompareResultMap.keySet()){
                insertDataTravelData(menuType, organizeCode, modifyDateTime, partNo, partName, field, fieldCompareResultMap);
            }
        }
    }

    @Override
    public <T> void doRecordChangeData(Integer menuType , T sourceData, T newData , Class clazz, String partNo, String partName, String organizeCode, List<String> fields, String modifyDateTime) {
        Map<String, List<String>> fieldCompareResultMap = BusiExtUtil.checkParamValue(sourceData, newData, clazz, fields);
        for (String field : fieldCompareResultMap.keySet()){
            insertDataTravelData(menuType, organizeCode, modifyDateTime, partNo, partName, field, fieldCompareResultMap);
        }

    }

    private void insertDataTravelData(Integer menuType, String organizeCode, String modifyDateTime, String partNo, String partName, String field, Map<String, List<String>> fieldCompareResultMap) {
        List<String> compareResultList = fieldCompareResultMap.get(field);
        SxMesDataTravel dataTravel = new SxMesDataTravel();
        dataTravel.setMenuCode(menuType);
        dataTravel.setPartNo(partNo);
        dataTravel.setPartNameRdd(partName);
        dataTravel.setFieldCode(field);
        dataTravel.setBeforeChange(compareResultList.get(0));
        dataTravel.setAfterChange(compareResultList.get(1));
        dataTravel.setCreateDatetime(modifyDateTime);
        dataTravel.setCreateUser(AuthUtil.getSessionUser().getUserName());
        dataTravel.setModifyDatetime(modifyDateTime);
        dataTravel.setModifyUser(AuthUtil.getSessionUser().getUserName());
        dataTravel.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
        dataTravel.setIsDeleted(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        dataTravel.setOrganizeCode(organizeCode);
        dataTravelRepository.insert(dataTravel);
    }

    @Override
    public <T> void doRecordChangeData(Integer menuType , T sourceData, T newData , Class clazz, String partNo, String partName, String itemPartNo, String itemPartName, String processCode, String processName, String organizeCode, String modifyDateTime) {
        List<MesTravelMenuFieldConfig> travelMenuFieldConfigList = travelMenuFieldConfigService.getTravelMenuFieldConfigList(organizeCode, menuType);
        if(!CollectionUtils.isEmpty(travelMenuFieldConfigList)){
            List<String> fields = travelMenuFieldConfigList.stream().map(MesTravelMenuFieldConfig::getFieldCode).distinct().collect(Collectors.toList());
            Map<String, List<String>> fieldCompareResultMap = BusiExtUtil.checkParamValue(sourceData, newData, clazz, fields);
            for (String field : fieldCompareResultMap.keySet()){
                List<String> compareResultList = fieldCompareResultMap.get(field);
                SxMesDataTravel dataTravel = new SxMesDataTravel();
                dataTravel.setMenuCode(menuType);
                dataTravel.setPartNo(partNo);
                dataTravel.setPartNameRdd(partName);
                dataTravel.setFieldCode(field);
                dataTravel.setBeforeChange(compareResultList.get(0));
                dataTravel.setAfterChange(compareResultList.get(1));
                dataTravel.setCreateDatetime(modifyDateTime);
                dataTravel.setCreateUser(AuthUtil.getSessionUser().getUserName());
                dataTravel.setModifyDatetime(modifyDateTime);
                dataTravel.setModifyUser(AuthUtil.getSessionUser().getUserName());
                dataTravel.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
                dataTravel.setIsDeleted(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
                dataTravel.setOrganizeCode(organizeCode);
                SxMesDataTravel mesDataTravelDb = dataTravelRepository.insert(dataTravel);
                SxMesDataTravelWithProcessBom dataTravelWithProcessBom = new SxMesDataTravelWithProcessBom();
                ConvertBean.serviceModelInitialize(dataTravelWithProcessBom, mesDataTravelDb.getModifyUser());
                dataTravelWithProcessBom.setOrganizeCode(organizeCode);
                dataTravelWithProcessBom.setPid(mesDataTravelDb.getId());
                dataTravelWithProcessBom.setItemPartNo(itemPartNo);
                dataTravelWithProcessBom.setItemPartNameRdd(itemPartName);
                dataTravelWithProcessBom.setProcessCode(processCode);
                dataTravelWithProcessBom.setProcessName(processName);
                dataTravelWithProcessBomRepository.insert(dataTravelWithProcessBom);
            }
        }
    }
}
