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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 org.apache.ibatis.annotations.Param;

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())
                .likeIfPresent(ProjectDO::getCustomerOwner, reqVO.getCustomerOwner())
                .betweenIfPresent(ProjectDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(ProjectDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(ProjectDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ProjectDO::getEnvInfo, reqVO.getEnvInfo())
                .betweenIfPresent(ProjectDO::getCreateTime, reqVO.getCreateTime())
                .likeIfPresent(LicenseDO::getSerialNo, reqVO.getSerialNo())
                .and(reqVO.getTechnicalOwner() != null,
                        c -> c.eq(ProjectDO::getTechnicalOwnerA, reqVO.getTechnicalOwner())
                                .or().eq(ProjectDO::getTechnicalOwnerB, reqVO.getTechnicalOwner())
                                .or().eq(ProjectDO::getTechnicalOwnerC, reqVO.getTechnicalOwner()))
                .orderByDesc(ProjectDO::getCreateTime));
    }


    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())
                .eqIfPresent(ProjectDO::getEnvInfo, reqVO.getEnvInfo())
                .betweenIfPresent(ProjectDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ProjectDO::getCreateTime));
    }

    default ProjectDO selectByName(String name) {
        return selectOne(Wrappers.<ProjectDO>lambdaQuery().eq(ProjectDO::getName, name), false);
    }

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

    Integer selectMaxCode();

    Integer selectOldMaxCode();

    IPage<ProjectRespVO> queryPage(IPage<?> page, @Param("query") ProjectPageReqVO reqVO);

    List<ProjectRespVO> queryList(@Param("query") ProjectListReqVO reqVO);

    List<ProjectRespVO> getLicenseProjects(@Param("query") ProjectListReqVO reqVO);

    ProjectDO getBySnAndProjectAndCustomer(@Param("serialNo") String serialNo, @Param("projectName") String projectName, @Param("customerName") String customerName);
}