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

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.license.vo.LicensePageReqVO;
import org.agt.module.license.controller.admin.license.vo.LicenseRespVO;
import org.agt.module.license.dal.dataobject.license.LicenseDO;
import org.agt.module.license.dal.dataobject.project.ProjectDO;
import org.agt.module.license.enums.LicenseStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

/**
 * License Mapper
 *
 * @author 管理员
 */
@Mapper
public interface LicenseMapper extends BaseMapperX<LicenseDO> {

    default PageResult<LicenseDO> selectPage(LicensePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<LicenseDO>()
                .eqIfPresent(LicenseDO::getProjectId, reqVO.getProjectId())
                .likeIfPresent(LicenseDO::getSerialNo, reqVO.getSerialNo())
                .betweenIfPresent(LicenseDO::getExpiryDate, reqVO.getExpiryDate())
                .eqIfPresent(LicenseDO::getUserNumber, reqVO.getUserNumber())
                .eqIfPresent(LicenseDO::getRanNumber, reqVO.getRanNumber())
                .eqIfPresent(LicenseDO::getApplicant, reqVO.getApplicant())
                .eqIfPresent(LicenseDO::getApprover, reqVO.getApprover())
                .eqIfPresent(LicenseDO::getStatus, reqVO.getStatus())
                .eqIfPresent(LicenseDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(LicenseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(LicenseDO::getApplicationTime));
    }

    default List<LicenseRespVO> selectListByExpiryDate() {

        return selectJoinList(LicenseRespVO.class, new MPJLambdaWrapperX<LicenseDO>()
                .selectAll(LicenseDO.class)
                .leftJoin(ProjectDO.class, ProjectDO::getId, LicenseDO::getProjectId)
                .selectAs(ProjectDO::getCustomerId, LicenseRespVO::getCustomerId)
                .gt(LicenseDO::getExpiryDate, LocalDateTime.now())
                .le(LicenseDO::getExpiryDate, LocalDateTime.now().plusDays(7))
                .eq(LicenseDO::getStatus, LicenseStatusEnum.COMPLETED.getCode())
                .orderByAsc(LicenseDO::getExpiryDate));
    }

    default LicenseDO selectBySn(String sn) {
        return selectOne(Wrappers.<LicenseDO>lambdaQuery().eq(LicenseDO::getSerialNo, sn), false);
    }

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

    IPage<LicenseRespVO> queryPage(IPage<?> page, @Param("query") LicensePageReqVO reqVO);
}