SettleService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }