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

import cn.estsh.i3plus.ext.mes.api.base.IMesPartCheckDetailService;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IPartCheckDetailDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartCheckDetail;
import cn.estsh.i3plus.ext.mes.pojo.model.PartCheckDetailModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPartCheckDetailRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
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.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/18 11:41 上午
 * @Description:
 **/
@Service
public class MesPartCheckDetailService implements IMesPartCheckDetailService {
    @Autowired
    private MesPartCheckDetailRepository mesPartCheckDetailRDao;
    @Autowired
    private IPartCheckDetailDao partCheckDetailDao;

    @Override
    public ListPager<PartCheckDetailModel> queryMesPartCheckDetail(MesPartCheckDetail detail, Pager pager) {
        pager = PagerHelper.getPager(pager, partCheckDetailDao.queryPartCheckDetailByPagerCount(detail));

        List<PartCheckDetailModel> partCheckDetailList = partCheckDetailDao.queryPartCheckDetailByPager(detail, pager);

        if (!CollectionUtils.isEmpty(partCheckDetailList)) {
            for (PartCheckDetailModel partCheckDetail : partCheckDetailList) {
                if (null == partCheckDetail) {
                    continue;
                }
                if (!StringUtils.isEmpty(partCheckDetail.getShiftGroupCheck())) {
                    partCheckDetail.setShiftGroupCheckName(MesExtEnumUtil.SHIFT_GROUP_CHECK.valueOfDescription(partCheckDetail.getShiftGroupCheck()));
                }
                if (!StringUtils.isEmpty(partCheckDetail.getQualityCheck())) {
                    partCheckDetail.setQualityCheckName(MesExtEnumUtil.QUALITY_CHECK.valueOfDescription(partCheckDetail.getQualityCheck()));
                }
            }
        }
        return new ListPager(partCheckDetailList, pager);
    }

    @Override
    public void updatePartCheckById(List<PartCheckDetailModel> partCheckDetailList, String userName) {
        for (PartCheckDetailModel mesPartCheckDetail : partCheckDetailList) {
            MesPartCheckDetail partCheckDetail = mesPartCheckDetailRDao.getById(mesPartCheckDetail.getId());
            if (partCheckDetail == null) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("当前物料点检明细数据不存在")
                        .build();
            }

            if (mesPartCheckDetail.getShiftGroupCheck() == MesExtEnumUtil.SHIFT_GROUP_CHECK.YES.getValue()) {
                partCheckDetail.setShiftGroupCheckUser(mesPartCheckDetail.getShiftGroupCheckUser());
                partCheckDetail.setShiftGroupCheckTime(mesPartCheckDetail.getShiftGroupCheckTime());
                partCheckDetail.setShiftGroupCheck(mesPartCheckDetail.getShiftGroupCheck());
                ConvertBean.serviceModelUpdate(partCheckDetail, partCheckDetail.getShiftGroupCheckUser());
            } else {
                partCheckDetail.setQualityCheck(mesPartCheckDetail.getQualityCheck());
                partCheckDetail.setQualityCheckTime(mesPartCheckDetail.getQualityCheckTime());
                partCheckDetail.setQualityCheckUser(mesPartCheckDetail.getQualityCheckUser());
                ConvertBean.serviceModelUpdate(partCheckDetail, partCheckDetail.getQualityCheckUser());
            }

            mesPartCheckDetailRDao.save(partCheckDetail);
        }
    }

    @Override
    public int updateShiftGroupCheck(String[] ids, Integer shiftGroupCheck) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), "id", wheres);

        return mesPartCheckDetailRDao.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesExtConstWords.SHIFT_GROUP_CHECK, MesExtConstWords.SHIFT_GROUP_CHECK_USER, MesExtConstWords.SHIFT_GROUP_CHECK_TIME, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{shiftGroupCheck, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true), AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)}
        );
    }

    @Override
    public int updateQualityCheck(String[] ids, Integer qualityCheck) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), "id", wheres);

        return mesPartCheckDetailRDao.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesExtConstWords.QUALITY_CHECK, MesExtConstWords.QUALITY_CHECK_USER, MesExtConstWords.QUALITY_CHECK_TIME, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{qualityCheck, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true), AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)}
        );
    }

    @Override
    public int orderStatusCheckUpdate(String orderNo, Integer orderStatus, String itemPartNo) {
        List<MesPartCheckDetail> partCheckDetailList = mesPartCheckDetailRDao.findByProperty(new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.ORDER_NO, MesExtConstWords.ITEM_PART_NO},
                new Object[]{AuthUtil.getOrganize().getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), MesExtEnumUtil.IS_VAILD.VAILD.getValue(), orderNo, itemPartNo});
        if (CollectionUtils.isEmpty(partCheckDetailList)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("无法获取此条码信息")
                    .build();
        }
        partCheckDetailList.forEach(pcd -> {
            if (pcd.getOrderStatus() == MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECKING.getValue()
                    || pcd.getOrderStatus() == MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue()
            ) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("该条码已扫描，不能重复扫描")
                        .build();
            }
            mesPartCheckDetailRDao.updateByProperties(new String[]{MesExtConstWords.ORDER_NO, MesExtConstWords.ITEM_PART_NO}, new Object[]{orderNo, itemPartNo},
                    new String[]{MesExtConstWords.ORDER_STATUS, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                    new Object[]{orderStatus, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)});
        });
        return 0;
    }
}
