package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.api.busi.jx.ISxWorkCellTaktCollectRecordService;
import cn.estsh.i3plus.ext.mes.pojo.bean.SxWorkCellTaktCollectRecord;
import cn.estsh.i3plus.ext.mes.pojo.repository.SxWorkCellTaktCollectRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
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.HqlPack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-jx
 * @DESCRIPTION: 松下工位节拍采集记录
 * @USER: xinwang.yi
 * @DATE: 2023-12-05 14:21
 */
@Service
public class SxWorkCellTaktCollectRecordService implements ISxWorkCellTaktCollectRecordService {

    @Autowired
    private SxWorkCellTaktCollectRecordRepository workCellTaktCollectRecordRepository;

    @Override
    public ListPager<SxWorkCellTaktCollectRecord> queryWorkCellTaktCollectRecrodLByPager(SxWorkCellTaktCollectRecord workCellTaktCollectRecord, Pager pager) {
        if (workCellTaktCollectRecord == null) {
            pager = PagerHelper.getPager(pager, workCellTaktCollectRecordRepository.listCount());
            return new ListPager(workCellTaktCollectRecordRepository.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesExtHqlPack.getSxWorkCellTaktCollectRecord(workCellTaktCollectRecord);
            hqlPack.setOrderByStr(workCellTaktCollectRecord.orderBy());
            pager = PagerHelper.getPager(pager, workCellTaktCollectRecordRepository.findByHqlWhereCount(hqlPack));
            List<SxWorkCellTaktCollectRecord> byHqlWherePage = workCellTaktCollectRecordRepository.findByHqlWherePage(hqlPack, pager);
            return new ListPager(byHqlWherePage, pager);
        }
    }

    @Override
    public int updateWorkCellTaktCollectRecordValid(String[] ids, int status, String userName) {

        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), MesExtConstWords.ID, wheres);

        int num = workCellTaktCollectRecordRepository.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesExtConstWords.SYSTEM_SYNC_STATUS, MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue(), status, userName, TimeTool.getNowTime(true)}
        );
        return num;

    }
}
