package cn.iocoder.yudao.module.crm.dal.mysql.crmcontract;


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.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.crm.controller.admin.crmanalysis.vo.ContractVO;
import cn.iocoder.yudao.module.crm.controller.admin.crmanalysis.vo.TimeQueryVO;
import cn.iocoder.yudao.module.crm.controller.admin.crmcontract.vo.CrmContractPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.crmcontract.vo.CrmContractRespVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.crmcontract.CrmContractDO;
import cn.iocoder.yudao.module.hrm.api.crmcontract.dto.CrmContractProductSettlementDTO;
import cn.iocoder.yudao.module.product.api.storeproductattrvalue.vo.UserProductCountVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

/**
 * 合同 Mapper
 *
 * @author yshop
 */
@Mapper
public interface CrmContractMapper extends BaseMapperX<CrmContractDO> {

    IPage<CrmContractRespVO> selectPageList(@Param("page") IPage page, @Param("dto") CrmContractPageReqVO pageReqVO, @Param("ids") List<Long> ids);

    default PageResult<CrmContractDO> selectPage(CrmContractPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CrmContractDO>()
                .eqIfPresent(CrmContractDO::getBusinessId, reqVO.getBusinessId())
                .eqIfPresent(CrmContractDO::getContactsId, reqVO.getContactsId())
                .eqIfPresent(CrmContractDO::getSourceId, reqVO.getSourceId())
                .likeIfPresent(CrmContractDO::getName, reqVO.getName())
                .eqIfPresent(CrmContractDO::getNumber, reqVO.getNumber())
                .betweenIfPresent(CrmContractDO::getOrderTime, reqVO.getOrderTime())
                .eqIfPresent(CrmContractDO::getMoney, reqVO.getMoney())
                .eqIfPresent(CrmContractDO::getTotalPrice, reqVO.getTotalPrice())
                .eqIfPresent(CrmContractDO::getReturnMoney, reqVO.getReturnMoney())
                .eqIfPresent(CrmContractDO::getDiscountRate, reqVO.getDiscountRate())
                .eqIfPresent(CrmContractDO::getCheckStatus, reqVO.getCheckStatus())
                .eqIfPresent(CrmContractDO::getFlowId, reqVO.getFlowId())
                .eqIfPresent(CrmContractDO::getStepId, reqVO.getStepId())
                .eqIfPresent(CrmContractDO::getCheckAdminId, reqVO.getCheckAdminId())
                .eqIfPresent(CrmContractDO::getFlowAdminId, reqVO.getFlowAdminId())
                .betweenIfPresent(CrmContractDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(CrmContractDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(CrmContractDO::getRemark, reqVO.getRemark())
                .eqIfPresent(CrmContractDO::getOwnerUserId, reqVO.getOwnerUserId())
                .betweenIfPresent(CrmContractDO::getNextTime, reqVO.getNextTime())
                .eqIfPresent(CrmContractDO::getExpireHandle, reqVO.getExpireHandle())
                .eqIfPresent(CrmContractDO::getInvoiceMoney, reqVO.getInvoiceMoney())
                .betweenIfPresent(CrmContractDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CrmContractDO::getId));
    }

    List<ContractVO> selectContractTop(@Param("vo") TimeQueryVO vo);

    /**
     * 获取用户合同产品数量
     *
     * @param userIds
     * @param createTime
     * @return
     */
    List<UserProductCountVO> getProductCount(@Param("userIds") List<Long> userIds, @Param("createTime") LocalDateTime[] createTime);

    /**
     * 获取合同结算信息
     *
     * @return
     */
    List<CrmContractProductSettlementDTO> getContractProductSettlement();
}
