package cn.iocoder.yudao.module.server.dal.mysql.workorderInfos;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.server.dal.dataobject.workorderInfos.WorkorderInfosDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.server.controller.admin.workorderInfos.vo.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工单 Mapper
 *
 * @author wyj
 */
@Mapper
public interface WorkorderInfosMapper extends BaseMapperX<WorkorderInfosDO> {

    default PageResult<WorkorderInfosDO> selectPage(WorkorderInfosPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WorkorderInfosDO>()
                .eqIfPresent(WorkorderInfosDO::getCategoryId, reqVO.getCategoryId())
                .eqIfPresent(WorkorderInfosDO::getTechOperatorId, reqVO.getTechOperatorId())
                .eqIfPresent(WorkorderInfosDO::getUserId, reqVO.getUserId())
                .likeIfPresent(WorkorderInfosDO::getRequirement, reqVO.getRequirement())
                .eqIfPresent(WorkorderInfosDO::getRequiredInfo, reqVO.getRequiredInfo())
                .betweenIfPresent(WorkorderInfosDO::getOrderTime, reqVO.getOrderTime())
                .inIfPresent(WorkorderInfosDO::getStatus, reqVO.getStatus())
                .eqIfPresent(WorkorderInfosDO::getRejectReason, reqVO.getRejectReason())
                .betweenIfPresent(WorkorderInfosDO::getExpectedCompletionDate, reqVO.getExpectedCompletionDate())
                .betweenIfPresent(WorkorderInfosDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(WorkorderInfosDO::getOrderTime));
    }

    default PageResult<WorkorderInfosDO> selectPage2(WorkorderInfosPageReqVO reqVO, List<Long> matchedIds) {

        LambdaQueryWrapperX<WorkorderInfosDO> wrapperX = new LambdaQueryWrapperX<WorkorderInfosDO>()
                .inIfPresent(WorkorderInfosDO::getId, matchedIds)
                .eqIfPresent(WorkorderInfosDO::getCategoryId, reqVO.getCategoryId())
                .eqIfPresent(WorkorderInfosDO::getTechOperatorId, reqVO.getTechOperatorId())
                .eqIfPresent(WorkorderInfosDO::getUserId, reqVO.getUserId())
                .likeIfPresent(WorkorderInfosDO::getRequirement, reqVO.getRequirement())
                .eqIfPresent(WorkorderInfosDO::getRequiredInfo, reqVO.getRequiredInfo())
                .betweenIfPresent(WorkorderInfosDO::getOrderTime, reqVO.getOrderTime())
                .eqIfPresent(WorkorderInfosDO::getRejectReason, reqVO.getRejectReason())
                .betweenIfPresent(WorkorderInfosDO::getExpectedCompletionDate, reqVO.getExpectedCompletionDate())
                .betweenIfPresent(WorkorderInfosDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(WorkorderInfosDO::getOrderTime);
//        if("search".equals(reqVO.getSearch()) && reqVO.getStatus() !=null) {
//            wrapperX.inIfPresent(WorkorderInfosDO::getStatus,reqVO.getStatus());
//        }
        return selectPage(reqVO, wrapperX);
    }
    Page<WorkorderInfosDO> selectWorkorders(
            @Param("page") Page<WorkorderInfosDO> page,
            @Param("reqVO") WorkorderInfosPageReqVO reqVO,
            @Param("idList") List<Long> idList
    );

    default PageResult<WorkorderInfosDO> selectPage3(WorkorderInfosPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WorkorderInfosDO>()
                .eqIfPresent(WorkorderInfosDO::getTechOperatorId, reqVO.getTechOperatorId())
                .eqIfPresent(WorkorderInfosDO::getUserId, reqVO.getUserId())
                .eqIfPresent(WorkorderInfosDO::getCategoryId, reqVO.getCategoryId())
                .likeIfPresent(WorkorderInfosDO::getRequirement, reqVO.getRequirement())
                .betweenIfPresent(WorkorderInfosDO::getOrderTime, reqVO.getOrderTime())
                .inIfPresent(WorkorderInfosDO::getStatus, reqVO.getStatus())
                .eqIfPresent(WorkorderInfosDO::getRejectReason, reqVO.getRejectReason())
                .betweenIfPresent(WorkorderInfosDO::getExpectedCompletionDate, reqVO.getExpectedCompletionDate())
                .betweenIfPresent(WorkorderInfosDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(WorkorderInfosDO::getOrderTime));
    }

    default PageResult<WorkorderInfosDO> selectPage4(WorkorderInfosPageReqVO reqVO,List<Long> matchedIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WorkorderInfosDO>()
                .inIfPresent(WorkorderInfosDO::getId,matchedIds)
                .eqIfPresent(WorkorderInfosDO::getTechOperatorId, reqVO.getTechOperatorId())
                .eqIfPresent(WorkorderInfosDO::getUserId, reqVO.getUserId())
                .eqIfPresent(WorkorderInfosDO::getCategoryId, reqVO.getCategoryId())
                .likeIfPresent(WorkorderInfosDO::getRequirement, reqVO.getRequirement())
                .betweenIfPresent(WorkorderInfosDO::getOrderTime, reqVO.getOrderTime())
                .inIfPresent(WorkorderInfosDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(WorkorderInfosDO::getExpectedCompletionDate, reqVO.getExpectedCompletionDate())
                .orderByDesc(WorkorderInfosDO::getOrderTime));
    }


    default PageResult<WorkorderInfosDO> pendingWorkorderSelectPage(WorkorderInfosPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WorkorderInfosDO>()
                .ne(WorkorderInfosDO::getStatus,"70")
                .ne(WorkorderInfosDO::getStatus,"0")
                .orderByDesc(WorkorderInfosDO::getOrderTime));
    }

    default PageResult<WorkorderInfosDO> pendingWorkorderSelectPage2(WorkorderInfosPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WorkorderInfosDO>()
                .eqIfPresent(WorkorderInfosDO::getTechOperatorId, reqVO.getTechOperatorId())
                .eqIfPresent(WorkorderInfosDO::getUserId, reqVO.getUserId())
                .inIfPresent(WorkorderInfosDO::getStatus, reqVO.getStatus())
                .orderByDesc(WorkorderInfosDO::getOrderTime));
    }

}