SettleService.php 5.5 KB

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