GoodsOrder.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use EasyWeChat\Factory;
  5. use think\Session;
  6. /**
  7. * @title 抽奖商品订单
  8. * @controller GoodsOrder
  9. * @group base
  10. */
  11. class GoodsOrder extends Base
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. parent::check_login();
  17. }
  18. /**
  19. * @title 获取订单详情
  20. * @desc 获取订单详情
  21. * @author qc
  22. * @url /api/Goods_order/getOrderDetail
  23. * @method GET
  24. * @header name:Authorization require:1 desc:Token
  25. * @param name:id type:int require:1 default:-- desc:订单id
  26. * @return name:order_no type:string default:-- desc:订单号
  27. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  28. * @return name:price_total type:float default:-- desc:订单金额
  29. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  30. * @return name:pay_no type:string default:-- desc:支付单号
  31. * @return name:create_at type:string default:-- desc:订单创建时间
  32. * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1明信片,2余额)
  33. * @return name:pay_time type:string default:-- desc:支付时间
  34. * @return name:cancel_state type:int default:-- desc:取消状态
  35. * @return name:goods_id type:int default:-- desc:商品id
  36. * @return name:goods_num type:int default:-- desc:商品数量
  37. * @return name:goods_cover type:int default:-- desc:商品封面
  38. */
  39. public function getOrderDetail()
  40. {
  41. $order_id = input('id',0);
  42. $detail = Db::table('goods_order')->find($order_id);
  43. $this->success('获取成功',$detail);
  44. }
  45. /**
  46. * @title 立即购买
  47. * @desc 立即购买
  48. * @author qc
  49. * @url /api/Goods_order/payOrderImmediately
  50. * @method POST
  51. * @header name:Authorization require:1 desc:Token
  52. * @param name:goods_id type:int require:1 default:-- desc:商品id
  53. * @param name:num type:int require:1 default:1 desc:商品数量
  54. * @param name:pay_type type:int require:1 default:1 desc:支付方式(1明信片,2余额)
  55. * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
  56. */
  57. public function payOrderImmediately()
  58. {
  59. $goods_id = input("post.goods_id");
  60. $num = input("post.num",1);
  61. $pay_type = input("post.pay_type",1);
  62. $goods_info = Db::table('store_goods')->where(['id'=>$goods_id,'status'=>1,'is_deleted'=>0])
  63. ->find();
  64. $price_total = bcmul($goods_info['goods_price'],$num,2);
  65. if(empty($goods_info)) $this->error('商品已下架');
  66. //if(!$goods_info['end_time'] || strtotime($goods_info['end_time']) < time()) $this->error('商品售卖时间已结束');
  67. if($goods_info['stock'] < $num) $this->error('商品库存不足');
  68. $user_info = Db::table('store_member')->find($this->uid);
  69. if($price_total <= 0) $this->error('金额有误');
  70. if(!in_array($pay_type,[1,2])) $this->error('支付方式有误!');
  71. if($pay_type == 1 && $user_info['crystal'] < $price_total) $this->error('明信片不足!');
  72. if($pay_type == 2 && $user_info['crystal_cash'] < $price_total) $this->error('余额不足!');
  73. $order_info =[
  74. 'uid'=>$this->uid,
  75. 'order_no' => get_order_sn(),
  76. 'price_total' => $price_total,
  77. 'goods_num' => $num,
  78. 'goods_id' => $goods_id,
  79. 'goods_cover' => $goods_info['cover'],
  80. 'goods_name' => $goods_info['name'],
  81. 'create_at' => date('Y-m-d H:i:s'),
  82. 'pay_type' => $pay_type,
  83. ];
  84. Db::startTrans();
  85. $msg= '';$com=true;
  86. try{
  87. //扣减库存(先扣库存在抽奖)
  88. Db::table('store_goods')->where('id',$goods_id)->setDec('stock',$num);
  89. // 生成订单
  90. Db::table('goods_order')->insert($order_info);
  91. $order_id = Db::getLastInsID();
  92. $order_info['id'] = $order_id;
  93. $goods_info = Db::table('store_goods')->where('id',$goods_id)->find();
  94. // 重新验证库存
  95. if($goods_info['stock'] < 0 ) {
  96. Db::rollback();
  97. $com = false;
  98. $msg = '库存不足';
  99. }else{
  100. // 抽奖设置
  101. $goods_spec = json_decode($goods_info['spec'],true);
  102. $is_clip = 0;
  103. $st_num = bcmul($goods_info['base_stock'],0.7);// 1000
  104. $end_num = bcmul($goods_info['base_stock'],0.2);
  105. if($goods_info['base_stock'] > 30) {
  106. if( $goods_info['stock'] < $st_num && $goods_info['stock'] > $end_num) {
  107. $is_clip = 1;
  108. }
  109. }
  110. // 抽奖
  111. if($goods_info['base_stock'] > 80 ) {
  112. $draw_data = draw_lottery($goods_spec,'store_num',$order_info['goods_num'],$is_clip);
  113. }else{
  114. $draw_data = point_lottery($goods_spec,'store_num',$order_info['goods_num'],$goods_info['base_stock'] - $goods_info['stock']);
  115. }
  116. $magic_data = [];
  117. $magic_day = intval(sysconf('magic_days'));// 获取设置天数
  118. // 扣减商品库存数组 && 魔玩柜数据
  119. foreach ($draw_data as $dk=>$dv) {
  120. $goods_spec[$dk]['store_num'] -= $dv;
  121. $magic_data[] = [
  122. 'order_id' => $order_id,
  123. 'goods_id' => $goods_info['id'],
  124. 'user_id' => $this->uid,
  125. 'create_at' => date('Y-m-d H:i:s'),
  126. 'case_name' => $goods_spec[$dk]['award_name'],
  127. 'case_cover'=> $goods_spec[$dk]['spec_img'],
  128. 'num' => $dv,
  129. 'base_num' => $dv,
  130. 'crystal' => $goods_spec[$dk]['crystal'],
  131. 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
  132. 'past_int' => strtotime("+$magic_day days"),
  133. 'title' => $goods_spec[$dk]['title'],
  134. 'color' => $goods_spec[$dk]['color'],
  135. ];
  136. }
  137. $magic_draw = [];// 待抽将数据
  138. foreach ($magic_data as $mv)
  139. {
  140. for ($i=0;$i< $mv['base_num'];$i++ ) {
  141. $magic_draw[]=[
  142. 'order_id' => $order_info['id'],
  143. 'goods_id' => $goods_info['id'],
  144. 'user_id' => $order_info['uid'],
  145. 'create_at' => date('Y-m-d H:i:s'),
  146. 'status' => 0,
  147. 'award_status' => 1,
  148. 'award_name' => $mv['case_name'],
  149. 'award_cover' => $mv['case_cover'],
  150. 'title' => $mv['title'],
  151. 'color_num' =>$mv['color'],
  152. ];
  153. }
  154. }
  155. // 推荐线奖励 start
  156. if($user_info['pid'])
  157. {
  158. $parent_info = Db::table('store_member')->find($user_info['pid']);
  159. $first_rate = intval(sysconf('first_rebate'));
  160. if($first_rate > 0) {
  161. $first_money = bcmul($order_info['price_total'],$first_rate/100,2);
  162. Db::table('store_member')->where(['id'=>$user_info['pid']])
  163. ->update(['crystal'=>bcadd($parent_info['crystal'],$first_money,2)]);
  164. crystal_log($user_info['pid'],$first_money,'一级推荐线奖励',12,$order_id);
  165. }
  166. if($parent_info['pid'] > 0)
  167. {
  168. $grand = Db::table('store_member')->find($parent_info['pid']);
  169. $second_rate = intval(sysconf('second_rebate'));
  170. if($second_rate > 0) {
  171. $second_money =bcmul($order_info['price_total'],$second_rate/100,2);
  172. Db::table('store_member')->where(['id'=>$grand['pid']])
  173. ->update(['crystal'=>bcadd($grand['crystal'],$second_money,2)]);
  174. crystal_log($parent_info['pid'],$second_money,'二级推荐线奖励',13,$order_id);
  175. }
  176. }
  177. }
  178. // 推荐线奖励 end
  179. // 更新库存 && 规格详情
  180. Db::table('store_goods')->where('id',$goods_info['id'])->update(['spec'=>json_encode($goods_spec)]);
  181. // 魔玩柜
  182. Db::table('magic_case')->insertAll($magic_data);
  183. // 抽奖数据
  184. Db::table('magic_draw')->insertAll($magic_draw);
  185. // 扣除余额
  186. if($pay_type == 1) {
  187. Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal'=>bcsub($user_info['crystal'],$order_info['price_total'],2)]);
  188. crystal_log($this->uid,$order_info['price_total'],'明信片支付',6);
  189. }else{
  190. Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal_cash'=>bcsub($user_info['crystal_cash'],$order_info['price_total'],2)]);
  191. balance_log($this->uid,$order_info['price_total'],'余额支付',6);
  192. }
  193. // 更新订单状态
  194. $pay_no = get_order_sn();
  195. Db::table('goods_order')->where(['uid'=>$this->uid,'id'=>$order_id])
  196. ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1,'pay_no'=>$pay_no]);
  197. // 特殊奖励
  198. $special_award = json_decode($goods_info['special_award'],true);
  199. $special_point = $goods_info['special_point'];
  200. $front_court = $special_award[0];// 前场设置
  201. $back_court = $special_award[1]; // 后场设置
  202. $is_draw = 0;
  203. // 前场未抽奖
  204. if($front_court['is_over'] == 0) {
  205. $front_magic = Db::table('magic_draw')
  206. ->field('id,user_id,order_id')
  207. ->where(['goods_id'=>$goods_info['id'],'status'=>0,'is_special'=>0,'is_last'=>0])
  208. ->limit(0,$special_point)
  209. ->order('id asc')
  210. ->select();
  211. // 前场抽奖次数达到前场抽奖节点
  212. if(count($front_magic) == $special_point) {
  213. $is_draw = 1;
  214. $special_award[0]['is_over'] = 1;// 前场抽奖完成标识
  215. $rand_id = array_rand($front_magic,1);// 中奖记录ids
  216. $front_user = $front_magic[$rand_id]['user_id']; // 中奖会员id
  217. $last_id = $front_magic[count($front_magic)-1]['id'];
  218. // 设置了奖励
  219. if($front_court['goods']) {
  220. // 中奖记录插入魔玩柜
  221. $front_case = [
  222. 'order_id' => $front_magic[$rand_id]['order_id'],
  223. 'goods_id' => $goods_info['id'],
  224. 'user_id' => $front_user,
  225. 'create_at' => date('Y-m-d H:i:s'),
  226. 'num' => 1,
  227. 'base_num' => 1,
  228. 'source' => 1,
  229. 'crystal' => $front_court['crystal'],
  230. 'case_name' => $front_court['goods'],
  231. 'case_cover' =>$front_court['cover'],
  232. 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
  233. 'past_int' => strtotime("+$magic_day days"),
  234. ];
  235. Db::table('magic_case')->insert($front_case);
  236. $special_award1 = [
  237. 'order_id' => $order_info['id'],
  238. 'goods_id' => $goods_info['id'],
  239. 'user_id' => $front_user,
  240. 'create_at' => date('Y-m-d H:i:s'),
  241. 'status' => 1,
  242. 'award_status' => 0,
  243. 'award_name' => $front_court['goods'],
  244. 'award_cover' =>$front_court['cover'],
  245. 'is_special' => 1,
  246. 'title' => $front_court['title'],
  247. 'color_num' => $front_court['color'],
  248. ];
  249. Db::table('magic_draw')->insert($special_award1);
  250. // 生成消息记录
  251. send_user_message($front_user,1,'恭喜您被大奖砸中!获得商品:'.$front_court['goods']);
  252. }
  253. // 更新抽奖记录
  254. Db::table('magic_draw')
  255. ->where('goods_id','=',$goods_info['id'])->where('id','<=', $last_id)
  256. ->where('id','<>',$front_magic[$rand_id]['id'])
  257. ->where('status','=',0)
  258. ->where('is_special','=',0)
  259. ->update(['status'=>1]);
  260. Db::table('magic_draw')
  261. ->where('id','=', $front_magic[$rand_id]['id'])
  262. ->where('status','=',0)
  263. ->where('is_special','=',0)
  264. ->update(['status'=>1,'award_status'=>2]);
  265. }
  266. }
  267. // 后场未抽奖 && 商品售罄
  268. if( $special_award[0]['is_over'] ==1 && $back_court['is_over'] == 0 && $goods_info['stock'] == 0){
  269. $back_num = $goods_info['base_stock'] - $special_point;
  270. $back_magic = Db::table('magic_draw')
  271. ->field('id,user_id,order_id')
  272. ->where(['goods_id'=>$goods_info['id'],'status'=>0,'is_special'=>0,'is_last'=>0])
  273. ->limit(0,$back_num)
  274. ->order('id asc')
  275. ->select();
  276. if($back_num == count($back_magic)) {
  277. $is_draw = 1;
  278. $special_award[1]['is_over'] = 1;// 后场抽奖完成标识
  279. $rand_id = array_rand($back_magic,1);// 中奖记录id
  280. $back_user = $back_magic[$rand_id]['user_id']; // 中奖会员id
  281. if( $back_court['goods']) {
  282. $back_case = [
  283. 'order_id' => $back_magic[$rand_id]['order_id'],
  284. 'goods_id' => $goods_info['id'],
  285. 'user_id' => $back_user,
  286. 'create_at' => date('Y-m-d H:i:s'),
  287. 'num' => 1,
  288. 'base_num' => 1,
  289. 'source' => 2,
  290. 'crystal' => $back_court['crystal'],
  291. 'case_name' => $back_court['goods'],
  292. 'case_cover' =>$back_court['cover'],
  293. 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
  294. 'past_int' => strtotime("+$magic_day days"),
  295. ];
  296. Db::table('magic_case')->insert($back_case);
  297. $special_award2 = [
  298. 'order_id' => $order_info['id'],
  299. 'goods_id' => $goods_info['id'],
  300. 'user_id' => $back_user,
  301. 'create_at' => date('Y-m-d H:i:s'),
  302. 'status' => 1,
  303. 'award_status' => 0,
  304. 'award_name' => $back_court['goods'],
  305. 'award_cover' =>$back_court['cover'],
  306. 'is_special' => 1,
  307. 'title' => $back_court['title'],
  308. 'color_num' => $back_court['color'],
  309. ];
  310. Db::table('magic_draw')->insert($special_award2);
  311. // 生成消息记录
  312. send_user_message($back_user,1,'恭喜您被大奖砸中!!获得商品:'.$back_court['goods']);
  313. }
  314. // 更新抽奖记录
  315. Db::table('magic_draw')
  316. ->where('goods_id','=',$goods_info['id'])->where('status','=', 0)
  317. ->where('id','<>',$back_magic[$rand_id]['id'])
  318. ->where('is_special','=',0)
  319. ->update(['status'=>1]);
  320. Db::table('magic_draw')->where('id','=', $back_magic[$rand_id]['id'])
  321. ->where('is_special','=',0)
  322. ->update(['status'=>1,'award_status'=>3]);
  323. }
  324. // 场终奖励
  325. $last_award = json_decode($goods_info['last_award'],true);
  326. if(!empty($last_award))
  327. {
  328. // 全局奖励
  329. if($last_award[0]['goods']) {
  330. $last_award[0]['is_over'] = 1;
  331. $last_all = rand(0,$goods_info['base_stock']-1);
  332. $last_info = Db::table('magic_draw')
  333. ->where(['goods_id'=>$goods_info['id']])
  334. ->limit($last_all,1)
  335. ->find();
  336. $qj_user = $last_info['user_id'];
  337. $qj_case = [
  338. 'order_id' => $last_info['order_id'],
  339. 'goods_id' => $goods_info['id'],
  340. 'user_id' => $qj_user,
  341. 'create_at' => date('Y-m-d H:i:s'),
  342. 'num' => 1,
  343. 'base_num' => 1,
  344. 'source' => 3,
  345. 'crystal' => $last_award[0]['crystal'],
  346. 'case_name' => $last_award[0]['goods'],
  347. 'case_cover' =>$last_award[0]['cover'],
  348. 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
  349. 'past_int' => strtotime("+$magic_day days"),
  350. ];
  351. Db::table('magic_case')->insert($qj_case);
  352. $last_award1 = [
  353. 'order_id' => $order_info['id'],
  354. 'goods_id' => $goods_info['id'],
  355. 'user_id' => $last_info['user_id'],
  356. 'create_at' => date('Y-m-d H:i:s'),
  357. 'status' => 1,
  358. 'award_status' => 0,
  359. 'award_name' => $last_award[0]['goods'],
  360. 'award_cover' =>$last_award[0]['cover'],
  361. 'is_last' => 1,
  362. 'title' => $last_award[0]['title'],
  363. 'color_num' => $last_award[0]['color'],
  364. ];
  365. Db::table('magic_draw')->insert($last_award1);
  366. }
  367. // W奖励
  368. if($last_award[1]['goods']) {
  369. $last_award[1]['is_over'] = 1;
  370. $w_case = [
  371. 'order_id' => $order_id,
  372. 'goods_id' => $goods_info['id'],
  373. 'user_id' => $this->uid,
  374. 'create_at' => date('Y-m-d H:i:s'),
  375. 'num' => 1,
  376. 'base_num' => 1,
  377. 'source' => 4,
  378. 'crystal' => $last_award[1]['crystal'],
  379. 'case_name' => $last_award[1]['goods'],
  380. 'case_cover' =>$last_award[1]['cover'],
  381. 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
  382. 'past_int' => strtotime("+$magic_day days"),
  383. ];
  384. Db::table('magic_case')->insert($w_case);
  385. $last_award_w = [
  386. 'order_id' => $order_info['id'],
  387. 'goods_id' => $goods_info['id'],
  388. 'user_id' => $this->uid,
  389. 'create_at' => date('Y-m-d H:i:s'),
  390. 'status' => 1,
  391. 'award_status' => 0,
  392. 'award_name' => $last_award[1]['goods'],
  393. 'award_cover' =>$last_award[1]['cover'],
  394. 'is_last' => 1,
  395. 'title' => $last_award[1]['title'],
  396. 'color_num' => $last_award[1]['color'],
  397. ];
  398. Db::table('magic_draw')->insert($last_award_w);
  399. }
  400. }
  401. Db::table('store_goods')->where('id',$goods_info['id'])->update(['last_award'=>json_encode($last_award)]);
  402. }
  403. // 更新商品抽奖详情
  404. if($is_draw) Db::table('store_goods')->where('id',$goods_info['id'])->update(['special_award'=>json_encode($special_award)]);
  405. Db::commit();
  406. }
  407. }catch (\Exception $e){
  408. Db::rollback();
  409. $com=false;
  410. $msg = $e->getMessage();
  411. }
  412. if($com) {
  413. $this->success('ok',['order_id'=>$order_id]);
  414. }else{
  415. $this->error($msg);
  416. }
  417. }
  418. /**
  419. * @title 获取订单魔玩柜(抽奖详情)
  420. * @desc 获取订单列表
  421. * @author qc
  422. * @url /api/Goods_order/getMagicCaseByOrderId
  423. * @method GET
  424. * @header name:Authorization require:1 desc:Token
  425. * @param name:order_id type:int require:1 default:-- desc:订单id
  426. * @return name:num type:int default:0 desc:数据总条数
  427. * @return name:list type:array default:-- desc:数据列表
  428. * @return name:list.order_id type:int default:0 desc:关联的订单id
  429. * @return name:list.create_at type:string default:0 desc:抽中时间
  430. * @return name:list.case_name type:string default:0 desc:名称
  431. * @return name:list.case_cover type:string default:0 desc:封面
  432. * @return name:list.num type:string default:0 desc:数量
  433. * @return name:list.crystal type:float default:0.00 desc:每个明信片价值
  434. * @return name:list.title type:float default:-- desc:标题
  435. */
  436. public function getMagicCaseByOrderId()
  437. {
  438. $order_id = input('order_id');
  439. $list = Db::table('magic_case')
  440. ->field('id,order_id,create_at,case_name,case_cover,num,crystal,title')
  441. ->where(['order_id'=>$order_id,'user_id'=>$this->uid,'source'=>0])->select();
  442. $this->success('ok',['list'=>$list,'num'=>empty($list) ? 0: count($list)]);
  443. }
  444. /**
  445. * @title 取消订单
  446. * @desc 取消订单
  447. * @author qc
  448. * @url /api/Goods_order/cancelOrder
  449. * @method POST
  450. * @header name:Authorization require:1 desc:Token
  451. * @param name:id type:int require:1 default:-- desc:订单id
  452. */
  453. public function cancelOrder()
  454. {
  455. $order_id = input('post.id');
  456. $order_detail = Db::table('goods_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
  457. if(empty($order_detail)) $this->error('订单不存在');
  458. if($order_detail['pay_state'] != 0) $this->error('订单已支付');
  459. if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
  460. Db::startTrans();
  461. try {
  462. //恢复库存
  463. Db::table('store_goods')->where('id',$order_detail['goods_id'])->setInc('stock',$order_detail['goods_num']);
  464. // 取消订单
  465. Db::table('goods_order')
  466. ->where(['id'=>$order_id])
  467. ->update(['cancel_state'=>1,'status'=>9]);
  468. Db::commit();
  469. }catch (\Exception $e){
  470. Db::rollback();
  471. }
  472. $this->success('取消成功');
  473. }
  474. /**
  475. * @title 获取订单列表
  476. * @desc 获取订单列表
  477. * @author qc
  478. * @url /api/Goods_order/getMyOrderList
  479. * @method GET
  480. * @header name:Authorization require:1 desc:Token
  481. * @param name:page type:int : default:1 desc:页数
  482. * @param name:page_num type:int : default:20 desc:每页数
  483. * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消)
  484. * @return name:order_no type:string default:-- desc:订单号
  485. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  486. * @return name:price_total type:float default:-- desc:订单金额
  487. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  488. * @return name:pay_no type:string default:-- desc:支付单号
  489. * @return name:create_at type:string default:-- desc:订单创建时间
  490. * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1明信片,2余额)
  491. * @return name:pay_time type:string default:-- desc:支付时间
  492. * @return name:cancel_state type:int default:-- desc:取消状态
  493. * @return name:goods_id type:int default:-- desc:商品id
  494. * @return name:goods_num type:int default:-- desc:商品数量
  495. * @return name:goods_cover type:int default:-- desc:商品封面
  496. */
  497. public function getMyOrderList()
  498. {
  499. $status = input('status',-1);
  500. $where=[];
  501. if($status > -1) $where['o.status'] = $status;
  502. $where['o.uid'] = $this->uid;
  503. $where['o.is_deleted'] = 0;
  504. $list = Db::table('goods_order o')
  505. ->field('o.id,o.order_no,o.pay_state,o.create_at,pay_time,o.price_total,o.status,pay_type,goods_id,goods_num,goods_cover')
  506. ->where($where)
  507. ->order('o.id desc')
  508. ->limit($this->off_set,$this->page_num)
  509. ->select();
  510. $this->success('获取成功',$list);
  511. }
  512. /**
  513. * @title 获取订单列表
  514. * @desc 获取订单列表
  515. * @author qc
  516. * @url /api/Goods_order/getOrderList
  517. * @method GET
  518. * @header name:Authorization require:1 desc:Token
  519. * @param name:page type:int : default:1 desc:页数
  520. * @param name:page_num type:int : default:20 desc:每页数
  521. * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消)
  522. * @return name:order_no type:string default:-- desc:订单号
  523. * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
  524. * @return name:price_total type:float default:-- desc:订单金额
  525. * @return name:freight type:float default:-- desc:运费
  526. * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
  527. * @return name:pay_no type:string default:-- desc:支付单号
  528. * @return name:create_at type:string default:-- desc:订单创建时间
  529. * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1明信片,2余额)
  530. * @return name:pay_time type:string default:-- desc:支付时间
  531. * @return name:cancel_state type:int default:-- desc:取消状态
  532. * @return name:goods_id type:int default:-- desc:商品id
  533. * @return name:goods_num type:int default:-- desc:商品数量
  534. * @return name:goods_cover type:int default:-- desc:商品封面
  535. * @return name:goods_num type:int default:-- desc:商品数量
  536. * @return name:express_company_title type:string default:-- desc:物流公司
  537. * @return name:express_send_no type:string default:-- desc:物流单号
  538. * @return name:express_send_at type:string default:-- desc:发货时间
  539. * @return name:express_company_id type:string default:-- desc:物流公司id
  540. * @return name:draw_time type:string default:-- desc:抽中时间
  541. * @return name:table_type type:int default:-- desc:1兑换订单2魔玩柜订单
  542. *
  543. */
  544. public function getOrderList()
  545. {
  546. $status = input('status',-1);
  547. $uid = $this->uid;
  548. $field = 'id,uid,order_no,price_total,create_at,pro_info,pay_time,pay_no,pay_state,status,table_type,freight,express_company_title,express_send_no,express_send_at,express_company_id';
  549. $where = $status == -1 ? 'uid = '.$uid.' AND is_deleted = 0':'status = '.$status.' AND uid = '.$uid.' AND is_deleted = 0';
  550. $sql = "select * from (select {$field} FROM magic_order WHERE {$where} union all select {$field} FROM exchange_order WHERE {$where}) as tab order by tab.create_at desc, tab.id desc "." LIMIT $this->off_set,$this->page_num";
  551. $res = Db::query($sql);
  552. if($res){
  553. foreach ($res as &$value){
  554. $value['pro_info'] = json_decode($value['pro_info'],true);
  555. if($value['table_type'] == 1){ //兑换订单
  556. $order_info = Db::name('exchange_order')->field('cl_id,goods_id,goods_name,goods_cover,freight,crystal')->where('id',$value['id'])->find();
  557. foreach ($order_info as $ok=>$ov) {
  558. $value[$ok]=$ov;
  559. }
  560. }else{
  561. // 魔玩柜订单
  562. $order_info = Db::name('magic_order')->field('pay_type,coupon_id')->where('id',$value['id'])->find();
  563. $value['pay_type'] = $order_info['pay_type'];
  564. $value['coupon_id'] = $order_info['coupon_id'];
  565. }
  566. }
  567. }
  568. $this->success('获取成功',$res);
  569. }
  570. /**
  571. * @title 删除订单
  572. * @desc 删除订单
  573. * @author qc
  574. * @url /api/Goods_order/delOrder
  575. * @method POST
  576. * @header name:Authorization require:1 desc:Token
  577. * @param name:id type:int require:1 default:1 desc:订单id
  578. */
  579. public function delOrder()
  580. {
  581. $order_id = input('post.id');
  582. $order_info = Db::table('goods_order')
  583. ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
  584. ->find();
  585. if(empty($order_info)) $this->error('订单不存在');
  586. Db::table('goods_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
  587. $this->success('删除成功');
  588. }
  589. /**
  590. * @title 收藏商品
  591. * @desc 收藏商品
  592. * @author qc
  593. * @url /api/Goods_order/goodsCollect`
  594. * @method POST
  595. * @header name:Authorization require:1 desc:Token
  596. * @param name:goods_id type:int require:1 default:0 desc:商品id
  597. */
  598. public function goodsCollect()
  599. {
  600. $goods_id = input('post.goods_id');
  601. if(!$goods_id) $this->error('请选择商品');
  602. $check_collect = Db::table('goods_collect')
  603. ->where(['goods_id'=>$goods_id,'user_id'=>$this->uid])
  604. ->value('id');
  605. if($check_collect) $this->error('商品已收藏');
  606. Db::table('goods_collect')->insert(['goods_id'=>$goods_id,'user_id'=>$this->uid,'create_at'=>date('Y-m-d H:i:s')]);
  607. $this->success('商品已收藏');
  608. }
  609. /**
  610. * @title 取消收藏
  611. * @desc 取消收藏
  612. * @author qc
  613. * @url /api/Goods_order/cancelCollect
  614. * @method POST
  615. * @header name:Authorization require:1 desc:Token
  616. * @param name:id type:int default:0 desc:收藏记录id(两个参数不能同时为空)
  617. * @param name:goods_id type:int default:0 desc:商品id(两个参数不能同时为空)
  618. */
  619. public function cancelCollect()
  620. {
  621. $id = input('post.id');
  622. $goods_id = input('post.goods_id');
  623. if(!$id && !$goods_id) $this->error('参数错误');
  624. $del_where = [];
  625. $del_where['user_id'] = $this->uid;
  626. if($id) $del_where['id'] = $id;
  627. if($goods_id) $del_where['goods_id'] = $goods_id;
  628. Db::table('goods_collect')->where($del_where)->delete();
  629. $this->success('取消成功');
  630. }
  631. /**
  632. * @title 获取我的收藏列表
  633. * @desc 获取我的收藏列表
  634. * @author qc
  635. * @url /api/Goods_order/getMyGoodsCollect
  636. * @method GET
  637. * @header name:Authorization require:1 desc:Token
  638. * @param name:page type:int default:1 desc:页数
  639. * @param name:page_num type:int default:20 desc:每页数
  640. * @return name:id type:int default:-- desc:收藏记录id
  641. * @return name:goods_id type:int default:-- desc:商品id
  642. * @return name:name type:string default:-- desc:商品名称
  643. * @return name:cover type:string default:-- desc:商品封面图
  644. * @return name:desc type:string default:-- desc:商品简述
  645. * @return name:goods_price type:float default:-- desc:售价
  646. * @return name:stock type:int default:-- desc:剩余库存
  647. * @return name:base_stock type:int default:-- desc:初始库存
  648. */
  649. public function getMyGoodsCollect()
  650. {
  651. $list = Db::table('goods_collect')->alias('c')
  652. ->field('c.id,c.goods_id,c.create_at,g.name,g.cover,g.desc,g.goods_price,g.stock,g.base_stock')
  653. ->join('store_goods g','c.goods_id = g.id','INNER')
  654. ->where(['c.user_id'=>$this->uid])
  655. ->order('c.id desc')
  656. ->limit($this->off_set,$this->page_num)
  657. ->select();
  658. $this->success('获取成功',['list'=>$list]);
  659. }
  660. }