GiveOutCommission.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\common\service\commission;
  4. use app\api\model\user\CommissionsDetail;
  5. use app\common\model\Order;
  6. use app\common\model\OrderGoods;
  7. use app\common\model\OrderRefund;
  8. use app\common\service\BaseService;
  9. use app\common\service\Message;
  10. use app\console\model\user\WithdrawMoneyLog;
  11. use Exception;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. /**
  16. * 确认收货后分佣下发
  17. * Class Refund
  18. * @package app\common\service\order
  19. */
  20. class GiveOutCommission extends BaseService
  21. {
  22. /**
  23. * 确认收货后分佣派发
  24. * @param $orderId int 订单id
  25. * @return bool
  26. */
  27. public function testConfirmOrderCommission(int $orderId): bool
  28. {
  29. $commissionDetails = CommissionsDetail::alias('cm')
  30. ->leftJoin('order og','cm.order_id=og.order_id')
  31. ->where('cm.order_id',$orderId)
  32. ->where('cm.clearing_status',0)
  33. ->where('og.commission_settlement_time','>',0)
  34. ->where('og.commission_settlement_time','<',time())
  35. ->where('og.commission_settlement_status',0)
  36. ->field('cm.id,cm.order_id,cm.user_id,cm.clearing_money,cm.commission_percent,og.order_no')
  37. ->select();
  38. if ($commissionDetails->isEmpty()){
  39. return true;
  40. }
  41. $data = [];
  42. $infos = [];
  43. try {
  44. foreach ($commissionDetails as $detail){
  45. if(self::endCommissionDetail($detail,$data) === false){
  46. $infos[] = '分佣失败commissionId::'.$detail->id;
  47. }
  48. }
  49. if (count($data)){
  50. Order::whereIn('order_id',$data)->update(['commission_settlement_status'=>1]);
  51. }
  52. }catch (Exception $e){
  53. $msg = __METHOD__.",orderId::".$orderId.'结算分佣失败'.$e->getMessage();
  54. log_record($msg,'error');
  55. Message::wxRobot($msg);
  56. return false;
  57. }
  58. if (count($infos)){
  59. $infoStr = json_encode($infos);
  60. log_record('info::'.$infoStr,'error');
  61. Message::wxRobot($infoStr);
  62. }
  63. return true;
  64. }
  65. /**
  66. * 处理单条待结算分佣记录
  67. * @param $detail
  68. * @param $data
  69. * @return bool
  70. * @throws DataNotFoundException
  71. * @throws DbException
  72. * @throws ModelNotFoundException
  73. */
  74. public static function endCommissionDetail($detail,&$data): bool
  75. {
  76. $orderGoods = OrderGoods::field('order_goods_id,total_num,total_pay_price,rice_card_money,total_pay_price,frozen_status')
  77. ->where('order_id',$detail->order_id)->select();
  78. $frozenS = $orderGoods->column('frozen_status');
  79. $s = array_search(1,$frozenS);//查找是否有冻结中的订单商品
  80. if (!($s === false)){
  81. log_record(__METHOD__.':orderGoods frozen orderId:'.$detail->order_id,'error');
  82. return false;
  83. }
  84. $saleVolume = '0';
  85. $refundCnt = 0;
  86. foreach ($orderGoods as $og){
  87. $rate = 1;
  88. $refund = OrderRefund::where('order_goods_id',$og->order_goods_id)
  89. ->where('finance_refund',10)->find();
  90. $ogSale = bcadd($og['total_pay_price'],$og['rice_card_money'],4);
  91. if ($refund){
  92. $refundCnt += 1;
  93. $rate = bcdiv(strval($og['total_num'] - $refund['goods_num']),strval($og['total_num']),4);
  94. }
  95. if ($rate <= 1 && $rate >= 0){
  96. $saleVolume = bcadd(bcmul($ogSale,strval($rate),4),strval($saleVolume),4);
  97. }
  98. }
  99. $clearing_money = bcmul($saleVolume,strval($detail->commission_percent/100),4);
  100. $can_withdraw_money = WithdrawMoneyLog::addNewLog($detail->user_id,50,$clearing_money,'',$detail->order_id);
  101. if ($can_withdraw_money === false){
  102. log_record(__METHOD__.':failed orderId:'.$detail->order_id,'error');
  103. return false;
  104. }
  105. $remark = $refundCnt>0?'订单退部分扣除部分佣金':'';
  106. // 结算金额累计
  107. CommissionsDetail::where('id', $detail->id)
  108. ->update(['clearing_money_amount'=>$can_withdraw_money,
  109. 'clearing_money'=>$clearing_money,
  110. 'order_sale_volume'=>$saleVolume,
  111. 'clearing_status'=>1,
  112. 'remark'=>$remark,
  113. 'update_time'=>time()]);
  114. if (!in_array($detail->order_id,$data)){
  115. $data[] = $detail->order_id;
  116. }
  117. return true;
  118. }
  119. }