OrderService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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\MemberModel;
  13. use App\Models\OrderModel;
  14. use App\Services\BaseService;
  15. use App\Services\ConfigService;
  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 OrderService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new OrderModel();
  36. }
  37. /**
  38. * 静态入口
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = new static();
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 订单列表
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $model = $this->getQuery($params);
  56. // 数据
  57. $list = $model->where(function ($query) use ($params) {
  58. $status = isset($params['status']) ? $params['status'] : 0;
  59. // 进行中
  60. if ($status > 0 && is_array($status)) {
  61. $query->whereIn('a.status', $status);
  62. }else if($status>0){
  63. $query->where('a.status', $status);
  64. }
  65. })->select(['a.*'])
  66. ->orderBy('a.status', 'desc')
  67. ->orderBy('a.create_time', 'desc')
  68. ->orderBy('a.id', 'desc')
  69. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  70. $list = $list ? $list->toArray() : [];
  71. if ($list) {
  72. $statusArr = [1 => '待审核', 2 => '进行中', 3 => '已完成'];
  73. foreach ($list['data'] as &$item) {
  74. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  75. $status = isset($item['status']) ? $item['status'] : 0;
  76. $item['status_text'] = '待审核';
  77. if ($status) {
  78. $item['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
  79. }
  80. $item['goods'] = isset($item['goods']) && $item['goods'] ? $item['goods'] : [];
  81. $item['goods']['shipper_phone_text'] = $item['goods']['shipper_phone']?format_mobile($item['goods']['shipper_phone']) : '';
  82. $item['goods']['receiver_phone_text'] = $item['goods']['receiver_phone']?format_mobile($item['goods']['receiver_phone']) : '';
  83. }
  84. unset($item);
  85. }
  86. return [
  87. 'pageSize' => $pageSize,
  88. 'total' => isset($list['total']) ? $list['total'] : 0,
  89. 'list' => isset($list['data']) ? $list['data'] : []
  90. ];
  91. }
  92. /**
  93. * 查询条件
  94. * @param $params
  95. * @return mixed
  96. */
  97. public function getQuery($params)
  98. {
  99. $where = ['a.mark' => 1];
  100. return $this->model->from('orders as a')->with(['goods'])
  101. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  102. ->where($where)
  103. ->where(function($query) use($params){
  104. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  105. if($userId>0){
  106. $query->where('a.user_id', $userId);
  107. }
  108. })
  109. ->where(function ($query) use ($params) {
  110. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  111. if ($keyword) {
  112. $query->where('a.order_no', 'like', "%{$keyword}%")
  113. ->orWhere('b.nickname', 'like', "%{$keyword}%")
  114. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  115. }
  116. });
  117. }
  118. /**
  119. * 订单详情
  120. * @param $id
  121. */
  122. public function getOrderInfo($id)
  123. {
  124. $statusArr = [1 => '待审核', 2 => '进行中', 3 => '已完成'];
  125. $info = $this->model->from('orders as a')->with(['goods'])
  126. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  127. ->where(['a.id' => $id, 'a.mark' => 1])
  128. ->select(['a.*'])
  129. ->first();
  130. if ($info) {
  131. $info = $info->toArray();
  132. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  133. $status = isset($info['status']) ? $info['status'] : 0;
  134. $info['status_text'] = '待付款';
  135. if ($status) {
  136. $info['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
  137. }
  138. $info['goods'] = isset($info['goods']) && $info['goods'] ? $info['goods'] : [];
  139. $info['goods']['shipper_phone_text'] = $info['goods']['shipper_phone']?format_mobile($info['goods']['shipper_phone']) : '';
  140. $info['goods']['receiver_phone_text'] = $info['goods']['receiver_phone']?format_mobile($info['goods']['receiver_phone']) : '';
  141. }
  142. return $info;
  143. }
  144. /**
  145. * 创建订单
  146. * @param $userId 用户
  147. * @param $params 参数
  148. * @return array|false
  149. */
  150. public function createOrder($userId, $params)
  151. {
  152. $goodsId = isset($params['goods_id']) && $params['goods_id'] ? intval($params['goods_id']) : 0;
  153. // 参数验证
  154. if (empty($goodsId)) {
  155. $this->error = 2103;
  156. return false;
  157. }
  158. // 缓存锁
  159. $cacheLockKey = "caches:orders:submit_lock:{$userId}_{$goodsId}";
  160. if (RedisService::get($cacheLockKey)) {
  161. $this->error = 2107;
  162. return false;
  163. }
  164. // 货物信息
  165. RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(2, 3));
  166. $goodsInfo = GoodsModel::where(['id' => $goodsId,'status'=>1, 'mark' => 1])
  167. ->select(['id','num', 'price','picker_status', 'status'])
  168. ->first();
  169. $pickerStatus = isset($goodsInfo['picker_status']) ? $goodsInfo['picker_status'] : 0;
  170. $price = isset($goodsInfo['price']) ? floatval($goodsInfo['price']) : 0;
  171. $num = isset($goodsInfo['num']) ? intval($goodsInfo['num']) : 0;
  172. if(empty($goodsInfo)){
  173. $this->error = 2201;
  174. RedisService::clear($cacheLockKey);
  175. return false;
  176. }
  177. if(in_array($pickerStatus,[2,3])){
  178. $this->error = "220{$pickerStatus}";
  179. RedisService::clear($cacheLockKey);
  180. return false;
  181. }
  182. if($price<=0){
  183. $this->error = 2204;
  184. RedisService::clear($cacheLockKey);
  185. return false;
  186. }
  187. // 用户信息
  188. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  189. ->select(['id', 'mobile', 'nickname','deposit','picker_status','confirm_status', 'status'])
  190. ->first();
  191. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  192. $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
  193. $confirmStatus = isset($userInfo['confirm_status']) ? $userInfo['confirm_status'] : 0;
  194. $userPickerStatus = isset($userInfo['picker_status']) ? $userInfo['picker_status'] : 0;
  195. $userDeposit = isset($userInfo['deposit']) ? floatval($userInfo['deposit']) : 0;
  196. if(empty($userInfo) || $status != 1){
  197. $this->error = 2016;
  198. RedisService::clear($cacheLockKey);
  199. return false;
  200. }
  201. // 账号审核情况
  202. if($confirmStatus != 1){
  203. $this->error = 2042;
  204. RedisService::clear($cacheLockKey);
  205. return false;
  206. }
  207. if($userPickerStatus != 1){
  208. $this->error = 2205;
  209. RedisService::clear($cacheLockKey);
  210. return false;
  211. }
  212. // 缴纳保证金验证
  213. $depositMoney = ConfigService::make()->getConfigByCode('deposit_money',0);
  214. $depositTotal = DepositService::make()->getPayDeposit($userId);
  215. if($depositMoney>0 && ($userDeposit < $depositMoney || $depositTotal < $depositMoney)){
  216. $this->error = 2206;
  217. RedisService::clear($cacheLockKey);
  218. return false;
  219. }
  220. // 订单数据
  221. $pickerCount = $this->model->where(['goods_id'=> $goodsId,'mark'=>1])->whereIn('status',[1,2])->count('id');
  222. if($this->model->where(['user_id'=>$userId,'goods_id'=>$goodsId,'mark'=>1])->whereIn('status',[1,2,3])->value('id')){
  223. $this->error = 2209;
  224. RedisService::clear($cacheLockKey);
  225. return false;
  226. }
  227. if($this->model->where(['user_id'=>$userId,'mark'=>1])->whereIn('status',[1,2])->value('id')){
  228. $this->error = 2209;
  229. RedisService::clear($cacheLockKey);
  230. return false;
  231. }
  232. $bonusRate = ConfigService::make()->getConfigByCode('picker_bonus_rate',100);
  233. $bonusRate = $bonusRate>0 && $bonusRate<=100? intval($bonusRate) : 100;
  234. $bonus = moneyFormat($price * $bonusRate/100,2);
  235. $orderNo = get_order_num('PD');
  236. $order = [
  237. 'order_no' => $orderNo,
  238. 'user_id' => $userId,
  239. 'goods_id' => $goodsId,
  240. 'total' => $price,
  241. 'bonus' => $bonus,
  242. 'create_time' => time(),
  243. 'update_time' => time(),
  244. 'status' => 1,
  245. 'mark' => 1,
  246. ];
  247. // 订单处理
  248. DB::beginTransaction();
  249. if (!$orderId = $this->model->insertGetId($order)) {
  250. DB::rollBack();
  251. $this->error = 2207;
  252. RedisService::clear($cacheLockKey);
  253. return false;
  254. }
  255. $title = "用户[{$nickname}]有新的抢单";
  256. $message = [
  257. 'from_uid'=> $userId,
  258. 'to_uid'=> 1,
  259. 'op'=> 'notice',
  260. 'scene'=> 'picker',
  261. 'message'=> '接单消息',
  262. 'type'=> 5,
  263. 'order'=>[
  264. 'title'=> $title.'<span class="ele-text-primary">查看订单</span>',
  265. 'order_no'=> $orderNo,
  266. 'money'=> $bonus,
  267. 'date'=> date('Y-m-d H:i:s'),
  268. 'user_id'=> $userId,
  269. 'type'=> 'deposit',
  270. 'remark'=> '接单消息',
  271. ]
  272. ];
  273. DB::commit();
  274. $pickerCount = $pickerCount+1;
  275. $this->error = lang('2208',['num'=>$pickerCount]);
  276. RedisService::clear($cacheLockKey);
  277. RedisService::keyDel("caches:goods:picker*");
  278. return ['order_id' => $orderId,'message'=>$message, 'total' => $price, 'num' => $pickerCount, 'goods' => $goodsId];
  279. }
  280. /**
  281. * 今日数量统计数据
  282. * @param $userId
  283. * @param int $type
  284. * @return array|mixed
  285. */
  286. public function getCountByDay($userId, $status=3)
  287. {
  288. $cacheKey = "caches:accounts:count_{$userId}_{$status}";
  289. $data = RedisService::get($cacheKey);
  290. if($data){
  291. return $data;
  292. }
  293. $data = $this->model->where(['user_id'=> $userId,'status'=>$status,'mark'=>1])
  294. ->where('create_time','>=', strtotime(date('Y-m-d')))
  295. ->count('id');
  296. if($data){
  297. RedisService::set($cacheKey, $data, rand(5,10));
  298. }
  299. return $data;
  300. }
  301. }