package cn.iocoder.yudao.module.bpm.dal.mysql.oa;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.workorder.BpmOAWorkOrderPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.workorder.BpmOAWorkOrderRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAWorkOrderDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * BPM OA 工单 Mapper
 *
 * @author 系统
 */
@Mapper
public interface BpmOAWorkOrderMapper extends BaseMapperX<BpmOAWorkOrderDO> {

    /**
     * 工单分页查询
     */
    default PageResult<BpmOAWorkOrderDO> selectPage(Long loginUserId, BpmOAWorkOrderPageReqVO pageVO) {
        return selectPage(pageVO, new LambdaQueryWrapperX<BpmOAWorkOrderDO>()
                .likeIfPresent(BpmOAWorkOrderDO::getTitle, pageVO.getTitle())
                .eqIfPresent(BpmOAWorkOrderDO::getType, pageVO.getType())
                .eqIfPresent(BpmOAWorkOrderDO::getStatus, pageVO.getStatus())
                .eqIfPresent(BpmOAWorkOrderDO::getAssigneeUserId, pageVO.getAssigneeUserId())
                .eqIfPresent(BpmOAWorkOrderDO::getFromUserId, pageVO.getFromUserId())
                .betweenIfPresent(BpmOAWorkOrderDO::getCreateTime, pageVO.getCreateTime())
                .orderByDesc(BpmOAWorkOrderDO::getId));
    }

    /**
     * 查询我的工单分页（我发起的）
     */
    default PageResult<BpmOAWorkOrderDO> selectMyPage(Long loginUserId, BpmOAWorkOrderPageReqVO pageVO) {
        return selectPage(pageVO, new LambdaQueryWrapperX<BpmOAWorkOrderDO>()
                .eq(BpmOAWorkOrderDO::getFromUserId, loginUserId)
                .likeIfPresent(BpmOAWorkOrderDO::getTitle, pageVO.getTitle())
                .eqIfPresent(BpmOAWorkOrderDO::getType, pageVO.getType())
                .eqIfPresent(BpmOAWorkOrderDO::getStatus, pageVO.getStatus())
                .betweenIfPresent(BpmOAWorkOrderDO::getCreateTime, pageVO.getCreateTime())
                .orderByDesc(BpmOAWorkOrderDO::getId));
    }

    /**
     * 查询我负责的工单分页（分配给我的）
     */
    default PageResult<BpmOAWorkOrderDO> selectAssignedPage(Long loginUserId, BpmOAWorkOrderPageReqVO pageVO) {
        return selectPage(pageVO, new LambdaQueryWrapperX<BpmOAWorkOrderDO>()
                .eq(BpmOAWorkOrderDO::getAssigneeUserId, loginUserId)
                .likeIfPresent(BpmOAWorkOrderDO::getTitle, pageVO.getTitle())
                .eqIfPresent(BpmOAWorkOrderDO::getType, pageVO.getType())
                .eqIfPresent(BpmOAWorkOrderDO::getStatus, pageVO.getStatus())
                .betweenIfPresent(BpmOAWorkOrderDO::getCreateTime, pageVO.getCreateTime())
                .orderByDesc(BpmOAWorkOrderDO::getId));
    }

    /**
     * 查询我负责工单的数量
     */
    default Long selectMyAssignedCount(Long userId) {
        return selectCount(new LambdaQueryWrapperX<BpmOAWorkOrderDO>()
                .eq(BpmOAWorkOrderDO::getAssigneeUserId, userId)
                .in(BpmOAWorkOrderDO::getStatus, 1, 2)); // 待分配和处理中
    }

    /**
     * 工单分页查询（带关联信息）- XML实现
     */
    IPage<BpmOAWorkOrderRespVO> selectWorkOrderPage(@Param("page") Page page,
                                                    @Param("req") BpmOAWorkOrderPageReqVO req);

    /**
     * 我发起的工单分页查询（带关联信息）- XML实现
     */
    PageResult<BpmOAWorkOrderRespVO> selectMyWorkOrderPage(@Param("page") Page page,
                                                           @Param("req") BpmOAWorkOrderPageReqVO req);

    /**
     * 分配给我的工单分页查询（带关联信息）- XML实现
     */
    IPage<BpmOAWorkOrderRespVO> selectAssignedWorkOrderPage(@Param("page") Page page,
                                                            @Param("req") BpmOAWorkOrderPageReqVO req);

    BpmOAWorkOrderDO selectByProcessInstanceId(@Param("processInstanceId") String processInstanceId);
}
