Trade.php 42 KB

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