SettleService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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\OrderModel;
  16. use App\Models\PayOrdersModel;
  17. use App\Models\StoreCategoryModel;
  18. use App\Models\StoreModel;
  19. use App\Services\BaseService;
  20. use App\Services\ConfigService;
  21. use App\Services\RedisService;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 结算管理-服务类
  25. * @author laravel开发员
  26. * @since 2020/11/11
  27. * @package App\Services\Api
  28. */
  29. class SettleService extends BaseService
  30. {
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. */
  36. public function __construct()
  37. {
  38. $this->model = new AccountLogModel();
  39. }
  40. /**
  41. * 静态入口
  42. * @return static|null
  43. */
  44. public static function make()
  45. {
  46. if (!self::$instance) {
  47. self::$instance = (new static());
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * 商家收益结算
  53. * @param $storeId
  54. * @param $money 收益
  55. * @param $order 订单数据
  56. * @return array|false|int
  57. */
  58. public function storeBonus($storeId, $money, $order)
  59. {
  60. $orderNo = isset($order['order_no'])? $order['order_no'] : '';
  61. $bonusSettle = isset($order['bonus_settle']) && $order['bonus_settle']? $order['bonus_settle'] : 2;
  62. if($money<=0 && $storeId<=0){
  63. $this->error = '无商家佣金可结算';
  64. return 0;
  65. }
  66. if($bonusSettle == 1){
  67. $this->error = '佣金已结算';
  68. return 0;
  69. }
  70. $storeInfo = StoreModel::where(['id'=> $storeId,'mark'=>1])->first();
  71. $balance = isset($storeInfo['balance'])? $storeInfo['balance'] : 0;
  72. $storeUserId = isset($storeInfo['user_id'])? $storeInfo['user_id'] : 0;
  73. if($storeUserId<=0){
  74. $this->error = '商家账号错误';
  75. return 0;
  76. }
  77. if(AccountLogModel::where(['source_order_no'=>$orderNo,'status'=>1,'mark'=>1])->value('id')){
  78. $this->error = '该订单已结算商家佣金';
  79. return 0;
  80. }
  81. if(!StoreModel::where(['id'=> $storeId])->update(['balance'=>DB::raw("balance + {$money}"),'income'=>DB::raw("income + {$money}"),'update_time'=>time()])){
  82. $this->error = '收货错误,商家结算错误,请联系客服处理';
  83. return -1;
  84. }
  85. $log = [
  86. 'user_id'=> $storeUserId,
  87. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  88. 'type'=> 7,
  89. 'money'=> $money,
  90. 'before_money'=> $balance,
  91. 'date'=>date('Y-m-d'),
  92. 'create_time'=>time(),
  93. 'remark'=> '商家收益',
  94. 'status'=>1
  95. ];
  96. if(!$id = $this->model->insertGetId($log)){
  97. $this->error = '商家收益结算失败,请联系客服处理';
  98. return -1;
  99. }
  100. $result = ['id'=>$id,'store_id'=>$storeId,'bonus'=>$money];
  101. if(env('APP_DEBUG')){
  102. RedisService::set("caches:settle:{$orderNo}:store_{$storeId}", $result, 7200);
  103. }
  104. return $result;
  105. }
  106. /**
  107. * 代理收益
  108. * @param $userId
  109. * @param $bonus
  110. * @param $order
  111. * @param $recBonusId
  112. * @param int $type
  113. * @return array|false|int
  114. */
  115. public function agentBonus($userId, $bonus, $order, $recBonusId, $type=0)
  116. {
  117. $orderId = isset($order['id'])? $order['id'] : 0;
  118. $total = isset($order['pay_total'])? $order['pay_total'] : 0;
  119. $bonusSettle = isset($order['bonus_settle']) && $order['bonus_settle']? $order['bonus_settle'] : 2;
  120. if($orderId<=0 || $bonus<=0 || $userId<=0 || $recBonusId<=0){
  121. $this->error = '无收益可结算';
  122. return false;
  123. }
  124. if($bonusSettle == 1){
  125. $this->error = '收益已结算';
  126. return 0;
  127. }
  128. $parent = AgentModel::with(['user'])->where(['user_id'=> $recBonusId,'status'=>1,'mark'=>1])
  129. ->select(['id','balance','user_id','income','status'])
  130. ->first();
  131. $agentId = isset($parent['id'])? $parent['id'] : 0;
  132. $balance = isset($parent['balance'])? $parent['balance'] : 0;
  133. $parentInfo = isset($parent['user'])? $parent['user'] : [];
  134. $parentOneId = isset($parentInfo['parent_id'])? $parentInfo['parent_id'] :0;
  135. // 推荐消费者的佣金
  136. $logs = [];
  137. if(empty($parent) || $agentId<=0){
  138. $this->error = '上级代理无效';
  139. return 0;
  140. }
  141. 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()])){
  142. $this->error = '推荐消费者收益结算错误,请联系客服处理';
  143. return -1;
  144. }
  145. $logs[] = [
  146. 'user_id'=> $recBonusId,
  147. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  148. 'type'=> 9,
  149. 'money'=> $bonus,
  150. 'before_money'=> $balance,
  151. 'date'=>date('Y-m-d'),
  152. 'create_time'=>time(),
  153. 'remark'=> '推广收益',
  154. 'remark1'=> ['','话费充值','电费充值','燃气充值'][$type],
  155. 'status'=>1
  156. ];
  157. // 一级代理收益
  158. $parentTwoId = 0;
  159. $oneBonus = 0;
  160. if($bonus>0 && $parentOneId){
  161. $parentOne = AgentModel::with(['user'])->where(['user_id'=> $parentOneId,'status'=>1,'mark'=>1])
  162. ->select(['id','balance','user_id','income','status'])
  163. ->first();
  164. $oneId = isset($parentOne['id'])? $parentOne['id'] : 0;
  165. $oneBalance = isset($parentOne['balance'])? $parentOne['balance'] : 0;
  166. $oneInfo = isset($parentOne['user'])? $parentOne['user'] : [];
  167. $parentTwoId = isset($oneInfo['parent_id'])? $oneInfo['parent_id'] :0;
  168. $agentBonusLevel1Rate = ConfigService::make()->getConfigByCode('agent_bonus_level1_rate', 0);
  169. $agentBonusLevel1Rate = $agentBonusLevel1Rate>0 && $agentBonusLevel1Rate<100? $agentBonusLevel1Rate : 0;
  170. $oneBonus = moneyFormat($agentBonusLevel1Rate * $bonus/100, 4);
  171. if($oneBonus>=0.01){
  172. if(!AgentModel::where(['id'=> $oneId])->update(['balance'=>DB::raw("balance + ".moneyFormat($oneBonus,2)),'income'=>DB::raw("income + ".moneyFormat($oneBonus,2)),'order_count'=>DB::raw("order_count + 1"),'update_time'=>time()])){
  173. $this->error = '推荐代理收益结算错误,请联系客服处理';
  174. return -1;
  175. }
  176. $logs[] = [
  177. 'user_id'=> $parentOneId,
  178. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  179. 'type'=> 8,
  180. 'money'=> moneyFormat($oneBonus,2),
  181. 'before_money'=> $oneBalance,
  182. 'date'=>date('Y-m-d'),
  183. 'create_time'=>time()+1,
  184. 'remark'=> '一级代理收益',
  185. 'remark1'=> ['','话费充值','电费充值','燃气充值'][$type],
  186. 'status'=>1
  187. ];
  188. }
  189. }
  190. // 二级代理收益
  191. $twoBonus = 0;
  192. if($bonus>0 && $parentTwoId){
  193. $parentTwo = AgentModel::with(['user'])->where(['user_id'=> $parentTwoId,'status'=>1,'mark'=>1])
  194. ->select(['id','balance','user_id','income','status'])
  195. ->first();
  196. $twoId = isset($parentTwo['id'])? $parentTwo['id'] : 0;
  197. $twoBalance = isset($parentTwo['balance'])? $parentTwo['balance'] : 0;
  198. $agentBonusLevel2Rate = ConfigService::make()->getConfigByCode('agent_bonus_level2_rate', 0);
  199. $agentBonusLevel2Rate = $agentBonusLevel2Rate>0 && $agentBonusLevel2Rate<100? $agentBonusLevel2Rate : 0;
  200. $twoBonus = moneyFormat($agentBonusLevel2Rate * $bonus/100, 4);
  201. if($twoBonus>=0.01){
  202. if(!AgentModel::where(['id'=> $twoId])->update(['balance'=>DB::raw("balance + ".moneyFormat($twoBonus,2)),'income'=>DB::raw("income + ".moneyFormat($twoBonus,2)),'order_count'=>DB::raw("order_count + 1"),'update_time'=>time()])){
  203. $this->error = '推荐代理收益结算错误,请联系客服处理';
  204. return -1;
  205. }
  206. $logs[] = [
  207. 'user_id'=> $parentTwoId,
  208. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  209. 'type'=> 8,
  210. 'money'=> moneyFormat($twoBonus,2),
  211. 'before_money'=> $twoBalance,
  212. 'date'=>date('Y-m-d'),
  213. 'create_time'=>time()+2,
  214. 'remark'=> '二级代理收益',
  215. 'remark1'=> ['','话费充值','电费充值','燃气充值'][$type],
  216. 'status'=>1
  217. ];
  218. }
  219. }
  220. if($logs && !$this->model->insert($logs)){
  221. $this->error = '推荐代理收益结算错误,请联系客服处理';
  222. return -1;
  223. }
  224. $result = ['user_id'=>$userId,'total'=> $total,'bonus'=>$bonus,'parent_id'=>$recBonusId,'oneId'=>$parentOneId,'oneBonus'=>$oneBonus,'twoId'=>$parentTwoId,'twoBonus'=>$twoBonus];
  225. return $result;
  226. }
  227. }