SettleService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\StoreModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 结算管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Api
  22. */
  23. class SettleService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new AccountLogModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 商家收益结算
  47. * @param $storeId
  48. * @param $money 收益
  49. * @param $order 订单数据
  50. * @return array|false|int
  51. */
  52. public function storeBonus($storeId, $money, $order)
  53. {
  54. $orderNo = isset($order['order_no'])? $order['order_no'] : '';
  55. if($money<=0 && $storeId<=0){
  56. $this->error = '无企业佣金可结算';
  57. return false;
  58. }
  59. $storeInfo = StoreModel::where(['id'=> $storeId,'mark'=>1])->first();
  60. $balance = isset($storeInfo['bonus_total'])? $storeInfo['bonus_total'] : 0;
  61. $storeUserId = isset($storeInfo['user_id'])? $storeInfo['user_id'] : 0;
  62. if($storeUserId<=0){
  63. $this->error = '企业账号错误';
  64. return false;
  65. }
  66. if(!StoreModel::where(['id'=> $storeId])->update(['bonus_total'=>DB::raw("bonus_total + {$money}"),'update_time'=>time()])){
  67. $this->error = '收货错误,企业结算错误,请联系客服处理';
  68. return -1;
  69. }
  70. $log = [
  71. 'user_id'=> $storeUserId,
  72. 'store_id'=> $storeId,
  73. 'source_order_no'=> isset($order['order_no'])? $order['order_no'] : '',
  74. 'type'=> 7,
  75. 'money'=> $money,
  76. 'after_money'=> moneyFormat($balance+$money,2),
  77. 'date'=>date('Y-m-d'),
  78. 'create_time'=>time(),
  79. 'remark'=> '企业收益',
  80. 'status'=>1
  81. ];
  82. if(!$id = $this->model->insertGetId($log)){
  83. $this->error = '企业收益结算失败,请联系客服处理';
  84. return -1;
  85. }
  86. $result = ['id'=>$id,'store_id'=>$storeId,'bonus'=>$money];
  87. if(env('APP_DEBUG')){
  88. RedisService::set("caches:settle:{$orderNo}:store_{$storeId}", $result, 7200);
  89. }
  90. return $result;
  91. }
  92. }