SettleService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AccountLogModel;
  13. use App\Models\CouponModel;
  14. use App\Models\MeetingModel;
  15. use App\Models\StoreModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 结算管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * @package App\Services\Api
  25. */
  26. class SettleService extends BaseService
  27. {
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new AccountLogModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 商家收益结算
  50. * @param $storeId
  51. * @param $money 收益
  52. * @param $order 订单数据
  53. * @return array|false|int
  54. */
  55. public function storeBonus($storeId, $money, $order)
  56. {
  57. $orderNo = isset($order['order_no'])? $order['order_no'] : '';
  58. if($money<=0 && $storeId<=0){
  59. $this->error = '无企业佣金可结算';
  60. return false;
  61. }
  62. $storeInfo = StoreModel::where(['id'=> $storeId,'mark'=>1])->first();
  63. $balance = isset($storeInfo['bonus_total'])? $storeInfo['bonus_total'] : 0;
  64. $storeUserId = isset($storeInfo['user_id'])? $storeInfo['user_id'] : 0;
  65. if($storeUserId<=0){
  66. $this->error = '企业账号错误';
  67. return false;
  68. }
  69. if(!StoreModel::where(['id'=> $storeId])->update(['bonus_total'=>DB::raw("bonus_total + {$money}"),'update_time'=>time()])){
  70. $this->error = '收货错误,企业结算错误,请联系客服处理';
  71. return -1;
  72. }
  73. $log = [
  74. 'user_id'=> $storeUserId,
  75. 'store_id'=> $storeId,
  76. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  77. 'type'=> 7,
  78. 'money'=> $money,
  79. 'after_money'=> moneyFormat($balance+$money,2),
  80. 'date'=>date('Y-m-d'),
  81. 'create_time'=>time(),
  82. 'remark'=> '收益',
  83. 'status'=>1
  84. ];
  85. if(!$id = $this->model->insertGetId($log)){
  86. $this->error = '企业收益结算失败,请联系客服处理';
  87. return -1;
  88. }
  89. $result = ['id'=>$id,'store_id'=>$storeId,'bonus'=>$money];
  90. if(env('APP_DEBUG')){
  91. RedisService::set("caches:settle:{$orderNo}:store_{$storeId}", $result, 7200);
  92. }
  93. return $result;
  94. }
  95. /**
  96. * 新人注册奖励
  97. * @param $userId
  98. * @return array|bool
  99. */
  100. public function registerReward($userId)
  101. {
  102. $rewardOpen = ConfigService::make()->getConfigByCode('register_award_coupon',0);
  103. $rewardCouponId = ConfigService::make()->getConfigByCode('register_reward_coupon_id',0);
  104. if($rewardCouponId<=0 || $rewardOpen!= 1){
  105. $this->error = '未开启或配置注册优惠券奖励';
  106. return true;
  107. }
  108. $couponInfo = CouponModel::where(['id'=>$rewardCouponId,'mark'=>1])
  109. ->first();
  110. $status = isset($couponInfo['status'])?$couponInfo['status']:0;
  111. if(empty($couponInfo) || $status != 1){
  112. $this->error = '配置的注册奖励优惠券不存在或无效';
  113. return true;
  114. }
  115. $data = [
  116. 'coupon_id'=> $rewardCouponId,
  117. 'user_id'=> $userId,
  118. 'store_id'=> isset($couponInfo['store_id'])?$couponInfo['store_id']:0,
  119. 'name'=> isset($couponInfo['name'])?$couponInfo['name']:'',
  120. 'coupon_type'=> isset($couponInfo['coupon_type'])?$couponInfo['coupon_type']: 20,
  121. 'reduce_price'=> isset($couponInfo['reduce_price'])?$couponInfo['reduce_price']:0,
  122. 'discount'=> isset($couponInfo['discount'])?$couponInfo['discount']:0,
  123. 'min_price'=> isset($couponInfo['min_price'])?$couponInfo['min_price']:0,
  124. 'expire_day'=> isset($couponInfo['expire_day'])?$couponInfo['expire_day']:0,
  125. 'start_time'=> isset($couponInfo['start_time'])?$couponInfo['start_time']:0,
  126. 'end_time'=> isset($couponInfo['end_time'])?$couponInfo['end_time']:0,
  127. 'goods_ids'=> isset($couponInfo['goods_ids'])&&$couponInfo['goods_ids']?$couponInfo['goods_ids']:'',
  128. 'create_time'=> time(),
  129. 'status'=> 1,
  130. ];
  131. if(!$id =CouponModel::insertGetId($data)){
  132. $this->error = '奖励注册优惠券失败';
  133. return true;
  134. }
  135. $data['id'] = $id;
  136. unset($data['create_time']);
  137. unset($data['status']);
  138. unset($data['goods_ids']);
  139. $this->error = '奖励注册优惠券成功';
  140. return $data;
  141. }
  142. }