package com.ruoyi.frequency.sale.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.controller.req.SaleDetailPageReq;
import com.ruoyi.controller.resp.RankingResp;
import com.ruoyi.controller.resp.SaleDataResp;
import com.ruoyi.controller.resp.SaleDetailPageResp;
import com.ruoyi.frequency.sale.entity.Sale;
import com.ruoyi.frequency.sale.model.result.SaleCount;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

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

/**
 * 销售账号Mapper接口
 *
 * @author zj
 * @date 2023-09-22
 */
public interface SaleMapper  extends BaseMapper<Sale>
{
    /**
     * 查询销售账号
     *
     * @param id 销售账号主键
     * @return 销售账号
     */
    public Sale selectSaleById(Long id);

    /**
     * 查询销售账号列表
     *
     * @param sale 销售账号
     * @return 销售账号集合
     */
    public List<Sale> selectSaleList(Sale sale);

    /**
     * 新增销售账号
     *
     * @param sale 销售账号
     * @return 结果
     */
    public int insertSale(Sale sale);

    /**
     * 修改销售账号
     *
     * @param sale 销售账号
     * @return 结果
     */
    public int updateSale(Sale sale);

    /**
     * 删除销售账号
     *
     * @param id 销售账号主键
     * @return 结果
     */
    public int deleteSaleById(Long id);

    /**
     * 批量删除销售账号
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteSaleByIds(Long[] ids);

    @Update("update  t_sale  set balance=balance + #{price} where id = #{saleId}")
    boolean addBalance(@Param("saleId") Long saleId, @Param("price") BigDecimal price);

    @Update("update  t_sale  set balance=balance - #{price} where id = #{saleId} and balance >=#{price}")
    boolean subBalance(@Param("saleId") Long saleId, @Param("price") BigDecimal price);

    @Update("update  t_sale  set income=income + #{income} where id = #{saleId}")
    boolean addIncome(@Param("saleId") Long saleId, @Param("income") BigDecimal income);

    @Update("update  t_sale  set income=income - #{income} where id = #{saleId} and income >=#{income}")
    boolean subIncome(@Param("saleId") Long saleId, @Param("income") BigDecimal income);


    Integer commissionRanking(@Param("userId") Long userId);

    Integer saleRanking(@Param("userId") Long userId);

    Page<RankingResp> commissionRankingPage(@Param("page") Page page,@Param("userId")Long userId);

    Page<RankingResp> saleRankingPage(@Param("page") Page page,@Param("userId")Long userId);

    Page<SaleDetailPageResp> saleDetailPage(@Param("page") Page<Object> objectPage, @Param("req") SaleDetailPageReq req,@Param("userId") Long userId);

    SaleDataResp saleOrderAmount( @Param("userId") Long userId);

    SaleCount statistics(@Param("list") List<Long> saleIdList);
}
