OrderService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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.goods_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.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.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['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  154. $address = MemberAddressService::make()->getBindInfo($info['user_id']);
  155. $info['address'] = $address? $address : [];
  156. // 积分
  157. // $scoreRate = MemberModel::where(['id'=> $info['user_id']])->value('score_rate');
  158. // $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  159. // $info['score'] = round($info['total']/$scoreRate, 0);
  160. $bankInfo = ConfigService::make()->getConfigByGroupValue('8');
  161. $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
  162. }
  163. return $info;
  164. }
  165. /**
  166. * 兑换
  167. * @param $userId
  168. */
  169. public function exchange($userId, $shopId)
  170. {
  171. $params = request()->all();
  172. $goodsId = isset($params['id'])? $params['id'] : 0;
  173. $info = ScoreGoodsModel::where(['id'=> $goodsId,'mark'=>1,'status'=>1])->first();
  174. if(empty($info)){
  175. $this->error = 2061;
  176. return false;
  177. }
  178. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->select(['id','member_level','nickname'])->first();
  179. $memberLevel = isset($memberInfo['member_level'])? $memberInfo['member_level'] : 0;
  180. if(empty($memberInfo)){
  181. $this->error = 2019;
  182. return false;
  183. }
  184. $total = $memberLevel? $info['price'] : $info['source_price'];
  185. $order = [
  186. 'user_id'=> $userId,
  187. 'shop_id'=> $shopId,
  188. 'goods_id'=> $goodsId,
  189. 'order_sn'=> get_order_num('S'),
  190. 'price'=> $info['price'],
  191. 'num'=> 1,
  192. 'total'=> $total,
  193. 'score'=> $info['score'],
  194. 'pay_money'=> 0,
  195. 'pay_score'=> 0,
  196. 'status'=>1,
  197. 'create_time'=> time(),
  198. 'update_time'=> time(),
  199. 'mark'=>1
  200. ];
  201. return $this->model->insertGetId($order);
  202. }
  203. /**
  204. * 支付兑换
  205. * @param $userId
  206. * @return bool
  207. */
  208. public function pay($userId)
  209. {
  210. $params = request()->all();
  211. $id = isset($params['id'])? $params['id'] : 0;
  212. $info = $this->model->where(['id'=> $id,'mark'=>1,'status'=>1])->first();
  213. $total = isset($info['total'])? $info['total'] : 0;
  214. $payScore = isset($info['score'])? $info['score'] : 0;
  215. if(!$id || empty($info)){
  216. $this->error = 2073;
  217. return false;
  218. }
  219. $addressInfo = isset($params['address'])? $params['address'] : [];
  220. $payImg = isset($params['pay_img'])? $params['pay_img'] : '';
  221. if(empty($addressInfo)){
  222. $this->error = 2075;
  223. return false;
  224. }
  225. if(empty($payImg)){
  226. $this->error = 2074;
  227. return false;
  228. }
  229. $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1,'status'=>1])->first();
  230. $score = isset($memberInfo['score'])? $memberInfo['score'] : 0;
  231. // $scoreRate = isset($memberInfo['score_rate'])? $memberInfo['score_rate'] : 0;
  232. // $scoreRate = $scoreRate>0 && $scoreRate<=1? $scoreRate : 1;
  233. if(empty($memberInfo)){
  234. $this->error = 2019;
  235. return false;
  236. }
  237. if($payScore > $score){
  238. $this->error = 2077;
  239. return false;
  240. }
  241. // 所需积分
  242. /*$needScore = round($total/$scoreRate, 0);
  243. if($payScore<$needScore && empty($payImg)){
  244. $this->error = 2076;
  245. return false;
  246. }*/
  247. /*$scorePayMoney = intval($payScore*$scoreRate, 0);
  248. $payMoney = round($total, 0);*/
  249. $payMoney = round($total, 0);
  250. $address = [];
  251. $address[] = isset($addressInfo['province'])? $addressInfo['province'] : '';
  252. $address[] = isset($addressInfo['city'])? $addressInfo['city'] : '';
  253. $address[] = isset($addressInfo['district'])? $addressInfo['district'] : '';
  254. $address[] = isset($addressInfo['address'])? $addressInfo['address'] : '';
  255. $address = array_filter($address);
  256. $data = [
  257. 'pay_img'=> $payImg,
  258. 'pay_time'=> time(),
  259. 'user_address'=> $address? implode(' ', $address) : '',
  260. 'realname'=> isset($addressInfo['realname'])? $addressInfo['realname'] : '',
  261. 'user_mobile'=> isset($addressInfo['mobile'])? $addressInfo['mobile'] : '',
  262. 'pay_score'=> $payScore,
  263. 'pay_money'=> $payMoney,
  264. 'status'=> 2,
  265. 'update_time'=>time()
  266. ];
  267. DB::beginTransaction();
  268. if(!$this->model->where(['id'=> $id])->update($data)){
  269. $this->error = 2079;
  270. Db::rollBack();
  271. return false;
  272. }
  273. if($payScore>0){
  274. if(!MemberModel::where(['id'=>$userId,'mark'=>1])->update(['score'=> max(0, $score - $payScore),'update_time'=>time()])){
  275. $this->error = 2079;
  276. DB::rollBack();
  277. return false;
  278. }
  279. $data = [
  280. 'user_id'=> $userId,
  281. 'shop_id'=>$info['shop_id'],
  282. 'source_order_sn'=> $info['order_sn'],
  283. 'type'=> 1,
  284. 'coin_type'=> 3,
  285. 'money'=> -$payScore,
  286. 'balance'=> $score,
  287. 'create_time'=> time(),
  288. 'update_time'=> time(),
  289. 'remark'=> '积分兑换商品',
  290. 'status'=>1,
  291. 'mark'=>1,
  292. ];
  293. if(!AccountModel::insertGetId($data)){
  294. $this->error = 2079;
  295. DB::rollBack();
  296. return false;
  297. }
  298. }
  299. Db::commit();
  300. $this->error = 2078;
  301. return true;
  302. }
  303. /**
  304. * 已收款
  305. * @return bool
  306. */
  307. public function received()
  308. {
  309. $params = request()->all();
  310. $id = isset($params['id'])? $params['id'] : 0;
  311. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  312. $status = isset($info['status'])? $info['status'] : 0;
  313. if(!$id || empty($info)){
  314. $this->error = 2081;
  315. return false;
  316. }
  317. if($status != 2){
  318. $this->error = 2090;
  319. return false;
  320. }
  321. if($this->model->where(['id'=> $id])->update(['status'=> 3, 'update_time'=> time()])){
  322. $this->error = 2092;
  323. return true;
  324. }else{
  325. $this->error = 2093;
  326. return false;
  327. }
  328. }
  329. /**
  330. * 已发货
  331. * @return bool
  332. */
  333. public function deliver()
  334. {
  335. $params = request()->all();
  336. $id = isset($params['id'])? $params['id'] : 0;
  337. $express = isset($params['express'])? $params['express'] : '';
  338. $expressNo = isset($params['express_no'])? $params['express_no'] : '';
  339. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  340. $status = isset($info['status'])? $info['status'] : 0;
  341. if(!$id || empty($info)){
  342. $this->error = 2081;
  343. return false;
  344. }
  345. if(!in_array($status, [2,3])){
  346. $this->error = 2090;
  347. return false;
  348. }
  349. if($this->model->where(['id'=> $id])->update(['status'=> 4,'express'=> $express,'express_no'=> $expressNo, 'update_time'=> time()])){
  350. $this->error = 2094;
  351. return true;
  352. }else{
  353. $this->error = 2095;
  354. return false;
  355. }
  356. }
  357. /**
  358. * 已收货
  359. * @return bool
  360. */
  361. public function receive()
  362. {
  363. $params = request()->all();
  364. $id = isset($params['id'])? $params['id'] : 0;
  365. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  366. $status = isset($info['status'])? $info['status'] : 0;
  367. if(!$id || empty($info)){
  368. $this->error = 2081;
  369. return false;
  370. }
  371. if(!in_array($status, [3,4,5])){
  372. $this->error = 2082;
  373. return false;
  374. }
  375. if($this->model->where(['id'=> $id])->update(['status'=> 5, 'update_time'=> time()])){
  376. $this->error = 2083;
  377. return true;
  378. }else{
  379. $this->error = 2084;
  380. return false;
  381. }
  382. }
  383. }