package cn.newfeifan.mall.module.distri.dal.mysql.ptprofit;

import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.mybatis.core.mapper.BaseMapperX;
import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.module.distri.controller.admin.ptprofit.vo.PtProfitPageReqVO;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofit.PtProfitDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;

/**
 * 平台利润 Mapper
 *
 * @author 非繁人
 */
@Mapper
public interface PtProfitMapper extends BaseMapperX<PtProfitDO> {

    default PageResult<PtProfitDO> selectPage(PtProfitPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PtProfitDO>()
                .eqIfPresent(PtProfitDO::getPtAdd, reqVO.getPtAdd())
                .eqIfPresent(PtProfitDO::getPtGrossAdd, reqVO.getPtGrossAdd())
                .eqIfPresent(PtProfitDO::getPtTotalAdd, reqVO.getPtTotalAdd())
                .betweenIfPresent(PtProfitDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PtProfitDO::getId));
    }

    @Update("UPDATE distri_integral SET current_quota = current_quota + #{amount},freeze_quota = freeze_quota + #{freeze_mount} WHERE (current_quota + #{amount2})>=0 and user_id=#{userId} and (freeze_quota + #{freeze_mount2})>=0 ")
    int updateUserIntegral(@Param("amount") Long amount,
                           @Param("amount2") Long amount2,
                           @Param("freeze_mount") Long freezeAmount,
                           @Param("freeze_mount2") Long freezeAmount2,
                           @Param("userId") Long userId);

    @Select("SELECT current_quota,freeze_quota FROM distri_integral WHERE user_id = #{userId}")
    Map<String, Object> selectUserIntegral(@Param("userId") Long userId);
}