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

import cn.estsh.i3plus.ext.mes.api.base.IPersonCheckDetailService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPersonCheckDetail;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPersonCheckDetailRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
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.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
import cn.estsh.i3plus.pojo.mes.sqlpack.MesHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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.ArrayList;
import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/04 19:27
 * @Description:
 **/
@Service
public class PersonCheckDetailService implements IPersonCheckDetailService {
    @Autowired
    private MesPersonCheckDetailRepository personCheckDetailRDao;
    @Override
    public ListPager<MesPersonCheckDetail> queryPersonCheckDetailByPager(MesPersonCheckDetail personCheckDetail, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganize().getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(personCheckDetail.getWorkOrderNo(), MesExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(personCheckDetail.getProcessCode(), MesExtConstWords.PROCESS_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(personCheckDetail.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(personCheckDetail.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(personCheckDetail.getShiftCode(), MesExtConstWords.SHIFT_CODE, packBean);
        if (!StringUtils.isEmpty(personCheckDetail.getCheckStartTime()) && !StringUtils.isEmpty(personCheckDetail.getCheckEndTime())) {
            DdlPreparedPack.timeBuilder(personCheckDetail.getCheckStartTime(), personCheckDetail.getCheckEndTime(), MesExtConstWords.CHECK_TIME, packBean, true);
        }
        List<Integer> orderStatusList=new ArrayList<>();
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CLOSE.getValue());
        DdlPreparedPack.getInPack(orderStatusList,MesExtConstWords.ORDER_STATUS,packBean);
        DdlPreparedPack.getNumEqualPack(personCheckDetail.getShiftGroupCheck(), MesExtConstWords.SHIFT_GROUP_CHECK, packBean);
        DdlPreparedPack.getNumEqualPack(personCheckDetail.getQualityCheck(), MesExtConstWords.QUALITY_CHECK, packBean);
        pager = PagerHelper.getPager(pager, personCheckDetailRDao.findByHqlWhereCount(packBean));
        packBean.setOrderByStr(" order by checkTime desc ");
        List<MesPersonCheckDetail> personCheckDetailList = personCheckDetailRDao.findByHqlWherePage(packBean, pager);
        if (!CollectionUtils.isEmpty(personCheckDetailList)) {
            for (MesPersonCheckDetail detail : personCheckDetailList) {
                if (null == detail) {
                    continue;
                }
                if (!StringUtils.isEmpty(detail.getShiftGroupCheck())) {
                    detail.setShiftGroupCheckName(MesExtEnumUtil.SHIFT_GROUP_CHECK.valueOfDescription(detail.getShiftGroupCheck()));
                }
                if (!StringUtils.isEmpty(detail.getQualityCheck())) {
                    detail.setQualityCheckName(MesExtEnumUtil.QUALITY_CHECK.valueOfDescription(detail.getQualityCheck()));
                }
                if (!StringUtils.isEmpty(detail.getCheckResult())) {
                    try {
                        detail.setCheckResultName(MesExtEnumUtil.CHECK_RESULT.valueOfDescription(Integer.valueOf(detail.getCheckResult())));
                    } catch (NumberFormatException e) {
                    }
                }
            }
        }
        return new ListPager(personCheckDetailList, pager);
    }


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

        return personCheckDetailRDao.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 personCheckDetailRDao.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)}
        );
    }

}
