package cn.iocoder.yudao.module.pms.dal.mysql.iotprojecttask;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
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.pms.controller.admin.iotprojecttask.vo.IotProjectTaskPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttask.IotProjectTaskDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 项目信息任务拆分 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface IotProjectTaskMapper extends BaseMapperX<IotProjectTaskDO> {

    default PageResult<IotProjectTaskDO> selectPage(IotProjectTaskPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotProjectTaskDO>()
                .eqIfPresent(IotProjectTaskDO::getProjectId, reqVO.getProjectId())
                .likeIfPresent(IotProjectTaskDO::getWellName, reqVO.getWellName())
                .eqIfPresent(IotProjectTaskDO::getWellType, reqVO.getWellType())
                .eqIfPresent(IotProjectTaskDO::getLocation, reqVO.getLocation())
                .eqIfPresent(IotProjectTaskDO::getTechnique, reqVO.getTechnique())
                .eqIfPresent(IotProjectTaskDO::getWorkloadDesign, reqVO.getWorkloadDesign())
                .betweenIfPresent(IotProjectTaskDO::getCreateTime, reqVO.getCreateTime())
                .likeIfPresent(IotProjectTaskDO::getUserName, reqVO.getUserName())
                .eqIfPresent(IotProjectTaskDO::getUserId, reqVO.getUserId())
                .eqIfPresent(IotProjectTaskDO::getRemark, reqVO.getRemark())
                .orderByDesc(IotProjectTaskDO::getId));
    }

    default PageResult<IotProjectTaskDO> selectPage1(IotProjectTaskPageReqVO reqVO,Collection<Long> deptIds) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotProjectTaskDO>()
                .eqIfPresent(IotProjectTaskDO::getId, reqVO.getId())
                .eqIfPresent(IotProjectTaskDO::getProjectId, reqVO.getProjectId())
                .likeIfPresent(IotProjectTaskDO::getWellName, reqVO.getWellName())
                .eqIfPresent(IotProjectTaskDO::getWellType, reqVO.getWellType())
                .eqIfPresent(IotProjectTaskDO::getLocation, reqVO.getLocation())
                .eqIfPresent(IotProjectTaskDO::getTechnique, reqVO.getTechnique())
                .eqIfPresent(IotProjectTaskDO::getWorkloadDesign, reqVO.getWorkloadDesign())
                .betweenIfPresent(IotProjectTaskDO::getCreateTime, reqVO.getCreateTime())
                .likeIfPresent(IotProjectTaskDO::getUserName, reqVO.getUserName())
                .eqIfPresent(IotProjectTaskDO::getUserId, reqVO.getUserId())
                .inIfPresent(IotProjectTaskDO::getDeptId,deptIds)
                .eqIfPresent(IotProjectTaskDO::getRemark, reqVO.getRemark())
                .orderByDesc(IotProjectTaskDO::getId));
    }

    default List<IotProjectTaskDO> selectList(IotProjectTaskPageReqVO reqVO) {
        LambdaQueryWrapperX<IotProjectTaskDO> wrapper = buildCommonQuery(reqVO)
                .inIfPresent(IotProjectTaskDO::getId, reqVO.getTaskIds());

        // 处理deptId特殊查询
        if (ObjUtil.isNotEmpty(reqVO.getDeptId())) {
            // wrapper.apply("FIND_IN_SET({0}, REPLACE(REPLACE(dept_ids,'[',''),']',''))", reqVO.getDeptId());
            // wrapper.apply("JSON_CONTAINS(dept_ids, {0})", reqVO.getDeptId());
            wrapper.apply("JSON_CONTAINS(CAST(dept_ids AS JSON), CAST({0} AS JSON))", reqVO.getDeptId());
        }

        // 新增searchKey多字段模糊查询
        if (ObjUtil.isNotEmpty(reqVO.getSearchKey())) {
            wrapper.and(wq -> wq.like(IotProjectTaskDO::getWellName, reqVO.getSearchKey())
                    .or()
                    .like(IotProjectTaskDO::getLocation, reqVO.getSearchKey()));
        }
        // 定时任务 如果是平台井 只生成主井号任务
        if (ObjUtil.isNotEmpty(reqVO.getJobFlag())) {
            wrapper.and(wq -> wq.ne(IotProjectTaskDO::getStatus, "wg"));
                    /* .and(innerWq -> innerWq.eq(IotProjectTaskDO::getPlatformWell, 0)
                    .or()
                    .eq(IotProjectTaskDO::getPlatformWell, 1))); */
        }
        return selectList(wrapper);
    }

    default List<IotProjectTaskDO> selectAppList(IotProjectTaskPageReqVO reqVO) {
        LambdaQueryWrapperX<IotProjectTaskDO> wrapper = buildCommonQuery(reqVO)
                .inIfPresent(IotProjectTaskDO::getId, reqVO.getTaskIds());

        // 处理deptId特殊查询
        if (CollUtil.isNotEmpty(reqVO.getDeptIds())) {
            String deptIdsJson = reqVO.getDeptIds().stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(",", "[", "]"));
            wrapper.apply("JSON_OVERLAPS(dept_ids, CAST({0} AS JSON))", deptIdsJson);
        }

        // 新增searchKey多字段模糊查询
        if (ObjUtil.isNotEmpty(reqVO.getSearchKey())) {
            wrapper.and(wq -> wq.like(IotProjectTaskDO::getWellName, reqVO.getSearchKey())
                    .or()
                    .like(IotProjectTaskDO::getLocation, reqVO.getSearchKey()));
        }
        // 定时任务 如果是平台井 只生成主井号任务
        if (ObjUtil.isNotEmpty(reqVO.getJobFlag())) {
            wrapper.and(wq -> wq.ne(IotProjectTaskDO::getStatus, "wg"));
                    /* .and(innerWq -> innerWq.eq(IotProjectTaskDO::getPlatformWell, 0)
                    .or()
                    .eq(IotProjectTaskDO::getPlatformWell, 1))); */
        }
        return selectList(wrapper);
    }

    IPage<IotProjectTaskDO> taskList(IPage<?> page,
                                     @Param("companyId") Long companyId,
                                     @Param("manufactureName") String manufactureName,
                                     @Param("contractName") String contractName,
                                     @Param("contractCode") String contractCode,
                                     @Param("wellName") String wellName,
                                     @Param("createTime") LocalDateTime[] createTime,
                                     @Param("deptIds") Collection<Long> deptIds,
                                     @Param("dataPermissionTaskIds") Collection<Long> dataPermissionTaskIds,
                                     @Param("searchDeptIds") Collection<Long> searchDeptIds,
                                     @Param("projectIds") Collection<Long> projectIds,
                                     @Param("qualifiedTaskIds") Collection<Long> qualifiedTaskIds,
                                     @Param("platformFlag") String platformFlag
    );

    /**
     * 构建公共查询条件
     * @param reqVO 查询条件
     */
    default LambdaQueryWrapperX<IotProjectTaskDO> buildCommonQuery(IotProjectTaskPageReqVO reqVO) {
        return new LambdaQueryWrapperX<IotProjectTaskDO>()
                .eqIfPresent(IotProjectTaskDO::getDeptId, reqVO.getCompanyId())
                .eqIfPresent(IotProjectTaskDO::getProjectId, reqVO.getProjectId())
                .likeIfPresent(IotProjectTaskDO::getWellName, reqVO.getWellName())
                .eqIfPresent(IotProjectTaskDO::getWellType, reqVO.getWellType())
                .eqIfPresent(IotProjectTaskDO::getPlatformGroup, reqVO.getPlatformGroup())
                .inIfPresent(IotProjectTaskDO::getPlatformGroup, reqVO.getPlatformGroups())
                .eqIfPresent(IotProjectTaskDO::getLocation, reqVO.getLocation())
                .eqIfPresent(IotProjectTaskDO::getTechnique, reqVO.getTechnique())
                .eqIfPresent(IotProjectTaskDO::getWorkloadDesign, reqVO.getWorkloadDesign())
                .betweenIfPresent(IotProjectTaskDO::getCreateTime, reqVO.getCreateTime())
                .likeIfPresent(IotProjectTaskDO::getUserName, reqVO.getUserName())
                .eqIfPresent(IotProjectTaskDO::getUserId, reqVO.getUserId())
                .eqIfPresent(IotProjectTaskDO::getRemark, reqVO.getRemark())
                .orderByDesc(IotProjectTaskDO::getId);
    }

}