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

import cn.estsh.i3plus.ext.mes.api.base.IProjectTourConfirmService;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IProjectTourConfirmDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProjectTourDetail;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProjectTourDetailRepository;
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.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
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.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/04 19:27
 * @Description:
 **/
@Service
public class ProjectTourConfirmService implements IProjectTourConfirmService {
    @Autowired
    private MesProjectTourDetailRepository projectTourDetailRDao;
    @Autowired
    private IProjectTourConfirmDao projectTourConfirmDao;
    @Override
    public ListPager<MesProjectTourDetail> queryProjectTourDetailByPager(MesProjectTourDetail projectTourDetail, Pager pager) {
        pager = PagerHelper.getPager(pager, projectTourConfirmDao.queryProjectTourDetailByPagerCount(projectTourDetail));
        List<MesProjectTourDetail> projectTourDetailList = projectTourConfirmDao.queryProjectTourDetailByPager(projectTourDetail, pager);
        if (!CollectionUtils.isEmpty(projectTourDetailList)) {
            for (MesProjectTourDetail detail : projectTourDetailList) {
                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.getProdCfgTypeClassify())) {
                    detail.setProdCfgTypeClassifyName(MesExtEnumUtil.PROD_CFG_TYPE_CLASSIFY.valueOfDescription(detail.getProdCfgTypeClassify()));
                }
                if (!StringUtils.isEmpty(detail.getCheckResult())) {
                    detail.setCheckResultName(MesExtEnumUtil.CHECK_RESULT.valueOfDescription(detail.getCheckResult()));
                }
            }
        }
        return new ListPager(projectTourDetailList, pager);
    }

    @Override
    public int updateProjectTourDetail(MesProjectTourDetail projectTourDetail) {

        return projectTourDetailRDao.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{projectTourDetail.getId()},
                new String[]{MesExtConstWords.BAD_MAINTAIN_MEASURE, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{projectTourDetail.getBadMaintainMeasure(), AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)});
    }


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

        return projectTourDetailRDao.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), MesExtConstWords.ID, wheres);

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

}
