SettleService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\AgentModel;
  14. use App\Models\MemberModel;
  15. use App\Models\StoreCategoryModel;
  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['balance'])? $storeInfo['balance'] : 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(['balance'=>DB::raw("balance + {$money}"),'income'=>DB::raw("income + {$money}"),'update_time'=>time()])){
  71. $this->error = '收货错误,商家结算错误,请联系客服处理';
  72. return -1;
  73. }
  74. $log = [
  75. 'user_id'=> $storeUserId,
  76. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  77. 'type'=> 7,
  78. 'money'=> $money,
  79. 'before_money'=> $balance,
  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. * @param $money
  99. * @param $order
  100. * @param $parentId
  101. * @param int $type
  102. * @return array|false|int
  103. */
  104. public function agentBonus($userId, $money, $order, $parentId, $type=0)
  105. {
  106. $orderNo = isset($order['order_no'])? $order['order_no'] : '';
  107. if($money<=0 || $userId<=0 || $parentId<=0){
  108. $this->error = '无收益可结算';
  109. return false;
  110. }
  111. $parent = AgentModel::with(['user'])->where(['user_id'=> $parentId,'status'=>1,'mark'=>1])
  112. ->select(['id','balance','user_id','income','status'])
  113. ->first();
  114. $agentId = isset($parent['id'])? $parent['id'] : 0;
  115. $balance = isset($parent['balance'])? $parent['balance'] : 0;
  116. $parentInfo = isset($parent['user'])? $parent['user'] : [];
  117. $parentOneId = isset($parentInfo['parent_id'])? $parentInfo['parent_id'] :0;
  118. // 推荐消费者的佣金
  119. $logs = [];
  120. $agentDirectBonusRate = ConfigService::make()->getConfigByCode('agent_direct_bonus_rate', 0);
  121. $agentDirectBonusRate = $agentDirectBonusRate>0 && $agentDirectBonusRate<100? $agentDirectBonusRate : 0;
  122. $bonus = moneyFormat($agentDirectBonusRate * $money/100, 2);
  123. if(empty($parent) || $bonus<=0 || $agentId<=0){
  124. $this->error = '上级代理无效';
  125. return 0;
  126. }
  127. if(!AgentModel::where(['id'=> $agentId])->update(['balance'=>DB::raw("balance + {$bonus}"),'income'=>DB::raw("income + {$bonus}"),'order_count'=>DB::raw("order_count + 1"),'update_time'=>time()])){
  128. $this->error = '推荐消费者收益结算错误,请联系客服处理';
  129. return -1;
  130. }
  131. $logs[] = [
  132. 'user_id'=> $parentId,
  133. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  134. 'type'=> 9,
  135. 'money'=> $bonus,
  136. 'before_money'=> $balance,
  137. 'date'=>date('Y-m-d'),
  138. 'create_time'=>time(),
  139. 'remark'=> '推广收益',
  140. 'remark1'=> ['','话费充值','电费充值','燃气充值'][$type],
  141. 'status'=>1
  142. ];
  143. // 一级代理收益
  144. $parentTwoId = 0;
  145. $oneBonus = 0;
  146. if($bonus>0 && $parentOneId){
  147. $parentOne = AgentModel::with(['user'])->where(['user_id'=> $parentOneId,'status'=>1,'mark'=>1])
  148. ->select(['id','balance','user_id','income','status'])
  149. ->first();
  150. $oneId = isset($parentOne['id'])? $parentOne['id'] : 0;
  151. $oneBalance = isset($parentOne['balance'])? $parentOne['balance'] : 0;
  152. $oneInfo = isset($parentOne['user'])? $parentOne['user'] : [];
  153. $parentTwoId = isset($oneInfo['parent_id'])? $oneInfo['parent_id'] :0;
  154. $agentBonusLevel1Rate = ConfigService::make()->getConfigByCode('agent_bonus_level1_rate', 0);
  155. $agentBonusLevel1Rate = $agentBonusLevel1Rate>0 && $agentBonusLevel1Rate<100? $agentBonusLevel1Rate : 0;
  156. $oneBonus = moneyFormat($agentBonusLevel1Rate * $bonus/100, 2);
  157. if(!AgentModel::where(['id'=> $oneId])->update(['balance'=>DB::raw("balance + {$oneBonus}"),'income'=>DB::raw("income + {$oneBonus}"),'order_count'=>DB::raw("order_count + 1"),'update_time'=>time()])){
  158. $this->error = '推荐代理收益结算错误,请联系客服处理';
  159. return -1;
  160. }
  161. $logs[] = [
  162. 'user_id'=> $parentOneId,
  163. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  164. 'type'=> 8,
  165. 'money'=> $oneBonus,
  166. 'before_money'=> $oneBalance,
  167. 'date'=>date('Y-m-d'),
  168. 'create_time'=>time()+1,
  169. 'remark'=> '一级代理收益',
  170. 'remark1'=> ['','话费充值','电费充值','燃气充值'][$type],
  171. 'status'=>1
  172. ];
  173. }
  174. // 二级代理收益
  175. $twoBonus = 0;
  176. if($bonus>0 && $parentTwoId){
  177. $parentTwo = AgentModel::with(['user'])->where(['user_id'=> $parentTwoId,'status'=>1,'mark'=>1])
  178. ->select(['id','balance','user_id','income','status'])
  179. ->first();
  180. $twoId = isset($parentTwo['id'])? $parentTwo['id'] : 0;
  181. $twoBalance = isset($parentTwo['balance'])? $parentTwo['balance'] : 0;
  182. $agentBonusLevel2Rate = ConfigService::make()->getConfigByCode('agent_bonus_level2_rate', 0);
  183. $agentBonusLevel2Rate = $agentBonusLevel2Rate>0 && $agentBonusLevel2Rate<100? $agentBonusLevel2Rate : 0;
  184. $twoBonus = moneyFormat($agentBonusLevel2Rate * $bonus/100, 2);
  185. if(!AgentModel::where(['id'=> $twoId])->update(['balance'=>DB::raw("balance + {$twoBonus}"),'income'=>DB::raw("income + {$twoBonus}"),'order_count'=>DB::raw("order_count + 1"),'update_time'=>time()])){
  186. $this->error = '推荐代理收益结算错误,请联系客服处理';
  187. return -1;
  188. }
  189. $logs[] = [
  190. 'user_id'=> $parentTwoId,
  191. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  192. 'type'=> 8,
  193. 'money'=> $twoBonus,
  194. 'before_money'=> $twoBalance,
  195. 'date'=>date('Y-m-d'),
  196. 'create_time'=>time()+2,
  197. 'remark'=> '二级代理收益',
  198. 'remark1'=> ['','话费充值','电费充值','燃气充值'][$type],
  199. 'status'=>1
  200. ];
  201. }
  202. if($logs && !$this->model->insert($logs)){
  203. $this->error = '推荐代理收益结算错误,请联系客服处理';
  204. return -1;
  205. }
  206. $result = ['user_id'=>$userId,'total'=> $money,'bonus'=>$bonus,'parent_id'=>$parentId,'oneId'=>$parentOneId,'oneBonus'=>$oneBonus,'twoId'=>$parentTwoId,'twoBonus'=>$twoBonus];
  207. if(env('APP_DEBUG')){
  208. RedisService::set("caches:settle:{$orderNo}:agent_{$userId}", $result, 7200);
  209. }
  210. return $result;
  211. }
  212. }