package cn.pluss.platform.mapper;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.dto.MerchantFeeDTO;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.entity.MerchantProfit;
import cn.pluss.platform.vo.DeviceGoodsVO;
import cn.pluss.platform.vo.MerChannelStatusVO;
import cn.pluss.platform.vo.MerchantProfitVO;
import cn.pluss.platform.vo.ProfitOrderVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface MerchantProfitMapper extends BaseMapper<MerchantProfit> {

    List<MerchantProfit> selectGroupMercProfit(@Param("userIdList") List<Long> userIdList);

    @Select("SELECT SUM(`price`) FROM tb_pluss_merchant_profit WHERE `status` = '5' AND userId = #{userId}")
    BigDecimal selectSumValidProfit(@Param("userId") String userId);

    MerchantProfit queryMerchantProfit(MerchantProfit merchantProfit);

    List<MerchantProfit> queryMerchantProfitList(MerchantProfit merchantProfit);

    List<MerchantProfit> queryMerchantProfitPage(Map map);

    Integer queryMerchantProfitPageCount(Map map);
    Integer queryMerchantProfitPageCountV2(@Param("userId") String userId, @Param("startTime") Date startTime,@Param("endTime") Date endTime);

    void saveMerchantProfitBatch(List<MerchantProfit> merchantProfitList);
    
    Double queryMerchantProfitSumConsumeFee(Map map);
    
    Double queryMerchantProfitSumPrice(Map map);
    
    Integer queryMerchantProfitPageCountGroupByOrderNumber(Map map);

    /**
     * 查询流水以及分润
     * @param userId            上级id, 也可能是上上级
     * @param merchantCodeList  店铺code列表
     * @param startTime         开始时间
     * @param endTime           截止时间
     * @return
     */
    List<MerchantFeeDTO> queryMerchantBillWithProfit(@Param("userId") String userId, @Param("merchantCodeList") List<String> merchantCodeList, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    void updateMerchantProfit(MerchantProfit p);

    /**
     * 批量更新 指定字段
     * @param listp
     */
    void updateBatch(List<MerchantProfit> listp);


    Map<String, Object> getProfitCount(Map<String, Object> map);

    /**
     * 通过分润数据查询对应的费率信息
     * @param merchantProfitList
     * @return
     */
    List<Map<String, Object>> queryMerchantProfitRate(@Param("merchantProfitList") List<MerchantProfit> merchantProfitList, @Param("userId") String userId);


    Map<String, BigDecimal> getProfitAmtCount(MerchantProfit condition);

    /**
     * 今日收益
     * @param userId
     * @return
     */
    @Select("SELECT\n" +
            "\tsum( t.price ) \n" +
            "FROM\n" +
            "\ttb_pluss_merchant_profit t \n" +
            "WHERE\n" +
            "\tt.type = 5 \n" +
            "\tAND t.userId = #{userId} \n" +
            "\tAND date_format( `t`.`createDt`, '%Y-%m-%d' ) = date_format(( curdate() - INTERVAL 0 DAY ), '%Y-%m-%d' );")
    BigDecimal getTodaySum(@Param("userId")String userId);

    /**
     * 直属商户交易金额
     * @return
     */
    @Select("SELECT\n" +
            "\tsum( o.consumeFee ) \n" +
            "FROM\n" +
            "\ttb_pluss_merchant_order o\n" +
            "\tLEFT JOIN tb_pluss_merchant_base_info b ON o.merchantCode = b.merchantCode\n" +
            "\tLEFT JOIN tb_pluss_user_promotion p ON b.userId = p.user_id \n" +
            "WHERE\n" +
            "\to.`status` = 1 \n" +
            "\tAND p.parent_user_id = #{userId}")
    BigDecimal getConsumeFee(@Param("userId") String userId);

    /**
     * 团队总交易金额
     * @param userId
     * @return
     */
    BigDecimal getConsumeFeeTeam(@Param("userId") String userId);

    /**
     * 团队总交易金额（今日）
     * @param userId
     * @return
     */
    BigDecimal getConsumeFeeTeamToday(@Param("userId") String userId);

    List<MerchantProfitVO> getTeamList(@Param("typeCode") String typeCode, @Param("userId") Long userId,
                                       @Param("userName") String userName, @Param("pageSize") Integer pageSize,
                                       @Param("offset") Integer offset);
    List<MerchantProfitVO> getMerchantTeamList(@Param("typeCode") String typeCode, @Param("userId") Long userId,
                                       @Param("userName") String userName, @Param("pageSize") Integer pageSize,
                                       @Param("offset") Integer offset);


    @Select("SELECT\n" +
            "\tcount(*) \n" +
            "FROM\n" +
            "\ttb_pluss_user_promotion \n" +
            "WHERE\n" +
            "\tparent_user_id = #{userId}\n" +
            "\tAND type_code = #{typeCode}")
    Integer getCountChild( @Param("userId") Long userId,@Param("typeCode") String typeCod);
    @Select("SELECT\n" +
            "\tcount(*) \n" +
            "FROM\n" +
            "\ttb_pluss_user_promotion \n" +
            "WHERE\n" +
            "\tparent_user_id = #{userId}\n" +
            "\tAND type_code = #{typeCode}"+
            "\tAND is_extend = '0'")
    Integer getCountQuotaChild( @Param("userId") Long userId,@Param("typeCode") String typeCod);
    Integer getCountChildV2( @Param("userId") Long userId);

    List<MerChannelStatusVO> getChannelStatus(@Param("merchantCode") String merchantCode);

    /**
     * 直属交易
     * @param userId
     * @param pageSize
     * @param offset
     * @return
     */
    List<ProfitOrderVO> directlyOrder(@Param("userId") Long userId,@Param("pageSize") Integer pageSize,@Param("offset") Integer offset,
                                      @Param("startTime") String startTime, @Param("endTime") String endTime);
    /**
     * 团队交易
     */
    List<ProfitOrderVO> teamOrder(@Param("userId") Long userId,@Param("pageSize") Integer pageSize,@Param("offset") Integer offset,
                                  @Param("startTime") String startTime, @Param("endTime") String endTime);
}

