OrderService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  99. }
  100. }
  101. return [
  102. 'pageSize' => $pageSize,
  103. 'total' => isset($list['total']) ? $list['total'] : 0,
  104. 'counts' => [
  105. ''
  106. ],
  107. 'list' => isset($list['data']) ? $list['data'] : []
  108. ];
  109. }
  110. /**
  111. * 添加或编辑
  112. * @return array
  113. * @since 2020/11/11
  114. * @author laravel开发员
  115. */
  116. public function edit()
  117. {
  118. $data = request()->all();
  119. return parent::edit($data); // TODO: Change the autogenerated stub
  120. }
  121. /**
  122. * 抢拍交易订单数
  123. * @param $userId 用户ID
  124. * @param int $status 状态
  125. * @return mixed
  126. */
  127. public function getNewTradeCount($userId, $status=0)
  128. {
  129. $where = ['user_id'=>$userId,'mark'=>1,'is_read'=>0];
  130. return $this->model->where($where)
  131. ->where(function($query) use($status){
  132. $query->whereIn('status',is_array($status)? $status:[$status]);
  133. })
  134. ->count('id');
  135. }
  136. /**
  137. * 详情
  138. * @param $id
  139. * @return mixed
  140. */
  141. public function getInfo($id)
  142. {
  143. $info = $this->model->from('orders as a')
  144. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  145. ->leftJoin('score_goods as g', 'g.id', '=', 'a.goods_id')
  146. ->where(['a.id'=>$id,'a.mark'=>1])
  147. ->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'])
  148. ->first();
  149. if($info){
  150. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  151. $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
  152. $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
  153. $info['qrcode'] = isset($info['qrcode']) && $info['qrcode'] ? get_image_url($info['qrcode']) : '';
  154. $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  155. $address = MemberAddressService::make()->getBindInfo($info['user_id']);
  156. $info['address'] = $address? $address : [];
  157. // 积分
  158. // $scoreRate = MemberModel::where(['id'=> $info['user_id']])->value('score_rate');
  159. // $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  160. // $info['score'] = round($info['total']/$scoreRate, 0);
  161. $bankInfo = ConfigService::make()->getConfigByGroupValue('8');
  162. $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
  163. }
  164. return $info;
  165. }
  166. /**
  167. * 兑换
  168. * @param $userId
  169. */
  170. public function exchange($userId, $shopId)
  171. {
  172. $params = request()->all();
  173. $goodsId = isset($params['id'])? $params['id'] : 0;
  174. $info = ScoreGoodsModel::where(['id'=> $goodsId,'mark'=>1,'status'=>1])->first();
  175. if(empty($info)){
  176. $this->error = 2061;
  177. return false;
  178. }
  179. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->select(['id','member_level','nickname'])->first();
  180. $memberLevel = isset($memberInfo['member_level'])? $memberInfo['member_level'] : 0;
  181. if(empty($memberInfo)){
  182. $this->error = 2019;
  183. return false;
  184. }
  185. $total = $memberLevel? $info['price'] : $info['source_price'];
  186. $order = [
  187. 'user_id'=> $userId,
  188. 'shop_id'=> $shopId,
  189. 'goods_id'=> $goodsId,
  190. 'order_sn'=> get_order_num('S'),
  191. 'price'=> $info['price'],
  192. 'num'=> 1,
  193. 'total'=> $total,
  194. 'score'=> $info['score'],
  195. 'pay_money'=> 0,
  196. 'pay_score'=> 0,
  197. 'status'=>1,
  198. 'create_time'=> time(),
  199. 'update_time'=> time(),
  200. 'mark'=>1
  201. ];
  202. return $this->model->insertGetId($order);
  203. }
  204. /**
  205. * 支付兑换
  206. * @param $userId
  207. * @return bool
  208. */
  209. public function pay($userId)
  210. {
  211. $params = request()->all();
  212. $id = isset($params['id'])? $params['id'] : 0;
  213. $info = $this->model->where(['id'=> $id,'mark'=>1,'status'=>1])->first();
  214. $total = isset($info['total'])? $info['total'] : 0;
  215. $payScore = isset($info['score'])? $info['score'] : 0;
  216. if(!$id || empty($info)){
  217. $this->error = 2073;
  218. return false;
  219. }
  220. $addressInfo = isset($params['address'])? $params['address'] : [];
  221. $payImg = isset($params['pay_img'])? $params['pay_img'] : '';
  222. if(empty($addressInfo)){
  223. $this->error = 2075;
  224. return false;
  225. }
  226. if(empty($payImg)){
  227. $this->error = 2074;
  228. return false;
  229. }
  230. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->first();
  231. $score = isset($memberInfo['score'])? $memberInfo['score'] : 0;
  232. // $scoreRate = isset($memberInfo['score_rate'])? $memberInfo['score_rate'] : 0;
  233. // $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  234. if(empty($memberInfo)){
  235. $this->error = 2019;
  236. return false;
  237. }
  238. if($payScore > $score){
  239. $this->error = 2077;
  240. return false;
  241. }
  242. // 所需积分
  243. /*$needScore = round($total/$scoreRate, 0);
  244. if($payScore<$needScore && empty($payImg)){
  245. $this->error = 2076;
  246. return false;
  247. }*/
  248. /*$scorePayMoney = intval($payScore*$scoreRate, 0);
  249. $payMoney = round($total, 0);*/
  250. $payMoney = round($total, 0);
  251. $address = [];
  252. $address[] = isset($addressInfo['province'])? $addressInfo['province'] : '';
  253. $address[] = isset($addressInfo['city'])? $addressInfo['city'] : '';
  254. $address[] = isset($addressInfo['district'])? $addressInfo['district'] : '';
  255. $address[] = isset($addressInfo['address'])? $addressInfo['address'] : '';
  256. $address = array_filter($address);
  257. $data = [
  258. 'pay_img'=> $payImg,
  259. 'pay_time'=> time(),
  260. 'user_address'=> $address? implode(' ', $address) : '',
  261. 'realname'=> isset($addressInfo['realname'])? $addressInfo['realname'] : '',
  262. 'user_mobile'=> isset($addressInfo['mobile'])? $addressInfo['mobile'] : '',
  263. 'pay_score'=> $payScore,
  264. 'pay_money'=> $payMoney,
  265. 'status'=> 2,
  266. 'update_time'=>time()
  267. ];
  268. DB::beginTransaction();
  269. if(!$this->model->where(['id'=> $id])->update($data)){
  270. $this->error = 2079;
  271. Db::rollBack();
  272. return false;
  273. }
  274. if($payScore>0){
  275. if(!MemberModel::where(['id'=>$userId,'mark'=>1])->update(['score'=> max(0, $score - $payScore),'update_time'=>time()])){
  276. $this->error = 2079;
  277. DB::rollBack();
  278. return false;
  279. }
  280. $data = [
  281. 'user_id'=> $userId,
  282. 'shop_id'=>$info['shop_id'],
  283. 'source_order_sn'=> $info['order_sn'],
  284. 'type'=> 1,
  285. 'coin_type'=> 3,
  286. 'money'=> -$payScore,
  287. 'balance'=> $score,
  288. 'create_time'=> time(),
  289. 'update_time'=> time(),
  290. 'remark'=> '积分兑换商品',
  291. 'status'=>1,
  292. 'mark'=>1,
  293. ];
  294. if(!AccountModel::insertGetId($data)){
  295. $this->error = 2079;
  296. DB::rollBack();
  297. return false;
  298. }
  299. }
  300. Db::commit();
  301. $this->error = 2078;
  302. return true;
  303. }
  304. /**
  305. * 已收款
  306. * @return bool
  307. */
  308. public function received()
  309. {
  310. $params = request()->all();
  311. $id = isset($params['id'])? $params['id'] : 0;
  312. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  313. $status = isset($info['status'])? $info['status'] : 0;
  314. if(!$id || empty($info)){
  315. $this->error = 2081;
  316. return false;
  317. }
  318. if($status != 2){
  319. $this->error = 2090;
  320. return false;
  321. }
  322. if($this->model->where(['id'=> $id])->update(['status'=> 3, 'update_time'=> time()])){
  323. $this->error = 2092;
  324. return true;
  325. }else{
  326. $this->error = 2093;
  327. return false;
  328. }
  329. }
  330. /**
  331. * 已发货
  332. * @return bool
  333. */
  334. public function deliver()
  335. {
  336. $params = request()->all();
  337. $id = isset($params['id'])? $params['id'] : 0;
  338. $express = isset($params['express'])? $params['express'] : '';
  339. $expressNo = isset($params['express_no'])? $params['express_no'] : '';
  340. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  341. $status = isset($info['status'])? $info['status'] : 0;
  342. if(!$id || empty($info)){
  343. $this->error = 2081;
  344. return false;
  345. }
  346. if(!in_array($status, [2,3])){
  347. $this->error = 2090;
  348. return false;
  349. }
  350. if($this->model->where(['id'=> $id])->update(['status'=> 4,'express'=> $express,'express_no'=> $expressNo, 'update_time'=> time()])){
  351. $this->error = 2094;
  352. return true;
  353. }else{
  354. $this->error = 2095;
  355. return false;
  356. }
  357. }
  358. /**
  359. * 已收货
  360. * @return bool
  361. */
  362. public function receive()
  363. {
  364. $params = request()->all();
  365. $id = isset($params['id'])? $params['id'] : 0;
  366. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  367. $status = isset($info['status'])? $info['status'] : 0;
  368. if(!$id || empty($info)){
  369. $this->error = 2081;
  370. return false;
  371. }
  372. if(!in_array($status, [3,4,5])){
  373. $this->error = 2082;
  374. return false;
  375. }
  376. if($this->model->where(['id'=> $id])->update(['status'=> 5, 'update_time'=> time()])){
  377. $this->error = 2083;
  378. return true;
  379. }else{
  380. $this->error = 2084;
  381. return false;
  382. }
  383. }
  384. }