package com.cf.imes.module.executor.dal.mysql.processStep;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.module.executor.dal.dataobject.process.OrderProcessDO;
import com.cf.imes.module.executor.dal.dataobject.processStep.ProcessStepDO;
import org.apache.ibatis.annotations.Mapper;
import com.cf.imes.module.executor.controller.admin.processStep.vo.*;

import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;

/**
 * 生产单工序步骤 Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface ProcessStepMapper extends BaseMapperX<ProcessStepDO> {

    default PageResult<ProcessStepDO> selectPage(ProcessStepPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProcessStepDO>()
                .eqIfPresent(ProcessStepDO::getOrderId, reqVO.getOrderId())
                .eq(ProcessStepDO::getOrganId,getUserOrganId())
                .betweenIfPresent(ProcessStepDO::getFinishTime, reqVO.getFinishTime())
                .eqIfPresent(ProcessStepDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ProcessStepDO::getType, reqVO.getType())
                .eqIfPresent(ProcessStepDO::getPieceRate, reqVO.getPieceRate())
                .eqIfPresent(ProcessStepDO::getPieceType, reqVO.getPieceType())
                .eqIfPresent(ProcessStepDO::getSort, reqVO.getSort())
                .likeIfPresent(ProcessStepDO::getName, reqVO.getName())
                .eqIfPresent(ProcessStepDO::getProcessinfoId, reqVO.getProcessinfoId())
                .eqIfPresent(ProcessStepDO::getOrderProcessId, reqVO.getOrderProcessId())
                .betweenIfPresent(ProcessStepDO::getScheduleDate, reqVO.getScheduleDate())
                .eqIfPresent(ProcessStepDO::getDuration, reqVO.getDuration())
                .eqIfPresent(ProcessStepDO::getManager, reqVO.getManager())
                .betweenIfPresent(ProcessStepDO::getProcessDate, reqVO.getProcessDate())
                .betweenIfPresent(ProcessStepDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ProcessStepDO::getId));
    }


    // 批量修改删除状态
    default int updateOrderDeleted(Long id, Integer deleted, Long organId) {
        LambdaUpdateWrapper<ProcessStepDO> wrapper = new LambdaUpdateWrapper<ProcessStepDO>()
                .set(ProcessStepDO::getDeleted, deleted)
                .eq(ProcessStepDO::getOrderId, id)
                .eq(ProcessStepDO::getOrganId, organId);
        if (deleted == 0)
            wrapper.eq(ProcessStepDO::getDeleted, 1);
        return update(wrapper);
    }
}