SettleService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\MemberCouponModel;
  15. use App\Models\OrderCommissionModel;
  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. public function businessBonus($userId, $total, $commission, $order)
  97. {
  98. }
  99. public function shopBonus($userId, $total, $commission, $order)
  100. {
  101. }
  102. /**
  103. * @param $order
  104. * @param int $orderType
  105. */
  106. public function commissionCount($order, $userInfo, $orderType=1)
  107. {
  108. $userId = isset($order['user_id'])?$order['user_id']:0;
  109. $orderNo = isset($order['order_no'])?$order['order_no']:'';
  110. $data = [
  111. 'user_id'=> $userId,
  112. 'order_no'=> $orderNo,
  113. 'store_id'=> isset($order['store_id'])?$order['store_id']:0,
  114. 'create_time'=>time(),
  115. 'status'=>2,
  116. 'mark'=>1,
  117. ];
  118. if($orderType==2){
  119. // 计算直推奖
  120. $parentId = isset($userInfo['parent_id'])? $userInfo['parent_id']:0;
  121. // 计算见单奖
  122. // 计算分红奖励
  123. }else{
  124. // 计算商家佣金
  125. // 计算绿色积分奖励
  126. // 计算数字资产+底池金额
  127. }
  128. // 佣金数据入库
  129. if(!$cid = OrderCommissionModel::where(['order_no'=>$orderNo])->value('id')){
  130. $cid = OrderCommissionModel::insertGetId($data);
  131. }else{
  132. OrderCommissionModel::where(['id'=>$cid])->update($data);
  133. }
  134. $this->error = '佣金计算成功';
  135. $data['id'] = $cid;
  136. return $data;
  137. }
  138. /**
  139. * 新人注册奖励
  140. * @param $userId
  141. * @return array|bool
  142. */
  143. public function registerReward($userId)
  144. {
  145. $rewardOpen = ConfigService::make()->getConfigByCode('register_award_coupon',0);
  146. $rewardCouponId = ConfigService::make()->getConfigByCode('register_reward_coupon_id',0);
  147. if($rewardCouponId<=0 || $rewardOpen!= 1){
  148. $this->error = '未开启或配置注册优惠券奖励';
  149. return true;
  150. }
  151. $couponInfo = CouponModel::where(['id'=>$rewardCouponId,'mark'=>1])
  152. ->first();
  153. $status = isset($couponInfo['status'])?$couponInfo['status']:0;
  154. if(empty($couponInfo) || $status != 1){
  155. $this->error = '配置的注册奖励优惠券不存在或无效';
  156. return true;
  157. }
  158. $data = [
  159. 'coupon_id'=> $rewardCouponId,
  160. 'user_id'=> $userId,
  161. 'store_id'=> isset($couponInfo['store_id'])?$couponInfo['store_id']:0,
  162. 'name'=> isset($couponInfo['name'])?$couponInfo['name']:'',
  163. 'coupon_type'=> isset($couponInfo['coupon_type'])?$couponInfo['coupon_type']: 20,
  164. 'reduce_price'=> isset($couponInfo['reduce_price'])?$couponInfo['reduce_price']:0,
  165. 'discount'=> isset($couponInfo['discount'])?$couponInfo['discount']:0,
  166. 'min_price'=> isset($couponInfo['min_price'])?$couponInfo['min_price']:0,
  167. 'expire_day'=> isset($couponInfo['expire_day'])?$couponInfo['expire_day']:0,
  168. 'start_time'=> isset($couponInfo['start_time'])?$couponInfo['start_time']:0,
  169. 'end_time'=> isset($couponInfo['end_time'])?$couponInfo['end_time']:0,
  170. 'goods_ids'=> isset($couponInfo['goods_ids'])&&$couponInfo['goods_ids']?$couponInfo['goods_ids']:'',
  171. 'create_time'=> time(),
  172. 'status'=> 1,
  173. ];
  174. if(!$id = MemberCouponModel::insertGetId($data)){
  175. $this->error = '奖励注册优惠券失败';
  176. return true;
  177. }
  178. $data['id'] = $id;
  179. $data['discount'] = floatval($data['discount']);
  180. $data['reduce_price'] = floatval($data['reduce_price']);
  181. unset($data['create_time']);
  182. unset($data['status']);
  183. unset($data['goods_ids']);
  184. $this->error = '奖励注册优惠券成功';
  185. return $data;
  186. }
  187. }