package org.agt.module.license.dal.mysql.project;


import cn.hutool.core.util.StrUtil;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.mybatis.core.mapper.BaseMapperX;
import org.agt.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.agt.framework.mybatis.core.query.MPJLambdaWrapperX;
import org.agt.module.license.controller.admin.project.vo.ProjectListReqVO;
import org.agt.module.license.controller.admin.project.vo.ProjectPageReqVO;
import org.agt.module.license.controller.admin.project.vo.ProjectRespVO;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.module.license.dal.dataobject.project.ProjectDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 项目 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface ProjectMapper extends BaseMapperX<ProjectDO> {

    default PageResult<ProjectRespVO> selectPage(ProjectPageReqVO reqVO) {
        return selectJoinPage(reqVO, ProjectRespVO.class, new MPJLambdaWrapperX<ProjectDO>()
                .selectAll(ProjectDO.class)
                .selectAs(LicenseDO::getSerialNo, ProjectRespVO::getSerialNo)
                .leftJoin(LicenseDO.class, LicenseDO::getProjectId, ProjectDO::getId)
                .eqIfPresent(ProjectDO::getCustomerId, reqVO.getCustomerId())
                .likeIfPresent(ProjectDO::getName, reqVO.getName())
                .likeIfPresent(ProjectDO::getCode, reqVO.getCode())
                .likeIfPresent(ProjectDO::getContractCode, reqVO.getContractCode())
                .eqIfPresent(ProjectDO::getBusinessStatus, reqVO.getBusinessStatus())
                .eqIfPresent(ProjectDO::getBusinessOwner, reqVO.getBusinessOwner())
                .eqIfPresent(ProjectDO::getCustomerOwner, reqVO.getCustomerOwner())
                .betweenIfPresent(ProjectDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(ProjectDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(ProjectDO::getStatus, reqVO.getStatus())
                .likeIfPresent(ProjectDO::getEnvInfo, reqVO.getEnvInfo())
                .betweenIfPresent(ProjectDO::getCreateTime, reqVO.getCreateTime())
                .likeIfPresent(LicenseDO::getSerialNo, reqVO.getSerialNo())
                .and(StrUtil.isNotBlank(reqVO.getTechnicalOwner()),
                        c -> c.like(ProjectDO::getTechnicalOwnerA, reqVO.getTechnicalOwner())
                                .or().like(ProjectDO::getTechnicalOwnerB, reqVO.getTechnicalOwner())
                                .or().like(ProjectDO::getTechnicalOwnerC, reqVO.getTechnicalOwner()))
                .orderByDesc(ProjectDO::getId));
    }


    default List<ProjectDO> selectList(ProjectListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ProjectDO>()
                .eqIfPresent(ProjectDO::getCustomerId, reqVO.getCustomerId())
                .likeIfPresent(ProjectDO::getName, reqVO.getName())
                .likeIfPresent(ProjectDO::getCode, reqVO.getCode())
                .likeIfPresent(ProjectDO::getContractCode, reqVO.getContractCode())
                .eqIfPresent(ProjectDO::getBusinessStatus, reqVO.getBusinessStatus())
                .eqIfPresent(ProjectDO::getBusinessOwner, reqVO.getBusinessOwner())
                .eqIfPresent(ProjectDO::getCustomerOwner, reqVO.getCustomerOwner())
                .eqIfPresent(ProjectDO::getTechnicalOwnerA, reqVO.getTechnicalOwnerA())
                .eqIfPresent(ProjectDO::getTechnicalOwnerB, reqVO.getTechnicalOwnerB())
                .eqIfPresent(ProjectDO::getTechnicalOwnerC, reqVO.getTechnicalOwnerC())
                .betweenIfPresent(ProjectDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(ProjectDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(ProjectDO::getStatus, reqVO.getStatus())
                .likeIfPresent(ProjectDO::getEnvInfo, reqVO.getEnvInfo())
                .betweenIfPresent(ProjectDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ProjectDO::getId));
    }

    default ProjectDO selectByName(String name) {
        return selectOne(ProjectDO::getName, name);
    }

    default ProjectDO selectByCode(String code) {
        return selectOne(ProjectDO::getCode, code);
    }

    Integer selectMaxCode();
}