| | |
| | | STATIC_BONUS("BONUS_TYPE", "STATIC_BONUS"), |
| | | DYNAMIC_BONUS("BONUS_TYPE", "DYNAMIC_BONUS"), |
| | | INDRECT_BONUS("BONUS_TYPE", "INDRECT_BONUS"), |
| | | AGENT_BONUS("BONUS_TYPE", "AGENT_BONUS"); |
| | | AGENT_BONUS("BONUS_TYPE", "AGENT_BONUS"), |
| | | THANKFUL_BONUS("BONUS_TYPE", "THANKFUL_BONUS"), |
| | | RANK_BONUS("BONUS_TYPE", "RANK_BONUS"); |
| | | |
| | | private String type; |
| | | |
| | |
| | | /** |
| | | * 竞猜积分签到 |
| | | */ |
| | | SCORE_SIGN(14); |
| | | SCORE_SIGN(14), |
| | | |
| | | /** |
| | | * 感恩奖 |
| | | */ |
| | | THANKFUL(15); |
| | | |
| | | private final int value; |
| | | |
| | |
| | | private BigDecimal amount; |
| | | |
| | | private BigDecimal costPrice; |
| | | |
| | | private Integer isNormal; |
| | | } |
| | |
| | | IPage<MallMember> selectRankListInPage(IPage<MallMember> page, @Param("record") MallMember mallMember); |
| | | |
| | | List<MallMember> selectMemberByName(@Param("name") String name); |
| | | |
| | | // 查询有下级的用户 |
| | | List<MallMember> selectMemberListHasChild(); |
| | | |
| | | List<MallMember> selectMemberDirectForHasLevel(@Param("inviteId") String inviteId); |
| | | } |
| | |
| | | BigDecimal selectProfitByDateAndMemberId(Long memberId); |
| | | |
| | | BigDecimal selectCommissionIncome(@Param("type") Integer type, @Param("date") Date date, @Param("memberId") Long memebrid); |
| | | |
| | | BigDecimal selectThankfulCommission(@Param("date") Date date, @Param("memberId") Long memberId); |
| | | } |
| | |
| | | |
| | | void add(BigDecimal amount, Long memberId, String... field); |
| | | |
| | | void reduce(BigDecimal amount, Long memberId, String field); |
| | | int reduce(BigDecimal amount, Long memberId, String field); |
| | | |
| | | void commissionChange(CommissionChangeDto commissionChange); |
| | | } |
| | |
| | | |
| | | void addMoneyFlow(Long memberId, BigDecimal amount, Integer type, String orderNo, Integer flowType); |
| | | |
| | | void addMoneyFlow(Long memberId, BigDecimal amount, Integer type, String orderNo, Long rtMemberId, Integer flowType); |
| | | |
| | | } |
| | |
| | | * 排名分红 |
| | | */ |
| | | void rankProfit(); |
| | | |
| | | /** |
| | | * 感恩奖 |
| | | */ |
| | | void thankfulProfit(); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void reduce(BigDecimal amount, Long memberId, String field) { |
| | | public int reduce(BigDecimal amount, Long memberId, String field) { |
| | | int i = 0; |
| | | boolean flag = true; |
| | | |
| | |
| | | BigDecimal balance = (BigDecimal) declaredField.get(wallet); |
| | | |
| | | if (amount.compareTo(balance) > 0) { |
| | | throw new FebsException("余额不足"); |
| | | return 2; |
| | | } |
| | | declaredField.set(update, balance.subtract(amount)); |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | return 1; |
| | | } |
| | | |
| | | @Override |
| | |
| | | orderItem.setStyleName(sku.getStyleName()); |
| | | orderItem.setSkuName(sku.getSkuName()); |
| | | orderItem.setSkuImage(sku.getSkuImage()); |
| | | orderItem.setIsNormal(mallGoods.getIsNormal()); |
| | | orderItem.setCostPrice(sku.getCostPrice()); |
| | | |
| | | total = total.add(amount); |
| | | |
| | |
| | | public void addMoneyFlow(Long memberId, BigDecimal amount, Integer type, String orderNo, Integer flowType) { |
| | | this.addMoneyFlow(memberId, amount, type, orderNo, null, null, null, null, flowType); |
| | | } |
| | | |
| | | @Override |
| | | public void addMoneyFlow(Long memberId, BigDecimal amount, Integer type, String orderNo, Long rtMemberId, Integer flowType) { |
| | | this.addMoneyFlow(memberId, amount, type, orderNo, null, null, rtMemberId, null, flowType); |
| | | } |
| | | } |
| | |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | private final IApiMallMemberWalletService walletService; |
| | | private final IMallMoneyFlowService moneyFlowService; |
| | | private final DataDictionaryCustomMapper dataDictionaryCustomMapper; |
| | | private final MallMoneyFlowMapper mallMoneyFlowMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | } |
| | | |
| | | Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountForDate(profitDate, null); |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "D"); |
| | | if (totalIncome.compareTo(BigDecimal.ZERO) == 0) { |
| | | return; |
| | | } |
| | |
| | | for (MallGoods goods : goodsList) { |
| | | BigDecimal goodsProfit = goods.getStaticProp().multiply(perProfit); |
| | | |
| | | try { |
| | | walletService.reduce(goodsProfit, member.getId(), "score"); |
| | | } catch (Exception e) { |
| | | log.info("静态分红异常:{}, {}", goodsProfit, member.getId()); |
| | | int reduce = walletService.reduce(goodsProfit, member.getId(), "score"); |
| | | if (reduce == 2) { |
| | | break; |
| | | } |
| | | |
| | |
| | | continue; |
| | | } |
| | | |
| | | try { |
| | | walletService.reduce(direct, parentMember.getId(), "score"); |
| | | } catch (Exception e) { |
| | | log.info("隔代推荐奖异常:{}, {}", direct, parentMember.getId()); |
| | | int reduce = walletService.reduce(direct, parentMember.getId(), "score"); |
| | | if (reduce == 2) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(direct, parentMember.getId(), "commission"); |
| | | |
| | | moneyFlowService.addMoneyFlow(parentMember.getId(), direct, MoneyFlowTypeEnum.RECOMMEND_BONUS.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue()); |
| | |
| | | direct = direct.divide(indrectDicProp, 2, RoundingMode.HALF_UP); |
| | | } |
| | | |
| | | try { |
| | | walletService.reduce(dynamicProfit, member.getId(), "score"); |
| | | } catch (Exception e) { |
| | | log.info("直推奖励异常:{}, {}", dynamicProfit, member.getId()); |
| | | int reduce = walletService.reduce(dynamicProfit, member.getId(), "score"); |
| | | if (reduce == 2) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(dynamicProfit, member.getId(), "commission"); |
| | | |
| | | moneyFlowService.addMoneyFlow(member.getId(), dynamicProfit, MoneyFlowTypeEnum.DYNAMIC_ACHIEVE.getValue(), orderInfo.getOrderNo(), FlowTypeEnum.COMMISSION.getValue()); |
| | |
| | | |
| | | @Override |
| | | public void agentProfit(Date profitDate) { |
| | | log.info("#####==代理分红==#####"); |
| | | log.info("#####==代理分红==start==#####"); |
| | | if (profitDate == null) { |
| | | profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | } |
| | |
| | | BigDecimal profitProp = jsonObject.getBigDecimal("profitProp"); |
| | | BigDecimal income = preProfit.multiply(profitProp); |
| | | |
| | | try { |
| | | walletService.reduce(income, agentMember.getId(), "score"); |
| | | } catch (Exception e) { |
| | | log.info("直推奖励异常:{}, {}", income, agentMember.getId()); |
| | | int reduce = walletService.reduce(income, agentMember.getId(), "score"); |
| | | if (reduce == 2) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(income, agentMember.getId(), "commission"); |
| | | |
| | | moneyFlowService.addMoneyFlow(agentMember.getId(), income, MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(agentMember.getId(), income.negate(), MoneyFlowTypeEnum.AGENT_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue()); |
| | | } |
| | | |
| | | log.info("#####==代理分红==end==#####"); |
| | | } |
| | | |
| | | @Override |
| | | public void rankProfit() { |
| | | log.info("######==排名分红==####"); |
| | | Page<MallMember> page = new Page<>(1, 5); |
| | | MallMember query = new MallMember(); |
| | | query.setQuery("2"); |
| | | Date profitDate = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | query.setCreatedTime(profitDate); |
| | | IPage<MallMember> rankListInPage = mallMemberMapper.selectRankListInPage(page, query); |
| | | |
| | | List<MallMember> rankList = rankListInPage.getRecords(); |
| | | if (CollUtil.isEmpty(rankList)) { |
| | | return; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | System.out.println(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1)); |
| | | BigDecimal totalIncome = mallOrderInfoMapper.selectTotalAmountUnCostForDate(profitDate, null, "M"); |
| | | |
| | | List<DataDictionaryCustom> rankAward = dataDictionaryCustomMapper.selectDicByType("RANK_AWARD"); |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.RANK_BONUS.getType(), DataDictionaryEnum.RANK_BONUS.getCode()); |
| | | |
| | | |
| | | BigDecimal rankBonusTotal = new BigDecimal(dic.getValue()).divide(totalIncome, 2, RoundingMode.HALF_UP); |
| | | BigDecimal preBonus = rankBonusTotal.divide(BigDecimal.valueOf(100),2 , RoundingMode.HALF_UP); |
| | | |
| | | int i = 0; |
| | | for (MallMember member : rankList) { |
| | | DataDictionaryCustom rank = rankAward.get(i); |
| | | i++; |
| | | BigDecimal bonus = preBonus.multiply(new BigDecimal(rank.getValue())); |
| | | int reduce = walletService.reduce(bonus, member.getId(), "score"); |
| | | if (reduce == 2) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(bonus, member.getId(), "commission"); |
| | | |
| | | moneyFlowService.addMoneyFlow(member.getId(), bonus, MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(member.getId(), bonus.negate(), MoneyFlowTypeEnum.RANK_BONUS.getValue(), null, FlowTypeEnum.SCORE.getValue()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 拿推荐人收益(代理,直推,排名)的10%加权平分给下面直推 |
| | | */ |
| | | @Override |
| | | public void thankfulProfit() { |
| | | log.info("######==感恩奖==start==####"); |
| | | List<MallMember> mallMembers = mallMemberMapper.selectMemberListHasChild(); |
| | | if (CollUtil.isEmpty(mallMembers)) { |
| | | return; |
| | | } |
| | | |
| | | DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.THANKFUL_BONUS.getType(), DataDictionaryEnum.THANKFUL_BONUS.getCode()); |
| | | // 感恩奖比例 |
| | | BigDecimal prop = new BigDecimal(dic.getValue()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP); |
| | | |
| | | Date date = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -1); |
| | | for (MallMember mallMember : mallMembers) { |
| | | BigDecimal income = mallMoneyFlowMapper.selectThankfulCommission(date, mallMember.getId()); |
| | | List<MallMember> children = mallMemberMapper.selectMemberDirectForHasLevel(mallMember.getInviteId()); |
| | | if (CollUtil.isEmpty(children)) { |
| | | continue; |
| | | } |
| | | |
| | | BigDecimal preProfit = income.multiply(prop).divide(BigDecimal.valueOf(children.size()), 2, RoundingMode.HALF_DOWN); |
| | | |
| | | |
| | | int reduce = walletService.reduce(preProfit.negate(), mallMember.getId(), "commission"); |
| | | if (reduce == 2) { |
| | | continue; |
| | | } |
| | | |
| | | moneyFlowService.addMoneyFlow(mallMember.getId(), income.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, FlowTypeEnum.COMMISSION.getValue()); |
| | | |
| | | for (MallMember child : children) { |
| | | int reduce1 = walletService.reduce(preProfit.negate(), child.getId(), "score"); |
| | | if (reduce1 == 2) { |
| | | continue; |
| | | } |
| | | |
| | | walletService.add(preProfit, child.getId(), "commission"); |
| | | moneyFlowService.addMoneyFlow(child.getId(), preProfit, MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.COMMISSION.getValue()); |
| | | moneyFlowService.addMoneyFlow(child.getId(), preProfit.negate(), MoneyFlowTypeEnum.THANKFUL.getValue(), null, mallMember.getId(), FlowTypeEnum.SCORE.getValue()); |
| | | } |
| | | } |
| | | log.info("######==感恩奖==end==####"); |
| | | } |
| | | } |
| | |
| | | DATE_FORMAT(a.CREATED_TIME, '%Y-%m-%d') DESC |
| | | </select> |
| | | |
| | | <select id="selectRankListInPage" resultType="cc.mrbird.febs.mall.entity.MallMember"> |
| | | select * from ( |
| | | select a.id, a.name, a.invite_id, a.avatar, sum(b.amount) amount,max(order_time) orderTime from mall_member a, mall_order_info b |
| | | where a.id=b.member_id and b.status = 4 |
| | | <!-- 日 --> |
| | | <if test="record.query == '1'"> |
| | | <!-- <select id="selectRankListInPage" resultType="cc.mrbird.febs.mall.entity.MallMember">--> |
| | | <!-- select * from (--> |
| | | <!-- select a.id, a.name, a.invite_id, a.avatar, sum(b.amount) amount,max(order_time) orderTime from mall_member a, mall_order_info b--> |
| | | <!-- where a.id=b.member_id and b.status = 4--> |
| | | <!-- <!– 日 –>--> |
| | | <!-- <if test="record.query == '1'">--> |
| | | |
| | | </if> |
| | | <!-- </if>--> |
| | | <!-- <!– 月 –>--> |
| | | <!-- <if test="record.query == '2'">--> |
| | | <!-- and date_format(#{record.createdTime},'%Y-%m') = date_format(b.order_time,'%Y-%m')--> |
| | | <!-- </if>--> |
| | | <!-- <if test="record.amount != null">--> |
| | | <!-- and amount = #{record.amount}--> |
| | | <!-- </if>--> |
| | | <!-- group by a.id--> |
| | | <!-- ) a order by amount desc, a.orderTime desc--> |
| | | <!-- </select>--> |
| | | <select id="selectRankListInPage" resultType="cc.mrbird.febs.mall.entity.MallMember"> |
| | | select a.*, b.* |
| | | from mall_member a |
| | | inner join ( |
| | | select b.referrer_id refererId, sum(a.amount) amount, max(a.order_time) orderTime |
| | | from mall_order_info a |
| | | inner join mall_member b on a.member_id=b.ID |
| | | inner join mall_order_item c on a.id = c.order_id and c.is_normal=2 |
| | | where a.status in (2, 3, 4) and a.order_type=1 |
| | | <!-- 月 --> |
| | | <if test="record.query == '2'"> |
| | | and date_format(#{record.createdTime},'%Y-%m') = date_format(b.order_time,'%Y-%m') |
| | | </if> |
| | | <if test="record.amount != null"> |
| | | and amount = #{record.amount} |
| | | </if> |
| | | group by a.id |
| | | ) a order by amount desc, a.orderTime desc |
| | | group by b.referrer_id |
| | | ) b on a.invite_id=b.refererId |
| | | order by b.amount desc, b.orderTime desc |
| | | </select> |
| | | |
| | | <select id="selectMemberByName" resultType="cc.mrbird.febs.mall.entity.MallMember"> |
| | | select * from mall_member |
| | | where name=#{name} |
| | | </select> |
| | | |
| | | <select id="selectMemberListHasChild" resultType="cc.mrbird.febs.mall.entity.MallMember"> |
| | | select distinct a.* from mall_member a, mall_member b |
| | | where a.invite_id=b.referrer_id |
| | | </select> |
| | | |
| | | <select id="selectMemberDirectForHasLevel" resultType="cc.mrbird.febs.mall.entity.MallMember"> |
| | | select * from mall_member |
| | | where referrer_id=#{inviteId} and level != 'ZERO_LEVEL' |
| | | </select> |
| | | </mapper> |
| | |
| | | and date_format(created_time, '%Y-%m') = date_format(#{date}, '%Y-%m') |
| | | </if> |
| | | </select> |
| | | |
| | | <select id="selectThankfulCommission" resultType="java.math.BigDecimal"> |
| | | select ifnull(sum(amount),0) from mall_money_flow |
| | | where flow_type=4 and amount > 0 |
| | | and member_id=#{memberId} |
| | | and date_format(created_time, '%Y-%m-%d') = date_format(#{date}, '%Y-%m-%d') |
| | | and type in (2,3,4) |
| | | </select> |
| | | </mapper> |
| | |
| | | select IFNULL(sum(IFNULL(b.amount,0) - IFNULL(b.cost_price*b.cnt, 0)),0) |
| | | from mall_order_info a |
| | | inner join mall_order_item b on a.id=b.order_id |
| | | where status = 4 |
| | | where a.status in (2, 3, 4) and a.is_normal = 2 and a.order_type=1 |
| | | <if test="member != null"> |
| | | and member_id=#{memberId} |
| | | </if> |