package cn.iocoder.yudao.module.pms.dal.mysql.qhse.accident;

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.qhse.accident.vo.IotAccidentReportPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.accident.IotAccidentReportDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Set;

/**
 * QHSE事故上报 Mapper
 *
 * @author 超级管理员
 */
@Mapper
public interface IotAccidentReportMapper extends BaseMapperX<IotAccidentReportDO> {

    default PageResult<IotAccidentReportDO> selectPage(IotAccidentReportPageReqVO reqVO, Set<Long> ids) {
        LambdaQueryWrapperX<IotAccidentReportDO> queryWrapperX = new LambdaQueryWrapperX<IotAccidentReportDO>()
                .betweenIfPresent(IotAccidentReportDO::getActualTime, reqVO.getActualTime())
                .eqIfPresent(IotAccidentReportDO::getAccidentGrade, reqVO.getAccidentGrade())
                .eqIfPresent(IotAccidentReportDO::getAccidentType, reqVO.getAccidentType())
                .eqIfPresent(IotAccidentReportDO::getAccidentAddress, reqVO.getAccidentAddress())
                .inIfPresent(IotAccidentReportDO::getDeptId, ids)
                .likeIfPresent(IotAccidentReportDO::getDeptName, reqVO.getDeptName())
                .eqIfPresent(IotAccidentReportDO::getDutyPerson, reqVO.getDutyPerson())
                .eqIfPresent(IotAccidentReportDO::getLossSituation, reqVO.getLossSituation())
                .eqIfPresent(IotAccidentReportDO::getEmergencyMeasure, reqVO.getEmergencyMeasure())
                .eqIfPresent(IotAccidentReportDO::getDescription, reqVO.getDescription())
                .eqIfPresent(IotAccidentReportDO::getPic, reqVO.getPic())
//                .inIfPresent(IotAccidentReportDO::getCurrentPerson, reqVO.getCurrentPerson())
                .betweenIfPresent(IotAccidentReportDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(IotAccidentReportDO::getRemark, reqVO.getRemark())
                .orderByDesc(IotAccidentReportDO::getId);
        if (reqVO.getCurrentPerson() != null) {
            queryWrapperX.apply("JSON_CONTAINS(CAST(current_person AS JSON), CAST({0} AS JSON))", reqVO.getCurrentPerson());

        }
        return selectPage(reqVO, queryWrapperX);
    }


    default PageResult<IotAccidentReportDO> selectApprovalPage(IotAccidentReportPageReqVO reqVO) {
        LambdaQueryWrapperX<IotAccidentReportDO> queryWrapperX = (LambdaQueryWrapperX<IotAccidentReportDO>) new LambdaQueryWrapperX<IotAccidentReportDO>()
                // 原有基础条件（保持不变，使用 LambdaQueryWrapperX 的扩展方法）
                .betweenIfPresent(IotAccidentReportDO::getActualTime, reqVO.getActualTime())
                .eqIfPresent(IotAccidentReportDO::getAccidentGrade, reqVO.getAccidentGrade())
                .eqIfPresent(IotAccidentReportDO::getAccidentType, reqVO.getAccidentType())
                .eqIfPresent(IotAccidentReportDO::getAccidentAddress, reqVO.getAccidentAddress())
                .eqIfPresent(IotAccidentReportDO::getDeptId, reqVO.getDeptId())
                .likeIfPresent(IotAccidentReportDO::getDeptName, reqVO.getDeptName())
                .eqIfPresent(IotAccidentReportDO::getDutyPerson, reqVO.getDutyPerson())
                .eqIfPresent(IotAccidentReportDO::getLossSituation, reqVO.getLossSituation())
                .eqIfPresent(IotAccidentReportDO::getEmergencyMeasure, reqVO.getEmergencyMeasure())
                .eqIfPresent(IotAccidentReportDO::getDescription, reqVO.getDescription())
                .eqIfPresent(IotAccidentReportDO::getPic, reqVO.getPic())
                .betweenIfPresent(IotAccidentReportDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(IotAccidentReportDO::getRemark, reqVO.getRemark())
                // 修复：嵌套逻辑使用原生 LambdaQueryWrapper 类型，手动实现非空判断
                .nested((LambdaQueryWrapper<IotAccidentReportDO> wrapper) -> {
                    boolean hasIdCondition = false;
                    // 手动实现 inIfPresent 的逻辑：ids 非空时才添加 in 条件
                    if (reqVO.getIds() != null && !reqVO.getIds().isEmpty()) {
                        wrapper.in(IotAccidentReportDO::getId, reqVO.getIds());
                        hasIdCondition = true;
                    }
                    // 处理 currentPerson 的 OR 条件
                    if (reqVO.getCurrentPerson() != null) {
                        // 只有当已有 id 条件时才加 OR，避免空条件下的语法错误
                        if (hasIdCondition) {
                            wrapper.or();
                        }
                        wrapper.apply("JSON_CONTAINS(CAST(current_person AS JSON), CAST({0} AS JSON))", reqVO.getCurrentPerson());
                    }
                })
                .orderByDesc(IotAccidentReportDO::getId);

        return selectPage(reqVO, queryWrapperX);
    }

    default List<IotAccidentReportDO> selectListByDept(IotAccidentReportPageReqVO reqVO, Set<Long> ids) {
        return selectList(new LambdaQueryWrapperX<IotAccidentReportDO>()
                .eq(IotAccidentReportDO::getDeleted, false)
                .betweenIfPresent(IotAccidentReportDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(IotAccidentReportDO::getDeptId, ids));
    }
}