package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISpeParamNewService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesEquipmentParam;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesSpeParam;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesSpeParamResult;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesEquipmentParamRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesSpeParamRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesSpeParamResultRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.util.DateUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/6/16 14:15
 **/
@Service
public class SpeParamNewService implements ISpeParamNewService {

    @Autowired
    private MesEquipmentParamRepository equipmentParamRepository;
    @Autowired
    private MesSpeParamRepository speParamRepository;
    @Autowired
    private MesSpeParamResultRepository resultRepository;


    /**
     * 查询设备
     *
     * @param workCenterCode 工作中心代码
     * @param orgCode        组织代码
     * @return {@link List<MesEquipmentParam>}
     */
    @Override
    public List<MesEquipmentParam> queryEquipment(String workCenterCode, String orgCode) {
        DdlPackBean paramCondition = DdlPackBean.getDdlPackBean(orgCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", paramCondition);
        List<MesEquipmentParam> list = equipmentParamRepository.findByHqlWhere(paramCondition);
        Map<String, List<MesEquipmentParam>> map = list.stream().collect(Collectors.groupingBy(MesEquipmentParam::getEquNo));
        List<MesEquipmentParam> mesEquipmentParams = new ArrayList<>();
        map.forEach((k, v) -> {
            mesEquipmentParams.add(v.get(0));
        });
        return mesEquipmentParams;
    }

    @Override
    public void insertSpeParams(String workCenterCode, String equNo, String equName, String orgCode) {

        DdlPackBean speCondition = DdlPackBean.getDdlPackBean(orgCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", speCondition);
        DdlPreparedPack.getStringEqualPack(equNo, "equNo", speCondition);
        DdlPreparedPack.getNumEqualPack(MesPcnExtEnumUtil.CONFIRM_TYPE.NO.getValue(), "paramStatus", speCondition);
        List<MesSpeParam> speParams = speParamRepository.findByHqlWhere(speCondition);

        DdlPackBean paramCondition = DdlPackBean.getDdlPackBean(orgCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", paramCondition);
        DdlPreparedPack.getStringEqualPack(equNo, "equNo", paramCondition);
        List<MesEquipmentParam> params = equipmentParamRepository.findByHqlWhere(paramCondition);

        if (CollectionUtils.isEmpty(speParams)) {
            if (!CollectionUtils.isEmpty(params)) {
                //生成明细记录
                for (MesEquipmentParam param : params) {
                    MesSpeParam speParam = new MesSpeParam();
                    speParam.setWorkCenterCode(workCenterCode);
                    speParam.setWorkCellCode(param.getWorkCellCode());
                    speParam.setWorkCellName(param.getWorkCellName());
                    speParam.setEquNo(param.getEquNo());
                    speParam.setParamNo(param.getParamNo());
                    speParam.setParamDesc(param.getParamDesc());
                    speParam.setParamStatus(MesPcnExtEnumUtil.CONFIRM_TYPE.NO.getValue());
                    speParam.setBeforeParam(param.getParamValue());
                    speParam.setEquName(param.getEquName());
                    speParam.setParamValue(param.getParamValue());
                    speParamRepository.insert(speParam);
                }
            }
        } else {
            Map<String, MesSpeParam> maps = speParams.stream().collect(Collectors.toMap(MesSpeParam::getParamNo, Function.identity(), (key1, key2) -> key2));
            for (MesSpeParam speParam : speParams) {
                speParam.setParamStatus(MesPcnExtEnumUtil.CONFIRM_TYPE.YES.getValue());
                speParamRepository.update(speParam);
            }
            for (MesEquipmentParam param : params) {
                MesSpeParam speParam = new MesSpeParam();
                speParam.setWorkCenterCode(workCenterCode);
                speParam.setWorkCellCode(param.getWorkCellCode());
                speParam.setWorkCellName(param.getWorkCellName());
                speParam.setEquNo(param.getEquNo());
                speParam.setParamNo(param.getParamNo());
                speParam.setParamDesc(param.getParamDesc());
                speParam.setParamStatus(MesPcnExtEnumUtil.CONFIRM_TYPE.NO.getValue());
                MesSpeParam mapSpeParam = maps.get(param.getParamNo());
                if (mapSpeParam != null) {
                    speParam.setBeforeParam(mapSpeParam.getAfterParam());
                }
                speParamRepository.insert(speParam);
            }
        }


    }

    @Override
    public void updateSpeParams(List<MesSpeParam> speParams) {
        MesSpeParam mesSpeParam = speParams.get(0);
        Long rId = mesSpeParam.getSpeParamResultId();
        MesSpeParamResult mesSpeParamResult = new MesSpeParamResult();
        BeanUtils.copyProperties(mesSpeParam, mesSpeParamResult, "id");
        if (rId == null) {
            resultRepository.save(mesSpeParamResult);
        } else {
            resultRepository.insert(mesSpeParamResult);

        }
        speParams.forEach(s -> {
            s.setSpeParamResultId(mesSpeParamResult.getId());
            ConvertBean.serviceModelUpdate(s, AuthUtil.getSessionUser().getUserName());
        });
        speParamRepository.saveAll(speParams);

    }

    @Override
    public MesSpeParamResult getParamResultById(Long rid) {
        return resultRepository.getById(rid);
    }


}
