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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IEpMesOrderJobService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ep.IEpOrderJobDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesEpEquipmentCheckDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOrderJob;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpOrderJobModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
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.base.tool.HqlPack;
import cn.estsh.i3plus.pojo.mes.bean.MesEquipment;
import cn.estsh.i3plus.pojo.mes.repository.MesEquipmentRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkOrderRepository;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-14 10:32
 */
@Service
@Transactional(readOnly = false)
public class EpMesOrderJobService implements IEpMesOrderJobService {
    @Autowired
    private MesOrderJobRepository mesOrderJobRDao;
    @Autowired
    private IEpOrderJobDao orderJobDao;
    @Autowired
    private MesWorkOrderRepository mesWorkOrderRDao;
    @Autowired
    private MesEpEquipmentCheckDetailRepository equipmentCheckDetailRepository;
    @Autowired
    private MesEquipmentRepository equipmentRepository;

    @Override
    public List<EpOrderJobModel> queryMesOrderJob(EpOrderJobModel orderJob) {
        //获取产线的当前班次
        //默认查询当前班次的单据作业
        /*MesWorkCenterExt workCenter = getWorkCenter(orderJob.getWorkCenterCode(), orderJob.getOrganizeCode());
        orderJob.setShiftCode(workCenter.getShiftCode());*/
        List<EpOrderJobModel> orderJobModels = this.orderJobDao.queryMesOrderJobNoMemo(orderJob);
        Iterator<EpOrderJobModel> orderJobModelIterator = orderJobModels.iterator();
        List<EpOrderJobModel> result = new ArrayList<>();
        while (orderJobModelIterator.hasNext()){
            EpOrderJobModel orderJobModel = orderJobModelIterator.next();
            String orderNo = orderJobModel.getOrderNo();
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(orderNo,"orderJobNo",packBean);
            MesEpEquipmentCheckDetail equipmentCheckDetail = equipmentCheckDetailRepository.getByProperty(packBean);

            if(equipmentCheckDetail == null){
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "单据[%s]-设备点检详细单未生成",orderNo);
            }

            packBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(equipmentCheckDetail.getEquNo(),"equipmentCode",packBean);
            MesEquipment equipment = equipmentRepository.getByProperty(packBean);
            if(equipment != null){
                orderJobModel.setEquipmentCode(equipment.getEquipmentCode());
                orderJobModel.setEquipmentName(equipment.getEquipmentName());
                orderJobModel.setMemo(equipment.getMemo());
//                throwImppException(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "设备[%s]不存在",equipmentCheckDetail.getEquNo());
            }



            result.add(orderJobModel);
        }

        return result;
    }

    @Override
    public int orderJobUpdateOrderStatus(long id, Integer orderStatus, String organizeCode, String modifyUser) {
        List<MesOrderJob> jobList = mesOrderJobRDao.findByProperty(new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue(), id});
        if (CollectionUtils.isEmpty(jobList)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("该单据作业不存在")
                    .build();
        }
        return mesOrderJobRDao.updateByProperties(new String[]{MesPcnExtConstWords.ID}, new Object[]{id},
                new String[]{MesPcnExtConstWords.ORDER_STATUS, MesPcnExtConstWords.MODIFY_USER, MesPcnExtConstWords.MODIFY_DATE_TIME},
                new Object[]{orderStatus, modifyUser, TimeTool.getNowTime(true)});
    }

    @Override
    public int updateOrderStatusByIds(String[] idsStr, Integer orderStatus, String organizeCode, String modifyUser) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", idsStr), MesPcnExtConstWords.ID, wheres);
        HqlPack.getStringEqualPack(organizeCode, MesPcnExtConstWords.ORGANIZE_CODE, wheres);
        int num = mesWorkOrderRDao.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesPcnExtConstWords.ORDER_STATUS, MesPcnExtConstWords.MODIFY_USER, MesPcnExtConstWords.MODIFY_DATE_TIME},
                new Object[]{orderStatus, modifyUser, TimeTool.getNowTime(true)}
        );
        return num;
    }

    /**
     * 异常
     * @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();
    }
}
