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

import cn.estsh.i3plus.ext.mes.pcn.api.report.IMesCimSeresReportService;
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.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.seres.MesCimSeresJisQueue;
import cn.estsh.i3plus.pojo.mes.bean.seres.MesCimSeresSrmRunSheetJis;
import cn.estsh.i3plus.pojo.mes.repository.seres.IMesCimSeresJisQueueRepository;
import cn.estsh.i3plus.pojo.mes.repository.seres.IMesCimSeresSrmRunSheetJisRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MesCimSeresReportServiceImpl implements IMesCimSeresReportService {

    @Autowired
    private IMesCimSeresSrmRunSheetJisRepository runSheetJisRDao;

    @Autowired
    private IMesCimSeresJisQueueRepository queueJisRDao;

    @Override
    public ListPager<MesCimSeresSrmRunSheetJis> querySrmSunSheetJisLogByPager(Integer status, String docNo,
                                                                              String partClassCode, String partClassName,
                                                                              Pager pager) {
        final String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringLikerPack(docNo, "docNo", packBean);
        DdlPreparedPack.getNumEqualPack(status, "status", packBean);
        DdlPreparedPack.getStringLikerPack(partClassCode, "partClassCode", packBean);
        DdlPreparedPack.getStringLikerPack(partClassName, "partClassName", packBean);
        DdlPreparedPack.getOrderBy("id", CommonEnumUtil.ASC_OR_DESC.DESC.getValue(), packBean);

        pager = PagerHelper.getPager(pager, runSheetJisRDao.findByHqlWhereCount(packBean));
        List<MesCimSeresSrmRunSheetJis> list = runSheetJisRDao.findByHqlWherePage(packBean, pager);
        return new ListPager<>(list, pager);
    }

    @Override
    public void doSrmSunSheetJisNormal(List<Long> ids) {
        final String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        final String username = AuthUtil.getSessionUser().getUserName();
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(ids, "id", packBean);

        runSheetJisRDao.updateByProperties(new String[]{"status","modifyUser","modifyDatetime"},new Object[]{MesExtEnumUtil.CIM_SERES_JIS_STATUS.WAIT_CHECK.getValue(), username, TimeTool.getNowTime(true)}, packBean);
    }

    @Override
    public ListPager<MesCimSeresJisQueue> queryJisQueueLogByPager(Integer checkStatus, String vin, String materielCode,
                                                                  String partClassCode, String partClassName, Pager pager) {
        final String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringLikerPack(vin, "vin", packBean);
        DdlPreparedPack.getStringLikerPack(materielCode, "materielCode", packBean);
        DdlPreparedPack.getNumEqualPack(checkStatus, "checkStatus", packBean);
        DdlPreparedPack.getStringLikerPack(partClassCode, "partClassCode", packBean);
        DdlPreparedPack.getStringLikerPack(partClassName, "partClassName", packBean);
        DdlPreparedPack.getOrderBy("id", CommonEnumUtil.ASC_OR_DESC.DESC.getValue(), packBean);

        pager = PagerHelper.getPager(pager, queueJisRDao.findByHqlWhereCount(packBean));
        List<MesCimSeresJisQueue> list = queueJisRDao.findByHqlWherePage(packBean, pager);
        return new ListPager<>(list, pager);
    }

    @Override
    public void doJisQueueNormal(List<Long> ids) {
        final String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
        final String username = AuthUtil.getSessionUser().getUserName();
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(ids, "id", packBean);

        queueJisRDao.updateByProperties(new String[]{"checkStatus","modifyUser","modifyDatetime"},new Object[]{MesExtEnumUtil.CIM_SERES_JIS_STATUS.WAIT_CHECK.getValue(), username, TimeTool.getNowTime(true)}, packBean);
    }

}
