Trade.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Session;
  8. use think\Validate;
  9. /**
  10. * 互助接口
  11. */
  12. class Trade extends Api
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = '*';
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. function getfunsorder()
  21. {
  22. $map['status']=3;
  23. $user=get_user_data($this->auth->id);
  24. $uids=db('user')->where(['id'=>['like','%,'.$this->auth->id.',%'],'tdeep'=>['elt',$user['tdeep']+2]])->column('id');
  25. if($uids)
  26. {
  27. $map['relevant_userid']=['in',$uids];
  28. }else{
  29. $map['relevant_userid']=0;
  30. }
  31. $p=$this->request->request('p');
  32. if(empty($p))
  33. {
  34. $this->error('缺少参数页码');
  35. }
  36. $pagesize=10;
  37. $total=db('trade')->where($map)->count();
  38. $totalPage=ceil($total/$pagesize);
  39. if($p>$totalPage && $total>0)
  40. {
  41. $this->error('页码有误');
  42. }
  43. $list=db('trade')->where($map)->page($p,$pagesize)->order('id desc')->select();
  44. foreach ($list as &$v)
  45. {
  46. $v['name']=get_table_column('goods',$v['goodsid'],'title');
  47. $v['ctime']=date('Y-m-d H:i',$v['ctime']);
  48. $v['mobile']=get_user_data($v['userid'],'mobile');
  49. }
  50. if(empty($list))
  51. {
  52. $data['list']=[];
  53. }else{
  54. $data['list']=$list;
  55. }
  56. $data['total']=$totalPage;
  57. $this->success('',$data);
  58. }
  59. function match()
  60. {
  61. $goodsid=$this->request->request("goodsid");
  62. $ginfo=db('goods')->where(['id'=>$goodsid])->find();
  63. if($ginfo['istrade'] == 1)
  64. {
  65. $this->error('已结束');
  66. }else{
  67. $user=get_user_data($this->auth->id);
  68. if(empty($user['bank_number']) && empty($user['wxprc']) &&empty($user['alipayprc']))
  69. {
  70. $this->error('请完善收款账户信息,在交易');
  71. }
  72. // if($user['isreal'] != 2)
  73. // {
  74. // $this->error('您还未实名~');
  75. // }
  76. $addressCnt=db('user_address')->where(['userid'=>$user['id']])->count();
  77. if($addressCnt == 0)
  78. {
  79. $this->error('您还未添加地址~');
  80. }
  81. if($ginfo['userid'] == $this->auth->id)
  82. {
  83. $this->error('您不能买自己的商品~');
  84. }
  85. $topbcf=db('bonus_config')->where(['id'=>5])->find();
  86. $studio=db('studio')->where(['title'=>$this->auth->login_studio])->find();
  87. $topcnt=db('trade')->where(['relevant_userid'=>$user['id'],'stuid'=>$studio['id'],'iscancel'=>0,'create_time'=>strtotime('today'),'type'=>0 ])->count();
  88. if($topcnt>=$topbcf['value'])
  89. {
  90. $this->error($topcnt.'您今日此分商家抢单次数已达上限'.$topbcf['value']);
  91. }
  92. $feeconfig=db('bonus_config')->where(['id'=>2])->find();
  93. $fee=$feeconfig['value']*0.01*$ginfo['price1'];
  94. if ($ginfo['istrade'] == 0){
  95. db()->startTrans();
  96. $tradedata=[
  97. 'orderNo'=>date('YmdHi').rand(100,999),
  98. 'userid'=>$ginfo['userid'],
  99. 'goodsid'=>$goodsid,
  100. 'nums'=>$ginfo['price1'],
  101. 'ctime'=>time(),
  102. 'create_time'=>strtotime('today'),
  103. 'status'=>1,
  104. 'relevant_userid'=>$this->auth->id,
  105. 'fee'=>$fee,
  106. 'remark'=>date('Y-m-d H:i:s'),
  107. 'catid'=>$ginfo['catid'],
  108. 'stuid'=>$ginfo['stuid'],
  109. 'type'=>0,
  110. ];
  111. $tradeid=db('trade')->insertGetId($tradedata);
  112. $res1=db('goods')->where(['id'=>$goodsid])->update(['istrade'=>1]);
  113. if($tradeid && $res1)
  114. {
  115. db()->commit();
  116. $this->success('成功',['id'=>$tradeid]);
  117. }else{
  118. db()->rollback();
  119. $this->error('抢购失败');
  120. }
  121. }else{
  122. $this->error('抢购结束');
  123. }
  124. }
  125. }
  126. function topay()
  127. {
  128. $tradid=$this->request->request("id");
  129. if(empty($tradid))
  130. {
  131. $this->error('网络错误,请重试~');
  132. }
  133. $prc=$this->request->request("prc");
  134. if(empty($prc))
  135. {
  136. $this->error('请上传凭证');
  137. }
  138. $res=db('trade')->where(['id'=>$tradid])->update(['pay_time'=>time(),'status'=>2,'prc'=>$prc]);
  139. if($res)
  140. {
  141. $this->success('支付完成');
  142. }else{
  143. $this->error('支付失败');
  144. }
  145. }
  146. function tocancelappeal()
  147. {
  148. $id=$this->request->request("id");
  149. $trade=db('trade')->where(['id'=>$id,'userid'=>$this->auth->id,'isappeal'=>1,'status'=>-1])->find();
  150. if($trade)
  151. {
  152. $res1=db('trade')->where(['id'=>$id])->update(['isappeal'=>0,'appeal_text'=>'','status'=>2]);
  153. if($res1)
  154. {
  155. $this->success('取消成功');
  156. }else{
  157. $this->error('取消失败');
  158. }
  159. }else{
  160. $this->error('订单不存在,或订单状态已发生改变');
  161. }
  162. }
  163. function tocancel1()
  164. {
  165. $id=$this->request->post("id");
  166. $trade=db('trade')->where(['id'=>$id,'status'=>0])->find();
  167. if(empty($trade))
  168. {
  169. $this->error('订单不存在,或以发生改变');
  170. }else{
  171. $res1=db('trade')->where(['id'=>$id])->update(['status'=>-2,'iscancel'=>1,'confirm_time'=>time()]);
  172. if($res1)
  173. {
  174. $selltrade=db('trade')->where(['goodsid'=>$trade['goodsid'],'status'=>3])->find();
  175. if(empty($selltrade))
  176. {
  177. $res2=db('goods')->where(['id'=>$trade['goodsid']])->update(['istrade'=>0]);
  178. }
  179. $this->success('取消成功');
  180. }else{
  181. $this->error('取消失败');
  182. }
  183. }
  184. }
  185. function tocancel()
  186. {
  187. $id=$this->request->request("id");
  188. $trade=db('trade')->where(['id'=>$id,'status'=>1])->find();
  189. if(empty($trade))
  190. {
  191. $this->error('订单不存在,或以发生改变');
  192. }else{
  193. db()->startTrans();
  194. $res1=db('trade')->where(['id'=>$id])->update(['status'=>-2,'iscancel'=>1,'confirm_time'=>time()]);
  195. $res2=db('goods')->where(['id'=>$trade['goodsid']])->update(['istrade'=>0]);
  196. if($res1 && $res2)
  197. {
  198. db()->commit();
  199. $this->success('取消成功');
  200. }else{
  201. db()->rollback();
  202. $this->error('取消失败');
  203. }
  204. }
  205. }
  206. function toeditorder()
  207. {
  208. $param=$this->request->request();
  209. $trade=db('trade')->where(['id'=>$param['id']])->find();
  210. if(empty($param['buyer']) && $param['status'] == $trade['status'])
  211. {
  212. $this->error('您并未修改订单');
  213. }
  214. db()->startTrans();
  215. if($param['buyer'])
  216. {
  217. $user=db('user')->where(['username|mobile'=>$param['buyer']])->find();
  218. if($user['id'] == $trade['relevant_userid'])
  219. {
  220. unset($param['buyer']);
  221. $res1=1;
  222. $res2=1;
  223. }else{
  224. $res1=db('trade')->where(['id'=>$param['id']])->update(['relevant_userid'=>$user['id']]);
  225. $res2=db('goods')->where(['id'=>$trade['goodsid']])->update(['userid'=>$user['id']]);
  226. }
  227. }
  228. if($param['status'] != $trade['status'])
  229. {
  230. if($param['status'] == 1)
  231. {
  232. $res3=db('trade')->where(['id'=>$param['id']])->update(['status'=>1,'pay_time'=>0,'confirm_time'=>0,'endnums'=>0]);
  233. $res4=1;
  234. }elseif($param['status'] == 2)
  235. {
  236. $res3=db('trade')->where(['id'=>$param['id']])->update(['status'=>2,'pay_time'=>time(),'confirm_time'=>0,'endnums'=>0]);
  237. $res4=1;
  238. }elseif($param['status'] == 3)
  239. {
  240. $bcf=db('bonus_config')->where(['id'=>1])->find();
  241. $endprice=$trade['nums']*$bcf['value']*0.01+$trade['nums'];
  242. $res3=db('trade')->where(['id'=>$param['id']])->update(['status'=>3,'endnums'=>$endprice,'confirm_time'=>time()]);
  243. $res4=db('goods')->where(['id'=>$trade['goodsid']])->update(['userid'=>$trade['relevant_userid'],'price1'=>$endprice]);
  244. }
  245. }else{
  246. $res3=1;
  247. $res4=1;
  248. }
  249. if($res1 && $res2 && $res3 && $res4)
  250. {
  251. db()->commit();
  252. $this->success('修改成功');
  253. }else{
  254. db()->rollback();
  255. $this->error('修改失败');
  256. }
  257. }
  258. function toconfirm()
  259. {
  260. $id=$this->request->request("id");
  261. $tradeinfo=db('trade')->where(['id'=>$id,'status'=>2])->find();
  262. if(empty($tradeinfo))
  263. {
  264. $this->error('订单不存在,或以发生改变');
  265. }else{
  266. $bcf=db('bonus_config')->where(['id'=>1])->find();
  267. $endprice=$tradeinfo['nums']*$bcf['value']*0.01+$tradeinfo['nums'];
  268. db()->startTrans();
  269. $res1=db('trade')->where(['id'=>$id])->update(['confirm_time'=>time(),'endnums'=>$endprice,'status'=>3]);
  270. $res2=db('goods')->where(['id'=>$tradeinfo['goodsid']])->update(['userid'=>$tradeinfo['relevant_userid'],'update_time'=>time(),'price1'=>$endprice]);
  271. if($res1 && $res2)
  272. {
  273. db()->commit();
  274. $this->success('操作完成');
  275. }else{
  276. db()->rollback();
  277. $this->error('操作失败');
  278. }
  279. }
  280. }
  281. /*发货*/
  282. function addorder()
  283. {
  284. $id=$this->request->post('id');
  285. if(empty($id))
  286. {
  287. $this->error('缺少参数');
  288. }
  289. $addressinfo=db('user_address')->where(['userid'=>$this->auth->id])->order('isdefault desc')->find();
  290. if(empty($addressinfo))
  291. {
  292. $this->error("地址不存在,请添加地址");
  293. }
  294. $info=db('trade')->where(['id'=>$id])->find();
  295. if($info['issell']>0)
  296. {
  297. $this->error('不可重复操作');
  298. }
  299. $goods=db('goods')->where(['id'=>$info['goodsid']])->find();
  300. db()->startTrans();
  301. $res=db('trade')->where(['id'=>$id])->update(['issell'=>-1,'ext'=>'发货']);
  302. $data=[
  303. 'orderNo'=>date('YmdHis').rand(100,999),
  304. 'orderStatus'=>1,
  305. 'totalMoney'=>$info['nums'],
  306. 'deliverMoney'=>0,
  307. 'userid'=>$this->auth->id,
  308. 'userName'=>$addressinfo['userName'],
  309. 'userPhone'=>$addressinfo['userPhone'],
  310. 'userAddress'=>$addressinfo['area'].$addressinfo['address'],
  311. 'addressId'=>$addressinfo['id'],
  312. 'remark'=>'',
  313. 'createTime'=>date('Y-m-d H:i:s'),
  314. 'needPay'=>$info['nums'],
  315. 'utime'=>time(),
  316. 'type'=>0
  317. ];
  318. $oids=db('order')->insertGetId($data);
  319. $ordGod=[
  320. 'orderid'=>$oids,
  321. 'goodsid'=>$info['goodsid'],
  322. 'nums'=>1,
  323. 'price'=>$info['nums'],
  324. 'attr'=>'',
  325. 'title'=>$goods['title'],
  326. 'image'=>$goods['image'],
  327. ];
  328. $ogids=db('order_goods')->insertGetId($ordGod);
  329. $logid=db('order_log')->insertGetId(['orderid'=>$oids,'title'=>'订单消息','content'=>'您的订单已生成,请耐心等待发货','userid'=>$this->auth->id,'ctime'=>time()]);
  330. if($res && $oids && $ogids && $logid)
  331. {
  332. db()->commit();
  333. $this->success('操作完成');
  334. }else{
  335. db()->rollback();
  336. $this->error('操作失败');
  337. }
  338. }
  339. /*代售*/
  340. function tosell()
  341. {
  342. $id=$this->request->post('id');
  343. if(empty($id))
  344. {
  345. $this->error('缺少参数');
  346. }
  347. $wtprc=$this->request->post('wtprc');
  348. if(empty($wtprc))
  349. {
  350. $this->error('请上传凭证');
  351. }
  352. $user=get_user_data($this->auth->id);
  353. $info=db('trade')->where(['id'=>$id])->find();
  354. if($info['issell']>0)
  355. {
  356. $this->error('不可重复操作');
  357. }
  358. $res=db('trade')->where(['id'=>$id])->update(['issell'=>1,'wtprc'=>$wtprc]);
  359. if($res)
  360. {
  361. $this->success('操作完成,请耐心等待审核');
  362. }else{
  363. $this->error('操作失败');
  364. }
  365. }
  366. function getonsale()
  367. {
  368. $type = $this->request->post('type');
  369. if($type > 0)
  370. {
  371. $map['issell']=$type;
  372. if($type == 2)
  373. {
  374. $map['status']=['egt',3];
  375. }else{
  376. $map['status']=3;
  377. }
  378. }else{
  379. $map['issell']=0;
  380. $map['status']=['in','1,2'];
  381. }
  382. $map['stuid']=get_user_data($this->auth->id,'stuid');
  383. $p = $this->request->post('p');
  384. $data_list = db('trade')->where($map)->page($p, 10)->order("id desc")->select();
  385. foreach ($data_list as &$v) {
  386. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  387. $v['goods']=$goods;
  388. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  389. $v['sendtime1']=$v['sendtime']>0?date('Y-m-d H:i:s',$v['sendtime']):'';
  390. $v['status_desc']=config('tradeStatus')[$v['status']];
  391. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  392. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  393. if($type == 1)
  394. {
  395. $v['desc']='待审核';
  396. }elseif($type == 2)
  397. {
  398. $v['desc']='已审核';
  399. }else{
  400. $v['desc']='交易中';
  401. }
  402. }
  403. if ($data_list) {
  404. $return['data'] = $data_list;
  405. } else {
  406. $return['data'] = null;
  407. }
  408. $count = db('trade' )->where($map)->count();
  409. $return['total'] = ceil($count / 10);
  410. return $return;
  411. }
  412. function getorders()
  413. {
  414. $map['stuid'] = $this->auth->stuid;
  415. $map['status'] = ['in','1,2,3'];
  416. $p = $this->request->post('p');
  417. $data_list = db('trade')->where($map)->page($p, 10)->order("id desc")->select();
  418. foreach ($data_list as &$v) {
  419. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  420. $v['goods']=$goods;
  421. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  422. $v['pay_time1']=$v['pay_time']>0?date('Y-m-d H:i:s',$v['pay_time']):'';
  423. $v['confirm_time1']=$v['confirm_time']>0?date('Y-m-d H:i:s',$v['confirm_time']):'';
  424. $v['status_desc']=config('tradeStatus')[$v['status']];
  425. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  426. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  427. }
  428. if ($data_list) {
  429. $return['data'] = $data_list;
  430. } else {
  431. $return['data'] = null;
  432. }
  433. $count = db('trade' )->where($map)->count();
  434. $return['total'] = ceil($count / 10);
  435. return $return;
  436. }
  437. function gettj()
  438. {
  439. $sid = $this->request->post('sid');
  440. if($sid>0)
  441. {
  442. $map['stuid']=$sid;
  443. }else{
  444. $map['stuid']=0;
  445. }
  446. $time=$this->request->post('starttime');
  447. if(empty($time))
  448. {
  449. $start=strtotime('today')-60*60*24*5;
  450. $map['ctime']=['gt',$start];
  451. }else{
  452. $start=strtotime($time);
  453. $end=$start+60*60*24;
  454. $map['ctime']=[['egt',$start],['lt',$end]];
  455. }
  456. $p = $this->request->post('p');
  457. $data_list = db('trade')->where($map)->page($p, 10)->order("id desc")->select();
  458. foreach ($data_list as &$v) {
  459. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  460. $v['goods']=$goods;
  461. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  462. $v['confirm_time1']=date('Y-m-d H:i:s',$v['confirm_time']);
  463. $v['status_desc']=config('tradeStatus')[$v['status']];
  464. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  465. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  466. $v['stuname']=get_table_column('studio',$v['stuid'],'name');
  467. }
  468. if ($data_list) {
  469. $return['data'] = $data_list;
  470. } else {
  471. $return['data'] = null;
  472. }
  473. $count = db('trade' )->where($map)->count();
  474. $return['total'] = ceil($count / 10);
  475. return $return;
  476. }
  477. /*我的挂卖*/
  478. function gettradeout()
  479. {
  480. $times=strtotime('today')-60*60*24*2;
  481. $map['ctime']=['gt',$times];
  482. $map['userid'] = $this->auth->id;
  483. $map['status'] = ['egt',3];
  484. $p = $this->request->post('p');
  485. $data_list = db('trade')->where($map)->page($p, 10)->order("id desc")->select();
  486. foreach ($data_list as &$v) {
  487. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  488. $v['goods']=$goods;
  489. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  490. $v['pay_time1']=date('Y-m-d H:i:s',$v['pay_time']);
  491. $v['confirm_time1']=date('Y-m-d H:i:s',$v['confirm_time']);
  492. $v['status_desc']=config('tradeStatus')[$v['status']];
  493. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  494. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  495. $v['flag']=0;
  496. if($v['userid']== $this->auth->id)
  497. {
  498. $v['flag']=1;
  499. }elseif($v['relevant_userid']== $this->auth->id)
  500. {
  501. $v['flag']=2;
  502. }
  503. }
  504. if ($data_list) {
  505. $return['data'] = $data_list;
  506. } else {
  507. $return['data'] = null;
  508. }
  509. $count = db('trade' )->where($map)->count();
  510. $return['total'] = ceil($count / 10);
  511. return $return;
  512. }
  513. function tochange()
  514. {
  515. $params=$this->request->post();
  516. if($params['old'] == $params['new'])
  517. {
  518. $this->error('变更商家不可与原商家一致');
  519. }
  520. /*转商家记录不可重复提交*/
  521. $info=db('studio_code')->where(['goodsid'=>$params['goodsid'],'status'=>1])->find();
  522. if($info)
  523. {
  524. $this->error('此商品新店长还未处理,不可重复操作');
  525. }
  526. $params['userid']=$this->auth->id;
  527. $params['hostid']=$params['new'];
  528. $params['ctime']=time();
  529. $params['status']=1;
  530. $ids=db('studio_code')->insertGetId($params);
  531. if($ids)
  532. {
  533. $this->success('已提交审核,请耐心等待');
  534. }else{
  535. $this->error('提交失败,请重新操作');
  536. }
  537. }
  538. function tosethost()
  539. {
  540. $params=$this->request->post();
  541. $studio=db('studio')->where(['id'=>$params['id']])->find();
  542. if($studio['userid'] != $this->auth->id)
  543. {
  544. $this->error('你还不是店长不可设置商家参数');
  545. }
  546. if(empty($params['title']))
  547. {
  548. unset($params['title']);
  549. }else{
  550. $params['title']=$studio['ident'].$params['title'];
  551. }
  552. $res=db('studio')->where(['id'=>$params['id']])->update($params);
  553. if($res)
  554. {
  555. $this->success('设置成功');
  556. }else{
  557. $this->error('设置失败');
  558. }
  559. }
  560. function todjgoods()
  561. {
  562. $id=$this->request->post('id');
  563. $status=$this->request->post('status');
  564. $info=db('goods')->where(['id'=>$id])->find();
  565. if(empty($info))
  566. {
  567. $this->error('要操作的信息不存在');
  568. }else{
  569. if($status == 1)
  570. {
  571. $res=db('goods')->where(['id'=>$id,'on_sale'=>-1])->update(['on_sale'=>1]);
  572. }else{
  573. $res=db('goods')->where(['id'=>$id,'on_sale'=>1])->update(['on_sale'=>-1]);
  574. }
  575. if($res)
  576. {
  577. $this->success('操作完成');
  578. }else{
  579. $this->error('操作失败');
  580. }
  581. }
  582. }
  583. function toviewchange()
  584. {
  585. $id=$this->request->post('id');
  586. $status=$params=$this->request->post('status');
  587. $info=db('studio_code')->where(['id'=>$id,'status'=>1])->find();
  588. if(empty($info))
  589. {
  590. $this->error('要操作的信息不存在');
  591. }
  592. if($status == 1)
  593. {
  594. db()->startTrans();
  595. $res1=db('studio_code')->where(['id'=>$id])->update(['status'=>2,'utime'=>time()]);
  596. $res2=db('goods')->where(['id'=>$info['goodsid']])->update(['stuid'=>$info['new']]);
  597. if($res1 && $res2)
  598. {
  599. db('trade')->where(['status'=>['neq',4],'goodsid'=>$info['goodsid']])->update(['stuid'=>$info['new']]);
  600. db()->commit();
  601. $this->success('审核完成');
  602. }else{
  603. db()->rollback();
  604. $this->error('审核失败');
  605. }
  606. }else{
  607. $res=db('studio_code')->where(['id'=>$id])->update(['status'=>-1]);
  608. if($res)
  609. {
  610. $this->success('审核完成');
  611. }else{
  612. $this->error('审核失败');
  613. }
  614. }
  615. }
  616. function getmystdgoods()
  617. {
  618. $type = $this->request->post('type');
  619. if($type)
  620. {
  621. $map['on_sale']=$type;
  622. }
  623. $studio=db('studio')->where(['title'=>$this->auth->login_studio])->find();
  624. $map['stuid']=$studio['id'];
  625. $p = $this->request->post('p');
  626. $data_list = db('goods')->where($map)->page($p, 10)->order("id desc")->select();
  627. foreach ($data_list as &$v)
  628. {
  629. $info=db('studio_code')->where(['goodsid'=>$v['id'],'status'=>1])->find();
  630. if($info)
  631. {
  632. $v['iszc']=1;
  633. }else{
  634. $v['iszc']=0;
  635. }
  636. $v['username']=get_user_data($v['userid'],'mobile');
  637. }
  638. if ($data_list) {
  639. $return['data'] = $data_list;
  640. } else {
  641. $return['data'] = null;
  642. }
  643. $count = db('goods' )->where($map)->count();
  644. $return['total'] = ceil($count / 10);
  645. return $return;
  646. }
  647. function gethostdraw()
  648. {
  649. $map['on_sale']=1;
  650. $map['stuid'] = $this->auth->stuid;
  651. $p = $this->request->post('p');
  652. $data_list = db('goods')->where($map)->page($p, 10)->order("id desc")->select();
  653. foreach ($data_list as &$v)
  654. {
  655. $v['username']=get_user_data($v['userid'],'mobile');
  656. }
  657. if ($data_list) {
  658. $return['data'] = $data_list;
  659. } else {
  660. $return['data'] = null;
  661. }
  662. $count = db('goods' )->where($map)->count();
  663. $return['total'] = ceil($count / 10);
  664. return $return;
  665. }
  666. function getchangedraw()
  667. {
  668. $status=$this->request->post('status');
  669. if($status == 1)
  670. {
  671. $map['status']=1;
  672. $map['hostid'] = $this->auth->stuid;
  673. }elseif($status == 2)
  674. {
  675. $map['new'] = $this->auth->stuid;
  676. }else{
  677. $map['old'] = $this->auth->stuid;
  678. }
  679. $p = $this->request->post('p');
  680. $data_list = db('studio_code')->where($map)->page($p, 10)->order("id desc")->select();
  681. foreach ($data_list as &$v)
  682. {
  683. $v['username']=get_user_data($v['userid'],'mobile');
  684. $v['olds']=get_table_column('studio',$v['old'],'title');
  685. $v['news']=get_table_column('studio',$v['new'],'title');
  686. $v['time1']=date('Y-m-d H:i:s',$v['ctime']);
  687. $v['time2']=$v['utime']>0?date('Y-m-d H:i:s',$v['utime']):'';
  688. }
  689. if ($data_list) {
  690. $return['data'] = $data_list;
  691. } else {
  692. $return['data'] = null;
  693. }
  694. $count = db('studio_code' )->where($map)->count();
  695. $return['total'] = ceil($count / 10);
  696. return $return;
  697. }
  698. function getmydraw()
  699. {
  700. $type = $this->request->post('type');
  701. if($type == 1)
  702. {
  703. $map['istrade']=0;
  704. }elseif($type == 2)
  705. {
  706. $map['istrade']=1;
  707. }
  708. $map['on_sale']=1;
  709. $map['userid'] = $this->auth->id;
  710. $p = $this->request->post('p');
  711. $data_list = db('goods')->where($map)->page($p, 10)->order("id desc")->select();
  712. if ($data_list) {
  713. $return['data'] = $data_list;
  714. } else {
  715. $return['data'] = null;
  716. }
  717. $count = db('goods' )->where($map)->count();
  718. $return['total'] = ceil($count / 10);
  719. return $return;
  720. }
  721. /*交易列表*/
  722. function gettradelist()
  723. {
  724. $times=strtotime('today')-60*60*24*2;
  725. $map['ctime']=['gt',$times];
  726. $map['status']=['not in','0,-2'];
  727. $map['userid|relevant_userid'] = $this->auth->id;
  728. $p = $this->request->post('p');
  729. $data_list = db('trade')->where($map)->page($p, 10)->order("id desc")->select();
  730. foreach ($data_list as &$v) {
  731. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  732. $v['goods']=$goods;
  733. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  734. $v['pay_time1']=date('Y-m-d H:i:s',$v['pay_time']);
  735. $v['confirm_time1']=date('Y-m-d H:i:s',$v['confirm_time']);
  736. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  737. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  738. if($v['status'] == 3 && $v['issell'] == 0)
  739. {
  740. $v['status_desc']='交易完成';
  741. }elseif($v['status'] == 3 && $v['issell'] == 1){
  742. $v['status_desc']='代售审核中';
  743. }elseif($v['status'] == 3 && $v['issell'] == 2){
  744. $v['status_desc']='已代售';
  745. }else{
  746. $v['status_desc']=config('tradeStatus')[$v['status']];
  747. }
  748. $v['flag']=0;
  749. if($v['userid']== $this->auth->id)
  750. {
  751. $v['flag']=1;
  752. }elseif($v['relevant_userid']== $this->auth->id)
  753. {
  754. $v['flag']=2;
  755. }
  756. }
  757. if ($data_list) {
  758. $return['data'] = $data_list;
  759. } else {
  760. $return['data'] = null;
  761. }
  762. $count = db('trade' )->where($map)->count();
  763. $return['total'] = ceil($count / 10);
  764. return $return;
  765. }
  766. function toselltrade()
  767. {
  768. $id=$this->request->request('id');
  769. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>1])->find();
  770. if(empty($trade))
  771. {
  772. $this->error('订单不存在,或状态已发生改变');
  773. }
  774. db()->startTrans();
  775. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time()]);
  776. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['issell'=>2,'create_time'=>strtotime('today'),'sendtime'=>time()]);
  777. if($res && $res1)
  778. {
  779. bonusorder($trade['relevant_userid'],$trade['nums']);
  780. addtrade($trade['goodsid']);
  781. db()->commit();
  782. $this->success('审核完成');
  783. }else{
  784. db()->rollback();
  785. $this->error('审核失败');
  786. }
  787. }
  788. function toupdatetrade()
  789. {
  790. $id=$this->request->request('id');
  791. $type=$this->request->request('type');
  792. $isadd=$this->request->request('isadd');
  793. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>0])->find();
  794. if(empty($trade))
  795. {
  796. $this->error('订单不存在,或状态已发生改变');
  797. }
  798. if($type == 1)
  799. {//代售
  800. db()->startTrans();
  801. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['issell'=>1]);
  802. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time()]);
  803. if($res && $res1)
  804. {
  805. db()->commit();
  806. $this->success('代售完成');
  807. }else{
  808. db()->rollback();
  809. $this->error('代售失败');
  810. }
  811. }else{
  812. $addressinfo=db('user_address')->where(['userid'=>$this->auth->id])->order('isdefault desc')->find();
  813. if(empty($addressinfo))
  814. {
  815. $this->error("地址不存在,请添加地址");
  816. }
  817. if($trade['issell']>0)
  818. {
  819. $this->error('已代售不可重复操作');
  820. }
  821. $goods=db('goods')->where(['id'=>$trade['goodsid']])->find();
  822. db()->startTrans();
  823. $res=db('trade')->where(['id'=>$id])->update(['status'=>4,'issell'=>-1,'sendtime'=>time()]);
  824. $data=[
  825. 'orderNo'=>date('YmdHis').rand(100,999),
  826. 'orderStatus'=>1,
  827. 'totalMoney'=>$trade['nums'],
  828. 'deliverMoney'=>0,
  829. 'userid'=>$this->auth->id,
  830. 'userName'=>$addressinfo['userName'],
  831. 'userPhone'=>$addressinfo['userPhone'],
  832. 'userAddress'=>$addressinfo['area'].$addressinfo['address'],
  833. 'addressId'=>$addressinfo['id'],
  834. 'remark'=>'',
  835. 'createTime'=>date('Y-m-d H:i:s'),
  836. 'needPay'=>0,
  837. 'utime'=>time(),
  838. 'type'=>0,
  839. 'tradeid'=>$id,
  840. ];
  841. $oids=db('order')->insertGetId($data);
  842. $ordGod=[
  843. 'orderid'=>$oids,
  844. 'goodsid'=>$trade['goodsid'],
  845. 'nums'=>1,
  846. 'price'=>$trade['nums'],
  847. 'attr'=>'',
  848. 'title'=>$goods['title'],
  849. 'image'=>$goods['image'],
  850. ];
  851. $ogids=db('order_goods')->insertGetId($ordGod);
  852. $logid=db('order_log')->insertGetId(['orderid'=>$oids,'title'=>'订单消息','content'=>'您的订单已生成,请耐心等待发货','userid'=>$this->auth->id,'ctime'=>time()]);
  853. if($res && $oids && $ogids && $logid)
  854. {
  855. db('goods')->where(['id'=>$goods['id']])->update(['on_sale'=>0]);
  856. db()->commit();
  857. $this->success('发货成功');
  858. }else{
  859. db()->rollback();
  860. $this->error('发货失败');
  861. }
  862. }
  863. }
  864. /*转售给平台 add by wes */
  865. function resaletrade()
  866. {
  867. $id=$this->request->request('id');
  868. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>2])->find();
  869. if(empty($trade))
  870. {
  871. $this->error('订单商品不存在,或未上架无法转售');
  872. }
  873. if($trade['on_resale'] == 1) {
  874. $this->error('订单商品已转售');
  875. }
  876. db()->startTrans();
  877. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['on_resale'=>1]);
  878. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time(),'on_resale'=> 1]);
  879. if($res && $res1)
  880. {
  881. db()->commit();
  882. $this->success('转售平台完成');
  883. }else{
  884. db()->rollback();
  885. $this->error('转售平台失败');
  886. }
  887. }
  888. /*申诉*/
  889. function toappeal()
  890. {
  891. $id=$this->request->request('id');
  892. $appeal_text=$this->request->request('appeal_text');
  893. if(empty($appeal_text))
  894. {
  895. $this->error('缺少参数');
  896. }
  897. $res=db('trade')->where(['id'=>$id])->update(['status'=>-1,'isappeal'=>1,'appeal_text'=>$appeal_text]);
  898. if($res)
  899. {
  900. $this->success('申诉提交完成');
  901. }else{
  902. $this->error('申诉失败');
  903. }
  904. }
  905. function toyuyue()
  906. {
  907. $type=$this->request->request('type');
  908. if(empty($type))
  909. {
  910. $this->error('请选择价值范围');
  911. }
  912. $sid=$this->request->request('sid');
  913. if(empty($sid))
  914. {
  915. $this->error('请选择商家');
  916. }else{
  917. $map['stuid']=$sid;
  918. $gmap['stuid']=$sid;
  919. }
  920. $studio=db('studio')->where(['id'=>$sid])->find();
  921. $isappoint=db('studio_user')->where(['sid'=>$studio['id'],'userid'=>$this->auth->id])->find();
  922. if($isappoint)
  923. {
  924. $this->error('预约已关闭');
  925. }
  926. $bcf=db('bonus_config')->where(['id'=>5])->find();
  927. $yuyue_count=db('trade')->where(['stuid'=>$sid,'type'=>1,'status'=>['neq',-2],'relevant_userid'=>$this->auth->id,'appoint_time'=>['gt',strtotime('today')]])->count();
  928. if($yuyue_count >= $bcf['cap'])
  929. {
  930. $this->error('本商家今日预约已达上限');
  931. }
  932. #价格区间
  933. if($type == 5)
  934. {
  935. $map['endnums']=['egt',config('price_range')[$type]];
  936. $gmap['price1']=['egt',config('price_range')[$type]];
  937. }else{
  938. $price_range=explode('-',config('price_range')[$type]);
  939. $map['endnums'][]=['egt',$price_range[0]];
  940. $map['endnums'][]=['lt',$price_range[1]];
  941. $gmap['price1'][]=['egt',$price_range[0]];
  942. $gmap['price1'][]=['lt',$price_range[1]];
  943. }
  944. /*下架的不算*/
  945. $godis =[];
  946. $ggids=db('goods')->where(['on_sale'=>['neq',1]])->column('id');
  947. $ggids2=db('trade')->where(['status'=>0])->column('goodsid');
  948. if($ggids)
  949. {
  950. if($ggids2)
  951. {
  952. $godis=array_merge($ggids,$ggids2);
  953. }else{
  954. $godis=$ggids;
  955. }
  956. $map['goodsid']=['not in',$godis];
  957. $gmap['id']=['not in',$godis];
  958. }else{
  959. if($ggids2)
  960. {
  961. $map['goodsid']=['not in',$ggids2];
  962. $gmap['id']=['not in',$godis];
  963. }
  964. }
  965. $map['relevant_userid']=['neq',$this->auth->id];/*不匹配自身订单*/
  966. $map['status']=3;
  967. $map['issell']=2;
  968. $map['isout']=0;
  969. $info=db('trade')->where($map)->find();
  970. if(empty($info))
  971. {
  972. $gmap['userid']=['neq',$this->auth->id];
  973. $gmap['on_sale']=1;
  974. $gmap['on_resale']=2;
  975. $gmap['istrade']=0;
  976. $gmap['userid']=['neq',$this->auth->id];
  977. $goods=db('goods')->where($gmap)->find();
  978. if(empty($goods))
  979. {
  980. $this->error('没有商品可以预约,请更换价格范围');
  981. }else{
  982. db()->startTrans();
  983. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  984. $tradedata=[
  985. 'orderNo'=>date('YmdHi').rand(100,999),
  986. 'userid'=>$goods['userid'],
  987. 'goodsid'=>$goods['id'],
  988. 'nums'=>$goods['price1'],
  989. 'ctime'=>time(),
  990. 'create_time'=>strtotime('today'),
  991. 'status'=>0,
  992. 'relevant_userid'=>$this->auth->id,
  993. 'fee'=>$goods['price']*$feebcf['value']*0.01,
  994. 'remark'=>date('Y-m-d H:i:s'),
  995. 'catid'=>$goods['catid'],
  996. 'stuid'=>$goods['stuid'],
  997. 'appoint_time'=>time(),
  998. 'type'=>1,
  999. ];
  1000. $tradeid=db('trade')->insertGetId($tradedata);
  1001. $res1=db('goods')->where(['id'=>$goods['id']])->update(['istrade'=>1]);
  1002. if($tradeid && $res1)
  1003. {
  1004. db()->commit();
  1005. $this->success('预约完成');
  1006. }else{
  1007. db()->rollback();
  1008. $this->error('预约失败');
  1009. }
  1010. }
  1011. }else{
  1012. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  1013. $tradedata=[
  1014. 'orderNo'=>date('YmdHi').rand(100,999),
  1015. 'userid'=>$info['relevant_userid'],
  1016. 'goodsid'=>$info['goodsid'],
  1017. 'nums'=>$info['endnums'],
  1018. 'ctime'=>time(),
  1019. 'create_time'=>strtotime('today'),
  1020. 'status'=>0,
  1021. 'relevant_userid'=>$this->auth->id,
  1022. 'fee'=>$info['endnums']*$feebcf['value']*0.01,
  1023. 'remark'=>date('Y-m-d H:i:s'),
  1024. 'catid'=>$info['catid'],
  1025. 'stuid'=>$info['stuid'],
  1026. 'appoint_time'=>time(),
  1027. 'type'=>1,
  1028. ];
  1029. $tradeid=db('trade')->insertGetId($tradedata);
  1030. if($tradeid)
  1031. {
  1032. $this->success('预约完成');
  1033. }else{
  1034. $this->error('预约失败');
  1035. }
  1036. }
  1037. }
  1038. function toownchange()
  1039. {
  1040. $goodsid=$this->request->request('goodsid');
  1041. $goods=db('goods')->where(['id'=>$goodsid])->find();
  1042. if(empty($goods))
  1043. {
  1044. $this->error('要操作的商品不存在');
  1045. }
  1046. $username=$this->request->request('username');
  1047. if(empty($username))
  1048. {
  1049. $this->error('请输入更改会员账户或手机号');
  1050. }
  1051. $user=db('user')->where(['username|mobile'=>$username])->find();
  1052. if($user['id'] == $goods['userid'])
  1053. {
  1054. $this->error('更改的所属人不可与原所属人一致');
  1055. }
  1056. $res=db('goods')->where(['id'=>$goodsid])->update(['userid'=>$user['id']]);
  1057. if($res)
  1058. {
  1059. db('trade')->where(['goodsid'=>$goodsid,'status'=>['in','1,2,3']])->update(['relevant_userid'=>$user['id']]);
  1060. $this->success('更改成功');
  1061. }else{
  1062. $this->error('更改失败');
  1063. }
  1064. }
  1065. function tosongoods()
  1066. {
  1067. $gid=input('id');
  1068. $goodsinfo=db('goods')->where(['id'=>$gid])->find();
  1069. $tradeinfo=db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>['in','1,2,-1']])->find();
  1070. if($tradeinfo)
  1071. {
  1072. $this->error('此商品交易中,不可拆分');
  1073. }
  1074. $tradeinfo1=db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>3,'issell'=>0])->find();
  1075. if($tradeinfo)
  1076. {
  1077. $this->error('此商品还未代售,不可拆分');
  1078. }
  1079. // if($goodsinfo['topprice']<=$goodsinfo['price1'])
  1080. // {
  1081. $sum=0;
  1082. for($i=1;$i<=(int)$goodsinfo['son_nums'];$i++)
  1083. {
  1084. if($i < (int)$goodsinfo['son_nums'])
  1085. {
  1086. $price=round(($goodsinfo['price1']/$goodsinfo['son_nums']),0);
  1087. $sum+=$price;
  1088. }else{
  1089. $price=$goodsinfo['price1']-$sum;
  1090. }
  1091. $data=[
  1092. 'catid'=>$goodsinfo['catid'],
  1093. 'title'=>$goodsinfo['title'].$i,
  1094. 'description'=>$goodsinfo['description'],
  1095. 'content'=>$goodsinfo['content'],
  1096. 'image'=>$goodsinfo['image'],
  1097. 'images'=>$goodsinfo['images'],
  1098. 'on_sale'=>1,
  1099. 'price'=>$price,
  1100. 'price1'=>$price,
  1101. 'topprice'=>$goodsinfo['topprice'],
  1102. 'son_nums'=>$goodsinfo['son_nums'],
  1103. 'utime'=>time(),
  1104. 'isnew'=>1,
  1105. 'istrade'=>0,
  1106. 'stuid'=>$goodsinfo['stuid'],
  1107. 'userid'=>$goodsinfo['userid'],
  1108. ];
  1109. $ids=db('goods')->insertGetId($data);
  1110. }
  1111. db('goods')->where(['id'=>$goodsinfo['id']])->update(['on_sale'=>0,'price1'=>0,'price'=>0]);
  1112. db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>3])->update(['isout'=>1]);
  1113. // }
  1114. $this->success('拆分成功');
  1115. }
  1116. }