package com.njcn.rdms.module.project.dal.mysql.project;

import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementPageReqVO;
import com.njcn.rdms.module.project.dal.dataobject.project.ProjectRequirementDO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 项目需求 Mapper
 */
@Mapper
public interface ProjectRequirementMapper extends BaseMapperX<ProjectRequirementDO> {

    /**
     * 分页查询需求列表
     */
    default PageResult<ProjectRequirementDO> selectPage(ProjectRequirementPageReqVO reqVO) {
        return selectPage(reqVO, buildQueryWrapper(reqVO));
    }

    /**
     * 查询所有符合条件的需求列表，用于树查询
     */
    default List<ProjectRequirementDO> selectList(ProjectRequirementPageReqVO reqVO) {
        return selectList(buildQueryWrapper(reqVO));
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapperX<ProjectRequirementDO> buildQueryWrapper(ProjectRequirementPageReqVO reqVO) {
        LambdaQueryWrapperX<ProjectRequirementDO> queryWrapper = new LambdaQueryWrapperX<>();
        if (StringUtils.hasText(reqVO.getTitle())) {
            queryWrapper.like(ProjectRequirementDO::getTitle, reqVO.getTitle());
        }
        queryWrapper.eqIfPresent(ProjectRequirementDO::getCategory, reqVO.getCategory())
                .eqIfPresent(ProjectRequirementDO::getPriority, reqVO.getPriority())
                .eqIfPresent(ProjectRequirementDO::getStatusCode, reqVO.getStatusCode())
                .eqIfPresent(ProjectRequirementDO::getCurrentHandlerUserId, reqVO.getCurrentHandlerUserId())
                .eqIfPresent(ProjectRequirementDO::getSourceType, reqVO.getSourceType())
                .inIfPresent(ProjectRequirementDO::getModuleId, reqVO.getModuleIds())
                .eqIfPresent(ProjectRequirementDO::getModuleId, reqVO.getModuleId())
                .eqIfPresent(ProjectRequirementDO::getParentId, reqVO.getParentId())
                .eq(ProjectRequirementDO::getProjectId, reqVO.getProjectId())
                .orderByAsc(ProjectRequirementDO::getSort)
                .orderByDesc(ProjectRequirementDO::getCreateTime);
        return queryWrapper;
    }

    /**
     * 根据父需求 ID 查询子需求列表
     */
    default List<ProjectRequirementDO> selectListByParentId(Long parentId) {
        return selectList(new LambdaQueryWrapperX<ProjectRequirementDO>()
                .eq(ProjectRequirementDO::getParentId, parentId)
                .orderByAsc(ProjectRequirementDO::getSort)
                .orderByDesc(ProjectRequirementDO::getCreateTime));
    }

    /**
     * 根据模块 ID 查询需求列表
     */
    default List<ProjectRequirementDO> selectListByModuleId(Long moduleId) {
        return selectList(new LambdaQueryWrapperX<ProjectRequirementDO>()
                .eq(ProjectRequirementDO::getModuleId, moduleId)
                .orderByAsc(ProjectRequirementDO::getSort));
    }

    /**
     * 带并发控制的状态更新
     */
    default int updateStatusByIdAndStatus(Long id, String fromStatus, String toStatus, String lastStatusReason) {
        ProjectRequirementDO update = new ProjectRequirementDO();
        update.setStatusCode(toStatus);
        update.setLastStatusReason(lastStatusReason);
        return update(update, new LambdaQueryWrapperX<ProjectRequirementDO>()
                .eq(ProjectRequirementDO::getId, id)
                .eq(ProjectRequirementDO::getStatusCode, fromStatus));
    }

    /**
     * 根据 ID 和状态删除，带并发控制
     */
    default int deleteByIdAndStatus(Long id, String statusCode) {
        return delete(new LambdaQueryWrapperX<ProjectRequirementDO>()
                .eq(ProjectRequirementDO::getId, id)
                .eq(ProjectRequirementDO::getStatusCode, statusCode));
    }

}
