package cn.iocoder.yudao.module.bpm.dal.mysql.oa;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.contract.BpmOAContractPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.contract.BpmOAContractRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.contract.ContractStatisticsRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAContractDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

/**
 * 合同审批 Mapper
 *
 * @author 符溶馨
 */
@Mapper
public interface BpmOAContractMapper extends BaseMapperX<BpmOAContractDO> {
    default PageResult<BpmOAContractRespVO> selectPage(BpmOAContractPageReqVO pageReqVO,
                                                       List<Long> userIds) {

        MPJLambdaWrapperX<BpmOAContractDO> query = new MPJLambdaWrapperX<>();
        query.selectAll(BpmOAContractDO.class);
        query.selectAs("u.nickname", BpmOAContractRespVO::getSignatoryName);
        query.selectAs("u1.nickname", BpmOAContractRespVO::getCreateName);
        query.selectAs("d.name", BpmOAContractRespVO::getCompanyName);
        query.leftJoin("system_users u on u.id = t.signatory_id");
        query.leftJoin("system_users u1 on u1.id = t.creator");
        query.leftJoin("system_dept d on d.id = t.company_id");
        query.eqIfPresent(BpmOAContractDO::getContractType, pageReqVO.getContractType());
        query.likeIfPresent(BpmOAContractDO::getContractName, pageReqVO.getContractName());
        query.likeIfPresent(BpmOAContractDO::getContractNo, pageReqVO.getContractNo());
        query.likeIfPresent(BpmOAContractDO::getCustomerName, pageReqVO.getCustomerName());
        query.eqIfPresent(BpmOAContractDO::getStatus, pageReqVO.getStatus());
        query.eqIfPresent(BpmOAContractDO::getResult, pageReqVO.getResult());
        query.inIfPresent(BpmOAContractDO::getUserId, userIds);
        query.apply(Objects.nonNull(pageReqVO.getSignatoryName()),"u.nickname Like CONCAT('%', {0}, '%')", pageReqVO.getSignatoryName());
        query.apply(Objects.nonNull(pageReqVO.getCreateName()),"u1.nickname Like CONCAT('%', {0}, '%')", pageReqVO.getCreateName());

        query.orderByDesc(BpmOAContractDO::getCreateTime);

        return selectJoinPage(pageReqVO, BpmOAContractRespVO.class, query);
    }

    ContractStatisticsRespVO selectContractStatistics(@Param("userId") Long userId,
                                                      @Param("relation") String relation);

    List<ContractStatisticsRespVO> selectContractStatisticsListByUserIds(@Param("userIds") List<Long> userIds,
                                                                         @Param("createTime") LocalDateTime[] createTime);
}
