package com.cf.imes.module.system.dal.mysql.process;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.cf.imes.module.system.controller.admin.process.vo.process.ProcessPageReqVO;
import com.cf.imes.module.system.dal.dataobject.process.ProcessDO;
import com.cf.imes.module.system.dal.dataobject.process.ProcessUserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 工序信息表 process Mapper
 *
 * @author 晨丰科技
 */
@Mapper
public interface ProcessMapper extends BaseMapperX<ProcessDO> {

    default PageResult<ProcessDO> selectPage(ProcessPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProcessDO>()
                .likeIfPresent(ProcessDO::getName, reqVO.getName())
                .eqIfPresent(ProcessDO::getPieceRate, reqVO.getPieceRate())
                .eqIfPresent(ProcessDO::getPieceType, reqVO.getPieceType())
                .eqIfPresent(ProcessDO::getType, reqVO.getType())
                .eqIfPresent(ProcessDO::getIsCustom, reqVO.getIsCustom())
                .eqIfPresent(ProcessDO::getIsDealer, reqVO.getIsDealer())
                .eqIfPresent(ProcessDO::getSort, reqVO.getSort())
                .eqIfPresent(ProcessDO::getHourCapacity, reqVO.getHourCapacity())
                .eqIfPresent(ProcessDO::getUnit, reqVO.getUnit())
                .eqIfPresent(ProcessDO::getOrganId, reqVO.getOrganId())
                .eqIfPresent(ProcessDO::getIsEnabled, reqVO.getIsEnabled())
                .betweenIfPresent(ProcessDO::getPrepareTime, reqVO.getPrepareTime())
                .likeIfPresent(ProcessDO::getDescription, reqVO.getDescription())
                .betweenIfPresent(ProcessDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ProcessDO::getId));
    }

    IPage<ProcessDO> selectProcessPage(@Param("page") IPage<ProcessDO> page , @Param("vo") ProcessPageReqVO reqVO);

    ProcessDO selectOneById(@Param("id") Long id, @Param("organId") Long organId);

    default List<ProcessDO> selectAll(Long organId) {
        return selectList(new LambdaQueryWrapperX<ProcessDO>()
//                .orderByDesc(ProcessDO::getId)
                .eqIfPresent(ProcessDO::getOrganId, organId)
                .eqIfPresent(ProcessDO::getIsEnabled, true));
    }

    int deleteById(@Param("id") Long id);

    default List<ProcessDO> selectProcessList(Long organId,Long userId) {
        return selectJoinList(ProcessDO.class, new MPJLambdaWrapperX<ProcessDO>()
                .leftJoin(ProcessUserDO.class, ProcessUserDO::getProcessId, ProcessDO::getId)
                .eq(ProcessUserDO::getUserId, userId)
                .eq(ProcessDO::getOrganId, organId)
                .eq(ProcessDO::getIsEnabled,true)
                .selectAll(ProcessDO.class)
                .orderByDesc(ProcessDO::getId));


    }
}