OrderService.php 14 KB

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