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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISpeParamService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesSpeParam;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesSpeParamRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkCenterExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkOrderExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.sqlpack.MesHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/2 15:19
 **/
@Service
public class SpeParamService implements ISpeParamService {

    @Autowired
    private MesSpeParamRepository speParamRepository;

    @Autowired
    private MesWorkCenterExtRepository workCenterRepository;

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;



    /**
     * 新增
     *
     * @param speParam
     * @return
     */
    @Override
    public MesSpeParam insert(MesSpeParam speParam) {
        ConvertBean.modelInitialize(speParam, AuthUtil.getSessionUser());
        speParam.setParamStatus(MesPcnExtEnumUtil.CONFIRM_TYPE.NO.getValue());
        return speParamRepository.insert(speParam);
    }

    /**
     * 修改
     *
     * @param speParam
     * @return
     */
    @Override
    public void update(MesSpeParam speParam) {
        //1、判断单据是否存在
        Long id = speParam.getId();
        MesSpeParam speParam2 = speParamRepository.getById(id);
        if (ObjectUtils.isEmpty(speParam2)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "数据不存在");
        }
        BeanUtils.copyProperties(speParam, speParam2);
        ConvertBean.serviceModelUpdate(speParam2, AuthUtil.getSessionUser().getUserName());
        speParamRepository.update(speParam2);

    }

    /**
     * 查询
     *
     * @param speParam
     * @return
     */
    @Override
    public List<MesSpeParam> queryList(MesSpeParam speParam) {

        String workCenterCode = speParam.getWorkCenterCode();
        String organizeCode = speParam.getOrganizeCode();
        String equNo = speParam.getEquNo();
        DdlPackBean packBean = MesHqlPack.getMesAllData(organizeCode);
        DdlPreparedPack.getNumEqualPack(speParam.getIsValid(), "isValid", packBean);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", packBean);
        DdlPreparedPack.getStringEqualPack(equNo, "equNo", packBean);
        DdlPreparedPack.getNumEqualPack(MesPcnExtEnumUtil.CONFIRM_TYPE.NO.getValue(), "paramStatus", packBean);
        return speParamRepository.findByHqlWhere(packBean);
    }


    private MesWorkCenterExt getWorkCenter(String workCenterCode, String organizeCode) {
        return workCenterRepository.getByProperty(
                new String[]{"workCenterCode", "organizeCode", "isValid", "isDeleted"},
                new Object[]{workCenterCode, organizeCode,
                        CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        CommonEnumUtil.IS_DEAL.NO.getValue()});
    }


    @Override
    public List<String> queryCell(String workCenterCode, String organizeCode) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(organizeCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", packBean);
        List<MesSpeParam> details = speParamRepository.findByHqlWhere(packBean);
        return details.stream().map(MesSpeParam::getWorkCellCode).distinct().collect(Collectors.toList());
    }


    @Override
    public List<MesWorkOrderExt> queryWorkOrder(String workCenterCode, String organizeCode) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue(), "workOrderStatus", ddlPackBean);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", ddlPackBean);
        ddlPackBean.setOrderByStr(" order by seq desc ");
        return workOrderExtRepository.findByHqlWhere(ddlPackBean);

    }


    /**
     * 异常
     *
     * @param softTypeCode          softTypeCode
     * @param errorDetailString     错误消息字符串
     * @param imppExceptionEnumCode 异常代码
     * @param args                  参数
     */
    private void throwImppException(String softTypeCode, String errorDetailString, String imppExceptionEnumCode, Object... args) {
        throw ImppExceptionBuilder.newInstance()
                .setSystemID(softTypeCode)
                .setErrorCode(imppExceptionEnumCode)
                .setErrorDetail(errorDetailString, args)
                .build();
    }
}
