Trade.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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']) && empty($user['usdt_address']) && empty($user['thb_bank_number']) && empty($user['idr_bank_number']))
  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['on_resale']=2;
  651. $map['stuid'] = $this->auth->stuid;
  652. $p = $this->request->post('p');
  653. $data_list = db('goods')->where($map)->page($p, 10)->order("id desc")->select();
  654. foreach ($data_list as &$v)
  655. {
  656. $v['username']=get_user_data($v['userid'],'mobile');
  657. }
  658. if ($data_list) {
  659. $return['data'] = $data_list;
  660. } else {
  661. $return['data'] = null;
  662. }
  663. $count = db('goods' )->where($map)->count();
  664. $return['total'] = ceil($count / 10);
  665. return $return;
  666. }
  667. function getchangedraw()
  668. {
  669. $status=$this->request->post('status');
  670. if($status == 1)
  671. {
  672. $map['status']=1;
  673. $map['hostid'] = $this->auth->stuid;
  674. }elseif($status == 2)
  675. {
  676. $map['new'] = $this->auth->stuid;
  677. }else{
  678. $map['old'] = $this->auth->stuid;
  679. }
  680. $p = $this->request->post('p');
  681. $data_list = db('studio_code')->where($map)->page($p, 10)->order("id desc")->select();
  682. foreach ($data_list as &$v)
  683. {
  684. $v['username']=get_user_data($v['userid'],'mobile');
  685. $v['olds']=get_table_column('studio',$v['old'],'title');
  686. $v['news']=get_table_column('studio',$v['new'],'title');
  687. $v['time1']=date('Y-m-d H:i:s',$v['ctime']);
  688. $v['time2']=$v['utime']>0?date('Y-m-d H:i:s',$v['utime']):'';
  689. }
  690. if ($data_list) {
  691. $return['data'] = $data_list;
  692. } else {
  693. $return['data'] = null;
  694. }
  695. $count = db('studio_code' )->where($map)->count();
  696. $return['total'] = ceil($count / 10);
  697. return $return;
  698. }
  699. function getmydraw()
  700. {
  701. $type = $this->request->post('type');
  702. if($type == 1)
  703. {
  704. $map['istrade']=0;
  705. }elseif($type == 2)
  706. {
  707. $map['istrade']=1;
  708. }
  709. $map['on_sale']=1;
  710. $map['on_resale']=2;
  711. $map['userid'] = $this->auth->id;
  712. $p = $this->request->post('p');
  713. $data_list = db('goods')->where($map)->page($p, 10)->order("id desc")->select();
  714. if ($data_list) {
  715. $return['data'] = $data_list;
  716. } else {
  717. $return['data'] = null;
  718. }
  719. $count = db('goods' )->where($map)->count();
  720. $return['total'] = ceil($count / 10);
  721. return $return;
  722. }
  723. /*交易列表*/
  724. function gettradelist()
  725. {
  726. $times=strtotime('today')-60*60*24*2;
  727. $map['ctime']=['gt',$times];
  728. $map['status']=['not in','0,-2'];
  729. $map['userid|relevant_userid'] = $this->auth->id;
  730. $p = $this->request->post('p');
  731. $data_list = db('trade')->where($map)->page($p, 10)->order("id desc")->select();
  732. foreach ($data_list as &$v) {
  733. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  734. $v['goods']=$goods;
  735. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  736. $v['pay_time1']=date('Y-m-d H:i:s',$v['pay_time']);
  737. $v['confirm_time1']=date('Y-m-d H:i:s',$v['confirm_time']);
  738. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  739. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  740. if($v['status'] == 3 && $v['issell'] == 0)
  741. {
  742. $v['status_desc']='交易完成';
  743. }elseif($v['status'] == 3 && $v['issell'] == 1){
  744. $v['status_desc']='上架审核中';
  745. }elseif($v['status'] == 3 && $v['issell'] == 2){
  746. $v['status_desc']='已上架';
  747. }else{
  748. $v['status_desc']=config('tradeStatus')[$v['status']];
  749. }
  750. $v['flag']=0;
  751. if($v['userid']== $this->auth->id)
  752. {
  753. $v['flag']=1;
  754. }elseif($v['relevant_userid']== $this->auth->id)
  755. {
  756. $v['flag']=2;
  757. }
  758. }
  759. if ($data_list) {
  760. $return['data'] = $data_list;
  761. } else {
  762. $return['data'] = null;
  763. }
  764. $count = db('trade' )->where($map)->count();
  765. $return['total'] = ceil($count / 10);
  766. return $return;
  767. }
  768. function toselltrade()
  769. {
  770. $id=$this->request->request('id');
  771. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>1])->find();
  772. if(empty($trade))
  773. {
  774. $this->error('订单不存在,或状态已发生改变');
  775. }
  776. db()->startTrans();
  777. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time()]);
  778. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['issell'=>2,'create_time'=>strtotime('today'),'sendtime'=>time()]);
  779. if($res && $res1)
  780. {
  781. bonusorder($trade['relevant_userid'],$trade['nums']);
  782. addtrade($trade['goodsid']);
  783. db()->commit();
  784. $this->success('审核完成');
  785. }else{
  786. db()->rollback();
  787. $this->error('审核失败');
  788. }
  789. }
  790. function toupdatetrade()
  791. {
  792. $id=$this->request->request('id');
  793. $type=$this->request->request('type');
  794. $isadd=$this->request->request('isadd');
  795. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>0])->find();
  796. if(empty($trade))
  797. {
  798. $this->error('订单不存在,或状态已发生改变');
  799. }
  800. if($type == 1)
  801. {//代售
  802. db()->startTrans();
  803. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['issell'=>1]);
  804. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time()]);
  805. if($res && $res1)
  806. {
  807. db()->commit();
  808. $this->success('代售完成');
  809. }else{
  810. db()->rollback();
  811. $this->error('代售失败');
  812. }
  813. }else{
  814. $addressinfo=db('user_address')->where(['userid'=>$this->auth->id])->order('isdefault desc')->find();
  815. if(empty($addressinfo))
  816. {
  817. $this->error("地址不存在,请添加地址");
  818. }
  819. if($trade['issell']>0)
  820. {
  821. $this->error('已代售不可重复操作');
  822. }
  823. $goods=db('goods')->where(['id'=>$trade['goodsid']])->find();
  824. db()->startTrans();
  825. $res=db('trade')->where(['id'=>$id])->update(['status'=>4,'issell'=>-1,'sendtime'=>time()]);
  826. $data=[
  827. 'orderNo'=>date('YmdHis').rand(100,999),
  828. 'orderStatus'=>1,
  829. 'totalMoney'=>$trade['nums'],
  830. 'deliverMoney'=>0,
  831. 'userid'=>$this->auth->id,
  832. 'userName'=>$addressinfo['userName'],
  833. 'userPhone'=>$addressinfo['userPhone'],
  834. 'userAddress'=>$addressinfo['area'].$addressinfo['address'],
  835. 'addressId'=>$addressinfo['id'],
  836. 'remark'=>'',
  837. 'createTime'=>date('Y-m-d H:i:s'),
  838. 'needPay'=>0,
  839. 'utime'=>time(),
  840. 'type'=>0,
  841. 'tradeid'=>$id,
  842. ];
  843. $oids=db('order')->insertGetId($data);
  844. $ordGod=[
  845. 'orderid'=>$oids,
  846. 'goodsid'=>$trade['goodsid'],
  847. 'nums'=>1,
  848. 'price'=>$trade['nums'],
  849. 'attr'=>'',
  850. 'title'=>$goods['title'],
  851. 'image'=>$goods['image'],
  852. ];
  853. $ogids=db('order_goods')->insertGetId($ordGod);
  854. $logid=db('order_log')->insertGetId(['orderid'=>$oids,'title'=>'订单消息','content'=>'您的订单已生成,请耐心等待发货','userid'=>$this->auth->id,'ctime'=>time()]);
  855. if($res && $oids && $ogids && $logid)
  856. {
  857. db('goods')->where(['id'=>$goods['id']])->update(['on_sale'=>0]);
  858. db()->commit();
  859. $this->success('发货成功');
  860. }else{
  861. db()->rollback();
  862. $this->error('发货失败');
  863. }
  864. }
  865. }
  866. /*转售给平台 add by wes */
  867. function resaletrade()
  868. {
  869. $id=$this->request->request('id');
  870. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>2])->find();
  871. if(empty($trade))
  872. {
  873. $this->error('订单商品不存在,或未上架无法转售');
  874. }
  875. if($trade['on_resale'] == 1) {
  876. $this->error('订单商品已转售');
  877. }
  878. db()->startTrans();
  879. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['on_resale'=>1,'status'=>4]);
  880. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time(),'on_resale'=> 1]);
  881. if($res && $res1)
  882. {
  883. db()->commit();
  884. $this->success('转售平台完成');
  885. }else{
  886. db()->rollback();
  887. $this->error('转售平台失败');
  888. }
  889. }
  890. /*申诉*/
  891. function toappeal()
  892. {
  893. $id=$this->request->request('id');
  894. $appeal_text=$this->request->request('appeal_text');
  895. if(empty($appeal_text))
  896. {
  897. $this->error('缺少参数');
  898. }
  899. $res=db('trade')->where(['id'=>$id])->update(['status'=>-1,'isappeal'=>1,'appeal_text'=>$appeal_text]);
  900. if($res)
  901. {
  902. $this->success('申诉提交完成');
  903. }else{
  904. $this->error('申诉失败');
  905. }
  906. }
  907. function toyuyue()
  908. {
  909. $type=$this->request->request('type');
  910. if(empty($type))
  911. {
  912. $this->error('请选择价值范围');
  913. }
  914. $sid=$this->request->request('sid');
  915. if(empty($sid))
  916. {
  917. $this->error('请选择商家');
  918. }else{
  919. $map['stuid']=$sid;
  920. $gmap['stuid']=$sid;
  921. }
  922. $studio=db('studio')->where(['id'=>$sid])->find();
  923. $isappoint=db('studio_user')->where(['sid'=>$studio['id'],'userid'=>$this->auth->id])->find();
  924. if($isappoint)
  925. {
  926. $this->error('预约已关闭');
  927. }
  928. $bcf=db('bonus_config')->where(['id'=>5])->find();
  929. $yuyue_count=db('trade')->where(['stuid'=>$sid,'type'=>1,'status'=>['neq',-2],'relevant_userid'=>$this->auth->id,'appoint_time'=>['gt',strtotime('today')]])->count();
  930. if($yuyue_count >= $bcf['cap'])
  931. {
  932. $this->error('本商家今日预约已达上限');
  933. }
  934. #价格区间
  935. if($type == 5)
  936. {
  937. $map['endnums']=['egt',config('price_range')[$type]];
  938. $gmap['price1']=['egt',config('price_range')[$type]];
  939. }else{
  940. $price_range=explode('-',config('price_range')[$type]);
  941. $map['endnums'][]=['egt',$price_range[0]];
  942. $map['endnums'][]=['lt',$price_range[1]];
  943. $gmap['price1'][]=['egt',$price_range[0]];
  944. $gmap['price1'][]=['lt',$price_range[1]];
  945. }
  946. /*下架的不算*/
  947. $godis =[];
  948. $ggids=db('goods')->where(['on_sale'=>['neq',1]])->column('id');
  949. $ggids2=db('trade')->where(['status'=>0])->column('goodsid');
  950. if($ggids)
  951. {
  952. if($ggids2)
  953. {
  954. $godis=array_merge($ggids,$ggids2);
  955. }else{
  956. $godis=$ggids;
  957. }
  958. $map['goodsid']=['not in',$godis];
  959. $gmap['id']=['not in',$godis];
  960. }else{
  961. if($ggids2)
  962. {
  963. $map['goodsid']=['not in',$ggids2];
  964. $gmap['id']=['not in',$godis];
  965. }
  966. }
  967. $map['relevant_userid']=['neq',$this->auth->id];/*不匹配自身订单*/
  968. $map['status']=3;
  969. $map['issell']=2;
  970. $map['isout']=0;
  971. $info=db('trade')->where($map)->find();
  972. if(empty($info))
  973. {
  974. $gmap['userid']=['neq',$this->auth->id];
  975. $gmap['on_sale']=1;
  976. $gmap['on_resale']=2;
  977. $gmap['istrade']=0;
  978. $gmap['userid']=['neq',$this->auth->id];
  979. $goods=db('goods')->where($gmap)->find();
  980. if(empty($goods))
  981. {
  982. $this->error('没有商品可以预约,请更换价格范围');
  983. }else{
  984. db()->startTrans();
  985. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  986. $tradedata=[
  987. 'orderNo'=>date('YmdHi').rand(100,999),
  988. 'userid'=>$goods['userid'],
  989. 'goodsid'=>$goods['id'],
  990. 'nums'=>$goods['price1'],
  991. 'ctime'=>time(),
  992. 'create_time'=>strtotime('today'),
  993. 'status'=>0,
  994. 'relevant_userid'=>$this->auth->id,
  995. 'fee'=>$goods['price']*$feebcf['value']*0.01,
  996. 'remark'=>date('Y-m-d H:i:s'),
  997. 'catid'=>$goods['catid'],
  998. 'stuid'=>$goods['stuid'],
  999. 'appoint_time'=>time(),
  1000. 'type'=>1,
  1001. ];
  1002. $tradeid=db('trade')->insertGetId($tradedata);
  1003. $res1=db('goods')->where(['id'=>$goods['id']])->update(['istrade'=>1]);
  1004. if($tradeid && $res1)
  1005. {
  1006. db()->commit();
  1007. $this->success('预约完成');
  1008. }else{
  1009. db()->rollback();
  1010. $this->error('预约失败');
  1011. }
  1012. }
  1013. }else{
  1014. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  1015. $tradedata=[
  1016. 'orderNo'=>date('YmdHi').rand(100,999),
  1017. 'userid'=>$info['relevant_userid'],
  1018. 'goodsid'=>$info['goodsid'],
  1019. 'nums'=>$info['endnums'],
  1020. 'ctime'=>time(),
  1021. 'create_time'=>strtotime('today'),
  1022. 'status'=>0,
  1023. 'relevant_userid'=>$this->auth->id,
  1024. 'fee'=>$info['endnums']*$feebcf['value']*0.01,
  1025. 'remark'=>date('Y-m-d H:i:s'),
  1026. 'catid'=>$info['catid'],
  1027. 'stuid'=>$info['stuid'],
  1028. 'appoint_time'=>time(),
  1029. 'type'=>1,
  1030. ];
  1031. $tradeid=db('trade')->insertGetId($tradedata);
  1032. if($tradeid)
  1033. {
  1034. $this->success('预约完成');
  1035. }else{
  1036. $this->error('预约失败');
  1037. }
  1038. }
  1039. }
  1040. function toownchange()
  1041. {
  1042. $goodsid=$this->request->request('goodsid');
  1043. $goods=db('goods')->where(['id'=>$goodsid])->find();
  1044. if(empty($goods))
  1045. {
  1046. $this->error('要操作的商品不存在');
  1047. }
  1048. $username=$this->request->request('username');
  1049. if(empty($username))
  1050. {
  1051. $this->error('请输入更改会员账户或手机号');
  1052. }
  1053. $user=db('user')->where(['username|mobile'=>$username])->find();
  1054. if($user['id'] == $goods['userid'])
  1055. {
  1056. $this->error('更改的所属人不可与原所属人一致');
  1057. }
  1058. $res=db('goods')->where(['id'=>$goodsid])->update(['userid'=>$user['id']]);
  1059. if($res)
  1060. {
  1061. db('trade')->where(['goodsid'=>$goodsid,'status'=>['in','1,2,3']])->update(['relevant_userid'=>$user['id']]);
  1062. $this->success('更改成功');
  1063. }else{
  1064. $this->error('更改失败');
  1065. }
  1066. }
  1067. function tosongoods()
  1068. {
  1069. $gid=input('id');
  1070. $goodsinfo=db('goods')->where(['id'=>$gid])->find();
  1071. $tradeinfo=db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>['in','1,2,-1']])->find();
  1072. if($tradeinfo)
  1073. {
  1074. $this->error('此商品交易中,不可拆分');
  1075. }
  1076. $tradeinfo1=db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>3,'issell'=>0])->find();
  1077. if($tradeinfo)
  1078. {
  1079. $this->error('此商品还未代售,不可拆分');
  1080. }
  1081. // if($goodsinfo['topprice']<=$goodsinfo['price1'])
  1082. // {
  1083. $sum=0;
  1084. for($i=1;$i<=(int)$goodsinfo['son_nums'];$i++)
  1085. {
  1086. if($i < (int)$goodsinfo['son_nums'])
  1087. {
  1088. $price=round(($goodsinfo['price1']/$goodsinfo['son_nums']),0);
  1089. $sum+=$price;
  1090. }else{
  1091. $price=$goodsinfo['price1']-$sum;
  1092. }
  1093. $data=[
  1094. 'catid'=>$goodsinfo['catid'],
  1095. 'title'=>$goodsinfo['title'].$i,
  1096. 'description'=>$goodsinfo['description'],
  1097. 'content'=>$goodsinfo['content'],
  1098. 'image'=>$goodsinfo['image'],
  1099. 'images'=>$goodsinfo['images'],
  1100. 'on_sale'=>1,
  1101. 'price'=>$price,
  1102. 'price1'=>$price,
  1103. 'topprice'=>$goodsinfo['topprice'],
  1104. 'son_nums'=>$goodsinfo['son_nums'],
  1105. 'utime'=>time(),
  1106. 'isnew'=>1,
  1107. 'istrade'=>0,
  1108. 'stuid'=>$goodsinfo['stuid'],
  1109. 'userid'=>$goodsinfo['userid'],
  1110. ];
  1111. $ids=db('goods')->insertGetId($data);
  1112. }
  1113. db('goods')->where(['id'=>$goodsinfo['id']])->update(['on_sale'=>0,'price1'=>0,'price'=>0]);
  1114. db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>3])->update(['isout'=>1]);
  1115. // }
  1116. $this->success('拆分成功');
  1117. }
  1118. }