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

import cn.hutool.core.collection.CollUtil;
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.api.oa.vo.receipt.ReceiptSettlementDTO;
import cn.iocoder.yudao.module.bpm.api.oa.vo.receipt.ReceiptSettlementVO;
import cn.iocoder.yudao.module.bpm.api.oa.vo.receipt.ReceiptStatisticsDTO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.receipt.BpmOAReceiptPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.receipt.BpmOAReceiptRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.receipt.ReceiptStatisticsVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAContractDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAReceiptDO;
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
public interface BpmOAReceiptMapper extends BaseMapperX<BpmOAReceiptDO> {
    default PageResult<BpmOAReceiptRespVO> selectReceiptPage(BpmOAReceiptPageReqVO pageReqVO, List<Long> userIds) {

        MPJLambdaWrapperX<BpmOAReceiptDO> query = new MPJLambdaWrapperX<>();
        query.selectAll(BpmOAReceiptDO.class);
        query.selectAs(BpmOAContractDO::getContractName, BpmOAReceiptRespVO::getContractName);
        query.selectAs(BpmOAContractDO::getCustomerName, BpmOAReceiptRespVO::getCustomerName);
        query.selectAs(BpmOAContractDO::getContractNo, BpmOAReceiptRespVO::getContractNo);
        query.selectAs("u.nickname", BpmOAReceiptRespVO::getCreateName);
        query.leftJoin(BpmOAContractDO.class, "c", BpmOAContractDO::getId, BpmOAReceiptDO::getContractId);
        query.leftJoin("system_users u on u.id = t.creator");
        query.eqIfPresent(BpmOAReceiptDO::getResult, pageReqVO.getResult());
        query.in(CollUtil.isNotEmpty(userIds), BpmOAContractDO::getUserId, userIds);
        query.apply(Objects.nonNull(pageReqVO.getContractId()), "c.id = {0}", pageReqVO.getContractId());
        query.apply(Objects.nonNull(pageReqVO.getCustomerName()), "c.customer_name LIKE CONCAT('%', {0}, '%')", pageReqVO.getCustomerName());
        query.apply(Objects.nonNull(pageReqVO.getContractName()), "c.contract_name LIKE CONCAT('%', {0}, '%')", pageReqVO.getContractName());
        query.apply(Objects.nonNull(pageReqVO.getCreateName()),"u.nickname Like CONCAT('%', {0}, '%')", pageReqVO.getCreateName());
        query.orderByDesc(BpmOAReceiptDO::getCreateTime);

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

    ReceiptStatisticsVO selectStatistics(@Param("userId") Long userId,
                                         @Param("relation") String relation);

    List<ReceiptStatisticsDTO> selectReceiptStatisticsListByUserIds(@Param("userIds") List<Long> userIds,
                                                                    @Param("createTime") LocalDateTime[] createTime);

    /**
     * 获取用户结算回款信息
     *
     * @return
     */
    List<ReceiptSettlementDTO> getReceiptSettlement(@Param("vo") ReceiptSettlementVO vo);
}
