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

import com.njcn.rdms.framework.mybatis.core.dataobject.BaseDO;
import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.project.dal.dataobject.project.ProjectDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Mapper
public interface ProjectMapper extends BaseMapperX<ProjectDO> {

    default ProjectDO selectByCode(String projectCode) {
        return selectOne(ProjectDO::getProjectCode, projectCode);
    }

    default ProjectDO selectActiveByProductIdAndName(Long productId, String projectName, String excludedStatusCode) {
        LambdaQueryWrapperX<ProjectDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(ProjectDO::getProjectName, projectName)
                .ne(ProjectDO::getStatusCode, excludedStatusCode);
        if (productId == null) {
            queryWrapper.isNull(ProjectDO::getProductId);
        } else {
            queryWrapper.eq(ProjectDO::getProductId, productId);
        }
        return selectOne(queryWrapper);
    }

    default List<ProjectDO> selectActiveMainlineListByProductId(Long productId, Collection<String> projectTypes,
                                                                String excludedStatusCode) {
        return selectList(new LambdaQueryWrapperX<ProjectDO>()
                .eq(ProjectDO::getProductId, productId)
                .in(ProjectDO::getProjectType, projectTypes)
                .ne(ProjectDO::getStatusCode, excludedStatusCode));
    }

    @Select("""
            SELECT status_code AS statusCode, COUNT(*) AS countValue
            FROM rdms_project
            WHERE deleted = b'0'
            GROUP BY status_code
            """)
    List<Map<String, Object>> selectStatusCountList();

    default List<ProjectDO> selectListByCodePrefix(String codePrefix) {
        return selectList(new LambdaQueryWrapperX<ProjectDO>()
                .likeRight(ProjectDO::getProjectCode, codePrefix)
                .orderByDesc(ProjectDO::getProjectCode));
    }

    default List<ProjectDO> selectListByProductId(Long productId) {
        return selectList(new LambdaQueryWrapperX<ProjectDO>()
                .eq(ProjectDO::getProductId, productId)
                .orderByDesc(BaseDO::getCreateTime));
    }

    default int updateStatusByIdAndStatus(Long id, String fromStatus, String toStatus, String lastStatusReason) {
        ProjectDO update = new ProjectDO();
        update.setStatusCode(toStatus);
        update.setLastStatusReason(lastStatusReason);
        return update(update, new LambdaQueryWrapperX<ProjectDO>()
                .eq(ProjectDO::getId, id)
                .eq(ProjectDO::getStatusCode, fromStatus));
    }

    default int deleteByIdAndStatus(Long id, String statusCode) {
        return delete(new LambdaQueryWrapperX<ProjectDO>()
                .eq(ProjectDO::getId, id)
                .eq(ProjectDO::getStatusCode, statusCode));
    }

}
