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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IExteriorScratchProofCheckService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IExteriorScratchProofDetailDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesExteriorScratchProofDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOrderJob;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesExteriorScratchProofDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesExteriorScratchProofDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOrderJobRepository;
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.convert.ConvertBean;
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.mes.bean.MesWorkCell;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkCellRepository;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
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.List;
import java.util.stream.Collectors;

/**
 * @author xiangming.liao
 * @version 1.0
 * @date 2021/03/15
 **/
@Slf4j
@Service
public class ExteriorScratchProofCheckService implements IExteriorScratchProofCheckService {

    @Autowired
    private IExteriorScratchProofDetailDao exteriorScratchProofDetailDao;
    @Autowired
    private MesExteriorScratchProofDetailRepository exteriorScratchProofDetailRDao;
    @Autowired
    private MesOrderJobRepository mesOrderJobRDao;
    @Autowired
    private MesWorkCellRepository mesWorkCellRDao;

    @Override
    public List<MesExteriorScratchProofDetailModel> queryExteriorScratchProofDetailByCell(String orderNo, String organizeCode, String workCellCode) {
        return exteriorScratchProofDetailDao.queryExteriorScratchProofDetail(orderNo, organizeCode, workCellCode);
    }

    @Override
    public List<MesWorkCell> queryCellByOrderNo(String orderNo, String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(orderNo, MesPcnExtConstWords.ORDER_NO, packBean);
        List<MesExteriorScratchProofDetail> proofDetailList = exteriorScratchProofDetailRDao.findByHqlWhere(packBean);
        List<String> centerList = proofDetailList.stream().map(MesExteriorScratchProofDetail::getWorkCenterCode).collect(Collectors.toList());
        List<String> cellList = proofDetailList.stream().map(MesExteriorScratchProofDetail::getWorkCellCode).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(cellList)) {
            return null;
        }
        DdlPackBean cellPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(cellList, MesPcnExtConstWords.WORK_CELL_CODE, cellPackBean);
        DdlPreparedPack.getInPackList(centerList, MesPcnExtConstWords.WORK_CENTER_CODE, cellPackBean);
        return mesWorkCellRDao.findByHqlWhere(cellPackBean);
    }

    @Override
    public void updateExteriorScratchProof(MesExteriorScratchProofDetail exteriorScratchProofDetail) {
        MesExteriorScratchProofDetail detailById = exteriorScratchProofDetailRDao.getById(exteriorScratchProofDetail.getId());
        if (null == detailById) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("该条数据不存在")
                    .build();
        }

        //单据主表状态改为点检中
        MesOrderJob mesOrderJob = mesOrderJobRDao.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ORDER_NO},
                new Object[]{exteriorScratchProofDetail.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),
                        MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue(), exteriorScratchProofDetail.getOrderNo()});
        if (mesOrderJob.getOrderStatus() == MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CREATE.getValue()) {
            mesOrderJob.setOrderStatus(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECKING.getValue());
            mesOrderJob.setComplateTime(TimeTool.getNowTime(true));
            ConvertBean.serviceModelUpdate(mesOrderJob, exteriorScratchProofDetail.getUserName());
            mesOrderJobRDao.save(mesOrderJob);
        }

        BeanUtils.copyProperties(exteriorScratchProofDetail, detailById);
        ConvertBean.serviceModelUpdate(detailById, exteriorScratchProofDetail.getUserName());
        detailById.setCheckTime(detailById.getModifyDatetime());
        detailById.setCheckUser(detailById.getModifyUser());
        exteriorScratchProofDetailRDao.save(detailById);
    }
}
