OrderService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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\Common;
  12. use App\Models\AccountModel;
  13. use App\Models\GoodsModel;
  14. use App\Models\MemberModel;
  15. use App\Models\OrderModel;
  16. use App\Models\ScoreGoodsModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 积分商城订单管理-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * Class OrderService
  25. * @package App\Services\Common
  26. */
  27. class OrderService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. * OrderService constructor.
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new OrderModel();
  40. }
  41. /**
  42. * 静态入口
  43. * @return static|null
  44. */
  45. public static function make()
  46. {
  47. if (!self::$instance) {
  48. self::$instance = (new static());
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * @param $params
  54. * @param int $pageSize
  55. * @return array
  56. */
  57. public function getDataList($params, $pageSize = 15)
  58. {
  59. $where = ['a.mark' => 1];
  60. $status = isset($params['status']) ? $params['status'] : 0;
  61. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  62. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  63. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  64. if ($shopId > 0) {
  65. $where['a.shop_id'] = $shopId;
  66. }
  67. if ($parentId > 0) {
  68. $where['b.parent_id'] = $parentId;
  69. }
  70. if ($status > 0) {
  71. $where['a.status'] = $status;
  72. }
  73. $list = $this->model->from('orders as a')
  74. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  75. ->leftJoin('score_goods as g', 'g.id', '=', 'a.goods_id')
  76. ->leftJoin('score_goods_cate as c', 'c.id', '=', 'g.cate_id')
  77. ->where($where)
  78. ->where(function ($query) use ($params) {
  79. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  80. if ($keyword) {
  81. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")->orWhere('g.company_name', 'like', "%{$keyword}%");
  82. }
  83. })
  84. ->where(function ($query) use ($userId) {
  85. if($userId){
  86. $query->where('a.user_id', '=', $userId);
  87. }
  88. })
  89. ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'c.name as cate_name', 'g.goods_name', 'g.company_name', 'g.code', 'g.thumb'])
  90. ->orderBy('a.pay_time', 'desc')
  91. ->orderBy('a.id', 'desc')
  92. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  93. $list = $list ? $list->toArray() : [];
  94. if ($list) {
  95. foreach ($list['data'] as &$item) {
  96. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  97. $item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
  98. $item['confirm_time'] = $item['confirm_time'] ? datetime($item['confirm_time'], 'Y-m-d H:i:s') : '';
  99. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  100. $item['pay_img'] = isset($item['pay_img']) && $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  101. }
  102. }
  103. return [
  104. 'pageSize' => $pageSize,
  105. 'total' => isset($list['total']) ? $list['total'] : 0,
  106. 'counts' => [
  107. ''
  108. ],
  109. 'list' => isset($list['data']) ? $list['data'] : []
  110. ];
  111. }
  112. /**
  113. * 添加或编辑
  114. * @return array
  115. * @since 2020/11/11
  116. * @author laravel开发员
  117. */
  118. public function edit()
  119. {
  120. $data = request()->all();
  121. return parent::edit($data); // TODO: Change the autogenerated stub
  122. }
  123. /**
  124. * 抢拍交易订单数
  125. * @param $userId 用户ID
  126. * @param int $status 状态
  127. * @return mixed
  128. */
  129. public function getNewTradeCount($userId, $status=0)
  130. {
  131. $where = ['user_id'=>$userId,'mark'=>1,'is_read'=>0];
  132. return $this->model->where($where)
  133. ->where(function($query) use($status){
  134. $query->whereIn('status',is_array($status)? $status:[$status]);
  135. })
  136. ->count('id');
  137. }
  138. /**
  139. * 详情
  140. * @param $id
  141. * @return mixed
  142. */
  143. public function getInfo($id)
  144. {
  145. $info = $this->model->from('orders as a')
  146. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  147. ->leftJoin('score_goods as g', 'g.id', '=', 'a.goods_id')
  148. ->where(['a.id'=>$id,'a.mark'=>1])
  149. ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'g.goods_name','g.source_price','g.company_name','g.qrcode', 'g.code','g.score', 'g.thumb'])
  150. ->first();
  151. if($info){
  152. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  153. $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
  154. $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
  155. $info['qrcode'] = isset($info['qrcode']) && $info['qrcode'] ? get_image_url($info['qrcode']) : '';
  156. $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  157. $address = MemberAddressService::make()->getBindInfo($info['user_id']);
  158. $info['address'] = $address? $address : [];
  159. // 积分
  160. // $scoreRate = MemberModel::where(['id'=> $info['user_id']])->value('score_rate');
  161. // $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  162. // $info['score'] = round($info['total']/$scoreRate, 0);
  163. $bankInfo = ConfigService::make()->getConfigByGroupValue('8');
  164. $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
  165. }
  166. return $info;
  167. }
  168. /**
  169. * 兑换
  170. * @param $userId
  171. */
  172. public function exchange($userId, $shopId)
  173. {
  174. $params = request()->all();
  175. $goodsId = isset($params['id'])? $params['id'] : 0;
  176. $info = ScoreGoodsModel::where(['id'=> $goodsId,'mark'=>1,'status'=>1])->first();
  177. if(empty($info)){
  178. $this->error = 2061;
  179. return false;
  180. }
  181. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->select(['id','member_level','nickname'])->first();
  182. $memberLevel = isset($memberInfo['member_level'])? $memberInfo['member_level'] : 0;
  183. if(empty($memberInfo)){
  184. $this->error = 2019;
  185. return false;
  186. }
  187. $total = $memberLevel? $info['price'] : $info['source_price'];
  188. $order = [
  189. 'user_id'=> $userId,
  190. 'shop_id'=> $shopId,
  191. 'goods_id'=> $goodsId,
  192. 'order_sn'=> get_order_num('S'),
  193. 'price'=> $info['price'],
  194. 'num'=> 1,
  195. 'total'=> $total,
  196. 'score'=> $info['score'],
  197. 'pay_money'=> 0,
  198. 'pay_score'=> 0,
  199. 'status'=>1,
  200. 'create_time'=> time(),
  201. 'update_time'=> time(),
  202. 'mark'=>1
  203. ];
  204. return $this->model->insertGetId($order);
  205. }
  206. /**
  207. * 支付兑换
  208. * @param $userId
  209. * @return bool
  210. */
  211. public function pay($userId)
  212. {
  213. $params = request()->all();
  214. $id = isset($params['id'])? $params['id'] : 0;
  215. $info = $this->model->where(['id'=> $id,'mark'=>1,'status'=>1])->first();
  216. $total = isset($info['total'])? $info['total'] : 0;
  217. $payScore = isset($info['score'])? $info['score'] : 0;
  218. if(!$id || empty($info)){
  219. $this->error = 2073;
  220. return false;
  221. }
  222. $addressInfo = isset($params['address'])? $params['address'] : [];
  223. $payImg = isset($params['pay_img'])? $params['pay_img'] : '';
  224. if(empty($addressInfo)){
  225. $this->error = 2075;
  226. return false;
  227. }
  228. if(empty($payImg)){
  229. $this->error = 2074;
  230. return false;
  231. }
  232. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->first();
  233. $score = isset($memberInfo['score'])? $memberInfo['score'] : 0;
  234. // $scoreRate = isset($memberInfo['score_rate'])? $memberInfo['score_rate'] : 0;
  235. // $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  236. if(empty($memberInfo)){
  237. $this->error = 2019;
  238. return false;
  239. }
  240. if($payScore > $score){
  241. $this->error = 2077;
  242. return false;
  243. }
  244. // 所需积分
  245. /*$needScore = round($total/$scoreRate, 0);
  246. if($payScore<$needScore && empty($payImg)){
  247. $this->error = 2076;
  248. return false;
  249. }*/
  250. /*$scorePayMoney = intval($payScore*$scoreRate, 0);
  251. $payMoney = round($total, 0);*/
  252. $payMoney = round($total, 0);
  253. $address = [];
  254. $address[] = isset($addressInfo['province'])? $addressInfo['province'] : '';
  255. $address[] = isset($addressInfo['city'])? $addressInfo['city'] : '';
  256. $address[] = isset($addressInfo['district'])? $addressInfo['district'] : '';
  257. $address[] = isset($addressInfo['address'])? $addressInfo['address'] : '';
  258. $address = array_filter($address);
  259. $data = [
  260. 'pay_img'=> $payImg,
  261. 'pay_time'=> time(),
  262. 'user_address'=> $address? implode(' ', $address) : '',
  263. 'realname'=> isset($addressInfo['realname'])? $addressInfo['realname'] : '',
  264. 'user_mobile'=> isset($addressInfo['mobile'])? $addressInfo['mobile'] : '',
  265. 'pay_score'=> $payScore,
  266. 'pay_money'=> $payMoney,
  267. 'status'=> 2,
  268. 'update_time'=>time()
  269. ];
  270. DB::beginTransaction();
  271. if(!$this->model->where(['id'=> $id])->update($data)){
  272. $this->error = 2079;
  273. Db::rollBack();
  274. return false;
  275. }
  276. if($payScore>0){
  277. if(!MemberModel::where(['id'=>$userId,'mark'=>1])->update(['score'=> max(0, $score - $payScore),'update_time'=>time()])){
  278. $this->error = 2079;
  279. DB::rollBack();
  280. return false;
  281. }
  282. $data = [
  283. 'user_id'=> $userId,
  284. 'shop_id'=>$info['shop_id'],
  285. 'source_order_sn'=> $info['order_sn'],
  286. 'type'=> 1,
  287. 'coin_type'=> 3,
  288. 'money'=> -$payScore,
  289. 'balance'=> $score,
  290. 'create_time'=> time(),
  291. 'update_time'=> time(),
  292. 'remark'=> '积分兑换商品',
  293. 'status'=>1,
  294. 'mark'=>1,
  295. ];
  296. if(!AccountModel::insertGetId($data)){
  297. $this->error = 2079;
  298. DB::rollBack();
  299. return false;
  300. }
  301. }
  302. Db::commit();
  303. $this->error = 2078;
  304. return true;
  305. }
  306. /**
  307. * 已收款
  308. * @return bool
  309. */
  310. public function received()
  311. {
  312. $params = request()->all();
  313. $id = isset($params['id'])? $params['id'] : 0;
  314. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  315. $status = isset($info['status'])? $info['status'] : 0;
  316. if(!$id || empty($info)){
  317. $this->error = 2081;
  318. return false;
  319. }
  320. if($status != 2){
  321. $this->error = 2090;
  322. return false;
  323. }
  324. if($this->model->where(['id'=> $id])->update(['status'=> 3,'confirm_time'=>time(), 'update_time'=> time()])){
  325. $this->error = 2092;
  326. return true;
  327. }else{
  328. $this->error = 2093;
  329. return false;
  330. }
  331. }
  332. /**
  333. * 已发货
  334. * @return bool
  335. */
  336. public function deliver()
  337. {
  338. $params = request()->all();
  339. $id = isset($params['id'])? $params['id'] : 0;
  340. $express = isset($params['express'])? $params['express'] : '';
  341. $expressNo = isset($params['express_no'])? $params['express_no'] : '';
  342. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  343. $status = isset($info['status'])? $info['status'] : 0;
  344. if(!$id || empty($info)){
  345. $this->error = 2081;
  346. return false;
  347. }
  348. if(!in_array($status, [2,3])){
  349. $this->error = 2090;
  350. return false;
  351. }
  352. if($this->model->where(['id'=> $id])->update(['status'=> 4,'express'=> $express,'express_no'=> $expressNo, 'update_time'=> time()])){
  353. $this->error = 2094;
  354. return true;
  355. }else{
  356. $this->error = 2095;
  357. return false;
  358. }
  359. }
  360. /**
  361. * 已收货
  362. * @return bool
  363. */
  364. public function receive()
  365. {
  366. $params = request()->all();
  367. $id = isset($params['id'])? $params['id'] : 0;
  368. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  369. $status = isset($info['status'])? $info['status'] : 0;
  370. if(!$id || empty($info)){
  371. $this->error = 2081;
  372. return false;
  373. }
  374. if(!in_array($status, [3,4,5])){
  375. $this->error = 2082;
  376. return false;
  377. }
  378. if($this->model->where(['id'=> $id])->update(['status'=> 5, 'update_time'=> time()])){
  379. $this->error = 2083;
  380. return true;
  381. }else{
  382. $this->error = 2084;
  383. return false;
  384. }
  385. }
  386. }