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

import cn.estsh.i3plus.ext.mes.api.base.IConsistencyAffirmRecordService;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IConsistencyAffirmRecordDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesConsistencyAffirmRecord;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesConsistencyAffirmRecordRepository;
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 ConsistencyAffirmRecordService implements IConsistencyAffirmRecordService {
    @Autowired
    private MesConsistencyAffirmRecordRepository consistencyAffirmRecordRDao;
    @Autowired
    private IConsistencyAffirmRecordDao consistencyAffirmRecordDao;
    @Override
    public ListPager<MesConsistencyAffirmRecord> queryConsistencyAffirmRecordByPager(MesConsistencyAffirmRecord consistencyAffirmRecord, Pager pager) {
        pager = PagerHelper.getPager(pager, consistencyAffirmRecordDao.queryConsistencyAffirmRecordCount(consistencyAffirmRecord));
        List<MesConsistencyAffirmRecord> consistencyAffirmRecordList = consistencyAffirmRecordDao.queryConsistencyAffirmRecordByPager(consistencyAffirmRecord , pager);
        if (!CollectionUtils.isEmpty(consistencyAffirmRecordList)) {
            for (MesConsistencyAffirmRecord detail : consistencyAffirmRecordList) {
                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())) {
                    detail.setCheckResultName(MesExtEnumUtil.CHECK_RESULT.valueOfDescription(detail.getCheckResult()));
                }
            }
        }
        return new ListPager(consistencyAffirmRecordList, pager);
    }


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

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

}
