SettleService.php 3.1 KB

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