Trade.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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. 'price'=>$ginfo['price'],
  104. 'ctime'=>time(),
  105. 'create_time'=>strtotime('today'),
  106. 'status'=>1,
  107. 'relevant_userid'=>$this->auth->id,
  108. 'fee'=>$fee,
  109. 'pre_price'=> $ginfo['pre_price'],
  110. 'remark'=>date('Y-m-d H:i:s'),
  111. 'catid'=>$ginfo['catid'],
  112. 'stuid'=>$ginfo['stuid'],
  113. 'type'=>0,
  114. ];
  115. $tradeid=db('trade')->insertGetId($tradedata);
  116. $res1=db('goods')->where(['id'=>$goodsid])->update(['istrade'=>1]);
  117. if($tradeid && $res1)
  118. {
  119. db()->commit();
  120. $this->success('成功',['id'=>$tradeid]);
  121. }else{
  122. db()->rollback();
  123. $this->error('抢购失败');
  124. }
  125. }else{
  126. $this->error('抢购结束');
  127. }
  128. }
  129. }
  130. function topay()
  131. {
  132. $tradid=$this->request->request("id");
  133. if(empty($tradid))
  134. {
  135. $this->error('网络错误,请重试~');
  136. }
  137. $prc=$this->request->request("prc");
  138. if(empty($prc))
  139. {
  140. $this->error('请上传凭证');
  141. }
  142. $res=db('trade')->where(['id'=>$tradid])->update(['pay_time'=>time(),'status'=>2,'prc'=>$prc]);
  143. if($res)
  144. {
  145. $this->success('支付完成');
  146. }else{
  147. $this->error('支付失败');
  148. }
  149. }
  150. function tocancelappeal()
  151. {
  152. $id=$this->request->request("id");
  153. $trade=db('trade')->where(['id'=>$id,'userid'=>$this->auth->id,'isappeal'=>1,'status'=>-1])->find();
  154. if($trade)
  155. {
  156. $res1=db('trade')->where(['id'=>$id])->update(['isappeal'=>0,'appeal_text'=>'','status'=>2]);
  157. if($res1)
  158. {
  159. $this->success('取消成功');
  160. }else{
  161. $this->error('取消失败');
  162. }
  163. }else{
  164. $this->error('订单不存在,或订单状态已发生改变');
  165. }
  166. }
  167. function tocancel1()
  168. {
  169. $id=$this->request->post("id");
  170. $trade=db('trade')->where(['id'=>$id,'status'=>0])->find();
  171. if(empty($trade))
  172. {
  173. $this->error('订单不存在,或以发生改变');
  174. }else{
  175. $res1=db('trade')->where(['id'=>$id])->update(['status'=>-2,'iscancel'=>1,'confirm_time'=>time()]);
  176. if($res1)
  177. {
  178. $selltrade=db('trade')->where(['goodsid'=>$trade['goodsid'],'status'=>3])->find();
  179. if(empty($selltrade))
  180. {
  181. $res2=db('goods')->where(['id'=>$trade['goodsid']])->update(['istrade'=>0]);
  182. }
  183. $this->success('取消成功');
  184. }else{
  185. $this->error('取消失败');
  186. }
  187. }
  188. }
  189. function tocancel()
  190. {
  191. $id=$this->request->request("id");
  192. $trade=db('trade')->where(['id'=>$id,'status'=>1])->find();
  193. if(empty($trade))
  194. {
  195. $this->error('订单不存在,或以发生改变');
  196. }else{
  197. db()->startTrans();
  198. $res1=db('trade')->where(['id'=>$id])->update(['status'=>-2,'iscancel'=>1,'confirm_time'=>time()]);
  199. $res2=db('goods')->where(['id'=>$trade['goodsid']])->update(['istrade'=>0]);
  200. if($res1 && $res2)
  201. {
  202. db()->commit();
  203. $this->success('取消成功');
  204. }else{
  205. db()->rollback();
  206. $this->error('取消失败');
  207. }
  208. }
  209. }
  210. function toeditorder()
  211. {
  212. $param=$this->request->request();
  213. $trade=db('trade')->where(['id'=>$param['id']])->find();
  214. if(empty($param['buyer']) && $param['status'] == $trade['status'])
  215. {
  216. $this->error('您并未修改订单');
  217. }
  218. db()->startTrans();
  219. if($param['buyer'])
  220. {
  221. $user=db('user')->where(['username|mobile'=>$param['buyer']])->find();
  222. if($user['id'] == $trade['relevant_userid'])
  223. {
  224. unset($param['buyer']);
  225. $res1=1;
  226. $res2=1;
  227. }else{
  228. $res1=db('trade')->where(['id'=>$param['id']])->update(['relevant_userid'=>$user['id']]);
  229. $res2=db('goods')->where(['id'=>$trade['goodsid']])->update(['userid'=>$user['id']]);
  230. }
  231. }
  232. if($param['status'] != $trade['status'])
  233. {
  234. if($param['status'] == 1)
  235. {
  236. $res3=db('trade')->where(['id'=>$param['id']])->update(['status'=>1,'pay_time'=>0,'confirm_time'=>0,'endnums'=>0]);
  237. $res4=1;
  238. }elseif($param['status'] == 2)
  239. {
  240. $res3=db('trade')->where(['id'=>$param['id']])->update(['status'=>2,'pay_time'=>time(),'confirm_time'=>0,'endnums'=>0]);
  241. $res4=1;
  242. }elseif($param['status'] == 3)
  243. {
  244. $bcf=db('bonus_config')->where(['id'=>1])->find();
  245. $endprice=$trade['nums']*$bcf['value']*0.01+$trade['nums'];
  246. $res3=db('trade')->where(['id'=>$param['id']])->update(['status'=>3,'endnums'=>$endprice,'confirm_time'=>time()]);
  247. $res4=db('goods')->where(['id'=>$trade['goodsid']])->update(['userid'=>$trade['relevant_userid'],'price1'=>$endprice]);
  248. }
  249. }else{
  250. $res3=1;
  251. $res4=1;
  252. }
  253. if($res1 && $res2 && $res3 && $res4)
  254. {
  255. db()->commit();
  256. $this->success('修改成功');
  257. }else{
  258. db()->rollback();
  259. $this->error('修改失败');
  260. }
  261. }
  262. function toconfirm()
  263. {
  264. $id=$this->request->request("id");
  265. $tradeinfo=db('trade')->where(['id'=>$id,'status'=>2])->find();
  266. if(empty($tradeinfo))
  267. {
  268. $this->error('订单不存在,或以发生改变');
  269. }else{
  270. $bcf=db('bonus_config')->where(['id'=>1])->find();
  271. $endprice=$tradeinfo['nums']*$bcf['value']*0.01+$tradeinfo['nums'];
  272. db()->startTrans();
  273. $res1=db('trade')->where(['id'=>$id])->update(['confirm_time'=>time(),'endnums'=>$endprice,'pre_price'=> $endprice-$tradeinfo['price'],'status'=>3]);
  274. $res2=db('goods')->where(['id'=>$tradeinfo['goodsid']])->update(['userid'=>$tradeinfo['relevant_userid'],'update_time'=>time(),'price1'=>$endprice,'pre_price'=> $endprice-$tradeinfo['price']]);
  275. if($res1 && $res2)
  276. {
  277. db()->commit();
  278. $this->success('操作完成');
  279. }else{
  280. db()->rollback();
  281. $this->error('操作失败');
  282. }
  283. }
  284. }
  285. /*发货*/
  286. function addorder()
  287. {
  288. $id=$this->request->post('id');
  289. if(empty($id))
  290. {
  291. $this->error('缺少参数');
  292. }
  293. $addressinfo=db('user_address')->where(['userid'=>$this->auth->id])->order('isdefault desc')->find();
  294. if(empty($addressinfo))
  295. {
  296. $this->error("地址不存在,请添加地址");
  297. }
  298. $info=db('trade')->where(['id'=>$id])->find();
  299. if($info['issell']>0)
  300. {
  301. $this->error('不可重复操作');
  302. }
  303. $goods=db('goods')->where(['id'=>$info['goodsid']])->find();
  304. db()->startTrans();
  305. $res=db('trade')->where(['id'=>$id])->update(['issell'=>-1,'ext'=>'发货']);
  306. $data=[
  307. 'orderNo'=>date('YmdHis').rand(100,999),
  308. 'orderStatus'=>1,
  309. 'totalMoney'=>$info['nums'],
  310. 'deliverMoney'=>0,
  311. 'userid'=>$this->auth->id,
  312. 'userName'=>$addressinfo['userName'],
  313. 'userPhone'=>$addressinfo['userPhone'],
  314. 'userAddress'=>$addressinfo['area'].$addressinfo['address'],
  315. 'addressId'=>$addressinfo['id'],
  316. 'remark'=>'',
  317. 'createTime'=>date('Y-m-d H:i:s'),
  318. 'needPay'=>$info['nums'],
  319. 'utime'=>time(),
  320. 'type'=>0
  321. ];
  322. $oids=db('order')->insertGetId($data);
  323. $ordGod=[
  324. 'orderid'=>$oids,
  325. 'goodsid'=>$info['goodsid'],
  326. 'nums'=>1,
  327. 'price'=>$info['nums'],
  328. 'attr'=>'',
  329. 'title'=>$goods['title'],
  330. 'image'=>$goods['image'],
  331. ];
  332. $ogids=db('order_goods')->insertGetId($ordGod);
  333. $logid=db('order_log')->insertGetId(['orderid'=>$oids,'title'=>'订单消息','content'=>'您的订单已生成,请耐心等待发货','userid'=>$this->auth->id,'ctime'=>time()]);
  334. if($res && $oids && $ogids && $logid)
  335. {
  336. db()->commit();
  337. $this->success('操作完成');
  338. }else{
  339. db()->rollback();
  340. $this->error('操作失败');
  341. }
  342. }
  343. /*代售*/
  344. function tosell()
  345. {
  346. $id=$this->request->post('id');
  347. if(empty($id))
  348. {
  349. $this->error('缺少参数');
  350. }
  351. $wtprc=$this->request->post('wtprc');
  352. if(empty($wtprc))
  353. {
  354. $this->error('请上传凭证');
  355. }
  356. $user=get_user_data($this->auth->id);
  357. $info=db('trade')->where(['id'=>$id])->find();
  358. if($info['issell']>0)
  359. {
  360. $this->error('不可重复操作');
  361. }
  362. $res=db('trade')->where(['id'=>$id])->update(['issell'=>1,'wtprc'=>$wtprc]);
  363. if($res)
  364. {
  365. $this->success('操作完成,请耐心等待审核');
  366. }else{
  367. $this->error('操作失败');
  368. }
  369. }
  370. function getonsale()
  371. {
  372. $type = $this->request->post('type');
  373. if($type > 0)
  374. {
  375. $map['issell']=$type;
  376. if($type == 2)
  377. {
  378. $map['status']=['egt',3];
  379. }else{
  380. $map['status']=3;
  381. }
  382. }else{
  383. $map['issell']=0;
  384. $map['status']=['in','1,2'];
  385. }
  386. $map['stuid']=get_user_data($this->auth->id,'stuid');
  387. $p = $this->request->post('p');
  388. $data_list = db('trade')->where($map)->page($p, 10)->order("id desc")->select();
  389. foreach ($data_list as &$v) {
  390. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  391. $v['goods']=$goods;
  392. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  393. $v['sendtime1']=$v['sendtime']>0?date('Y-m-d H:i:s',$v['sendtime']):'';
  394. $v['status_desc']=config('tradeStatus')[$v['status']];
  395. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  396. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  397. if($type == 1)
  398. {
  399. $v['desc']='待审核';
  400. }elseif($type == 2)
  401. {
  402. $v['desc']='已审核';
  403. }else{
  404. $v['desc']='交易中';
  405. }
  406. }
  407. if ($data_list) {
  408. $return['data'] = $data_list;
  409. } else {
  410. $return['data'] = null;
  411. }
  412. $count = db('trade' )->where($map)->count();
  413. $return['total'] = ceil($count / 10);
  414. return $return;
  415. }
  416. function getorders()
  417. {
  418. $map['stuid'] = $this->auth->stuid;
  419. $map['status'] = ['in','1,2,3'];
  420. $p = $this->request->post('p');
  421. $data_list = db('trade')->where($map)->page($p, 10)->order("status asc,id desc")->select();
  422. foreach ($data_list as &$v) {
  423. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  424. $v['goods']=$goods;
  425. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  426. $v['pay_time1']=$v['pay_time']>0?date('Y-m-d H:i:s',$v['pay_time']):'';
  427. $v['confirm_time1']=$v['confirm_time']>0?date('Y-m-d H:i:s',$v['confirm_time']):'';
  428. $v['status_desc']=config('tradeStatus')[$v['status']];
  429. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  430. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  431. }
  432. if ($data_list) {
  433. $return['data'] = $data_list;
  434. } else {
  435. $return['data'] = null;
  436. }
  437. $count = db('trade' )->where($map)->count();
  438. $return['total'] = ceil($count / 10);
  439. return $return;
  440. }
  441. function gettj()
  442. {
  443. $sid = $this->request->post('sid');
  444. if($sid>0)
  445. {
  446. $map['stuid']=$sid;
  447. }else{
  448. $map['stuid']=0;
  449. }
  450. $time=$this->request->post('starttime');
  451. if(empty($time))
  452. {
  453. $start=strtotime('today')-60*60*24*5;
  454. $map['ctime']=['gt',$start];
  455. }else{
  456. $start=strtotime($time);
  457. $end=$start+60*60*24;
  458. $map['ctime']=[['egt',$start],['lt',$end]];
  459. }
  460. $p = $this->request->post('p');
  461. $data_list = db('trade')->where($map)->page($p, 10)->order("id desc")->select();
  462. foreach ($data_list as &$v) {
  463. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  464. $v['goods']=$goods;
  465. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  466. $v['confirm_time1']=date('Y-m-d H:i:s',$v['confirm_time']);
  467. $v['status_desc']=config('tradeStatus')[$v['status']];
  468. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  469. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  470. $v['stuname']=get_table_column('studio',$v['stuid'],'name');
  471. }
  472. if ($data_list) {
  473. $return['data'] = $data_list;
  474. } else {
  475. $return['data'] = null;
  476. }
  477. $count = db('trade' )->where($map)->count();
  478. $return['total'] = ceil($count / 10);
  479. return $return;
  480. }
  481. /*我的挂卖*/
  482. function gettradeout()
  483. {
  484. $times=strtotime('today')-60*60*24*2;
  485. $userId = $this->auth->id;
  486. $map['ctime']=['gt',$times];
  487. // $map['userid'] = $this->auth->id;
  488. $map['status'] = ['egt',3];
  489. $p = $this->request->post('p');
  490. $data_list = db('trade')->where($map)->where(function($query) use($userId){
  491. $query->where(['userid'=> $userId])->whereOr(function($query) use($userId){
  492. $query->where(['relevant_userid'=> $userId,'on_resale'=>1]);
  493. });
  494. })->page($p, 10)->order("id desc")->select();
  495. //var_dump(db('trade')->getLastSql());
  496. //$speedData = \app\common\model\Trade::getSpeedTotalByUser($userId);
  497. $config = Config::getConfigByGroup('trade');
  498. $releaseRate = isset($config['release_rate'])? floatval($config['release_rate']['value']) : 0;
  499. foreach ($data_list as &$v) {
  500. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  501. $v['goods']=$goods;
  502. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  503. $v['pay_time1']=date('Y-m-d H:i:s',$v['pay_time']);
  504. $v['confirm_time1']=date('Y-m-d H:i:s',$v['confirm_time']);
  505. $v['status_desc']=config('tradeStatus')[$v['status']];
  506. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  507. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  508. $v['release_rate']=$releaseRate;
  509. $v['flag']=0;
  510. if($v['userid']== $this->auth->id)
  511. {
  512. $v['flag']=1;
  513. }elseif($v['relevant_userid']== $this->auth->id)
  514. {
  515. $v['flag']=2;
  516. }
  517. }
  518. if ($data_list) {
  519. $return['data'] = $data_list;
  520. } else {
  521. $return['data'] = null;
  522. }
  523. $count = db('trade' )->where($map)->count();
  524. $return['total'] = ceil($count / 10);
  525. return $return;
  526. }
  527. function tochange()
  528. {
  529. $params=$this->request->post();
  530. if($params['old'] == $params['new'])
  531. {
  532. $this->error('变更商家不可与原商家一致');
  533. }
  534. /*转商家记录不可重复提交*/
  535. $info=db('studio_code')->where(['goodsid'=>$params['goodsid'],'status'=>1])->find();
  536. if($info)
  537. {
  538. $this->error('此商品新店长还未处理,不可重复操作');
  539. }
  540. $params['userid']=$this->auth->id;
  541. $params['hostid']=$params['new'];
  542. $params['ctime']=time();
  543. $params['status']=1;
  544. $ids=db('studio_code')->insertGetId($params);
  545. if($ids)
  546. {
  547. $this->success('已提交审核,请耐心等待');
  548. }else{
  549. $this->error('提交失败,请重新操作');
  550. }
  551. }
  552. function tosethost()
  553. {
  554. $params=$this->request->post();
  555. $studio=db('studio')->where(['id'=>$params['id']])->find();
  556. if($studio['userid'] != $this->auth->id)
  557. {
  558. $this->error('你还不是店长不可设置商家参数');
  559. }
  560. if($params['new_title'])
  561. {
  562. $params['title']=$studio['ident'].$params['new_title'];
  563. }
  564. unset($params['new_title']);
  565. $res=db('studio')->where(['id'=>$params['id']])->update($params);
  566. if($res)
  567. {
  568. if(isset($params['title']) && $params['title']){
  569. \app\common\model\User::where(['id'=> $this->auth->id])->update(['login_studio'=> $params['title']]);
  570. }
  571. $this->success('设置成功');
  572. }else{
  573. $this->error('设置失败');
  574. }
  575. }
  576. function todjgoods()
  577. {
  578. $id=$this->request->post('id');
  579. $status=$this->request->post('status');
  580. $info=db('goods')->where(['id'=>$id])->find();
  581. if(empty($info))
  582. {
  583. $this->error('要操作的信息不存在');
  584. }else{
  585. if($status == 1)
  586. {
  587. $res=db('goods')->where(['id'=>$id,'on_sale'=>-1])->update(['on_sale'=>1]);
  588. }else{
  589. $res=db('goods')->where(['id'=>$id,'on_sale'=>1])->update(['on_sale'=>-1]);
  590. }
  591. if($res)
  592. {
  593. $this->success('操作完成');
  594. }else{
  595. $this->error('操作失败');
  596. }
  597. }
  598. }
  599. function toviewchange()
  600. {
  601. $id=$this->request->post('id');
  602. $status=$params=$this->request->post('status');
  603. $info=db('studio_code')->where(['id'=>$id,'status'=>1])->find();
  604. if(empty($info))
  605. {
  606. $this->error('要操作的信息不存在');
  607. }
  608. if($status == 1)
  609. {
  610. db()->startTrans();
  611. $res1=db('studio_code')->where(['id'=>$id])->update(['status'=>2,'utime'=>time()]);
  612. $res2=db('goods')->where(['id'=>$info['goodsid']])->update(['stuid'=>$info['new']]);
  613. if($res1 && $res2)
  614. {
  615. db('trade')->where(['status'=>['neq',4],'goodsid'=>$info['goodsid']])->update(['stuid'=>$info['new']]);
  616. db()->commit();
  617. $this->success('审核完成');
  618. }else{
  619. db()->rollback();
  620. $this->error('审核失败');
  621. }
  622. }else{
  623. $res=db('studio_code')->where(['id'=>$id])->update(['status'=>-1]);
  624. if($res)
  625. {
  626. $this->success('审核完成');
  627. }else{
  628. $this->error('审核失败');
  629. }
  630. }
  631. }
  632. function getmystdgoods()
  633. {
  634. $type = $this->request->post('type');
  635. if($type)
  636. {
  637. $map['on_sale']=$type;
  638. }
  639. $studio=db('studio')->where(['title'=>$this->auth->login_studio])->find();
  640. $map['stuid']=$studio['id'];
  641. $p = $this->request->post('p');
  642. $data_list = db('goods')->where($map)->page($p, 10)->order("id desc")->select();
  643. foreach ($data_list as &$v)
  644. {
  645. $info=db('studio_code')->where(['goodsid'=>$v['id'],'status'=>1])->find();
  646. if($info)
  647. {
  648. $v['iszc']=1;
  649. }else{
  650. $v['iszc']=0;
  651. }
  652. $v['username']=get_user_data($v['userid'],'mobile');
  653. $v['nickname']=get_user_data($v['userid'],'nickname');
  654. }
  655. if ($data_list) {
  656. $return['data'] = $data_list;
  657. } else {
  658. $return['data'] = null;
  659. }
  660. $count = db('goods' )->where($map)->count();
  661. $return['total'] = ceil($count / 10);
  662. return $return;
  663. }
  664. function gethostdraw()
  665. {
  666. $map['on_sale']=1;
  667. $map['on_resale']=2;
  668. $map['stuid'] = $this->auth->stuid;
  669. $p = $this->request->post('p');
  670. $data_list = db('goods')->where($map)->page($p, 10)->order("id desc")->select();
  671. foreach ($data_list as &$v)
  672. {
  673. $v['username']=get_user_data($v['userid'],'mobile');
  674. }
  675. if ($data_list) {
  676. $return['data'] = $data_list;
  677. } else {
  678. $return['data'] = null;
  679. }
  680. $count = db('goods' )->where($map)->count();
  681. $return['total'] = ceil($count / 10);
  682. return $return;
  683. }
  684. function getchangedraw()
  685. {
  686. $status=$this->request->post('status');
  687. if($status == 1)
  688. {
  689. $map['status']=1;
  690. $map['hostid'] = $this->auth->stuid;
  691. }elseif($status == 2)
  692. {
  693. $map['new'] = $this->auth->stuid;
  694. }else{
  695. $map['old'] = $this->auth->stuid;
  696. }
  697. $p = $this->request->post('p');
  698. $data_list = db('studio_code')->where($map)->page($p, 10)->order("id desc")->select();
  699. foreach ($data_list as &$v)
  700. {
  701. $v['username']=get_user_data($v['userid'],'mobile');
  702. $v['olds']=get_table_column('studio',$v['old'],'title');
  703. $v['news']=get_table_column('studio',$v['new'],'title');
  704. $v['time1']=date('Y-m-d H:i:s',$v['ctime']);
  705. $v['time2']=$v['utime']>0?date('Y-m-d H:i:s',$v['utime']):'';
  706. }
  707. if ($data_list) {
  708. $return['data'] = $data_list;
  709. } else {
  710. $return['data'] = null;
  711. }
  712. $count = db('studio_code' )->where($map)->count();
  713. $return['total'] = ceil($count / 10);
  714. return $return;
  715. }
  716. function getmydraw()
  717. {
  718. $type = $this->request->post('type');
  719. if($type == 1)
  720. {
  721. $map['istrade']=0;
  722. }elseif($type == 2)
  723. {
  724. $map['istrade']=1;
  725. }
  726. $map['on_sale']=1;
  727. $map['on_resale']=2;
  728. $map['userid'] = $this->auth->id;
  729. $p = $this->request->post('p');
  730. $data_list = db('goods')->where($map)->page($p, 10)->order("id desc")->select();
  731. if ($data_list) {
  732. $return['data'] = $data_list;
  733. } else {
  734. $return['data'] = null;
  735. }
  736. $count = db('goods' )->where($map)->count();
  737. $return['total'] = ceil($count / 10);
  738. return $return;
  739. }
  740. /*交易列表*/
  741. function gettradelist()
  742. {
  743. $loginStu = db('studio')->where(['title' => $this->auth->login_studio])->find();
  744. $times=strtotime('today')-60*60*24*2;
  745. $map['ctime']=['gt',$times];
  746. $map['status']=['not in','0,-2'];
  747. $map['stuid'] = isset($loginStu['id'])? $loginStu['id'] : 0;
  748. $map['userid|relevant_userid'] = $this->auth->id;
  749. $p = $this->request->post('p');
  750. $data_list = db('trade')->where($map)->page($p, 10)->order("status asc,id desc")->select();
  751. foreach ($data_list as &$v) {
  752. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  753. $v['goods']=$goods? $goods : ['image'=>''];
  754. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  755. $v['pay_time1']=date('Y-m-d H:i:s',$v['pay_time']);
  756. $v['confirm_time1']=date('Y-m-d H:i:s',$v['confirm_time']);
  757. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  758. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  759. $v['flag']=0;
  760. if($v['userid']== $this->auth->id)
  761. {
  762. $v['flag']=1;
  763. }elseif($v['relevant_userid']== $this->auth->id)
  764. {
  765. $v['flag']=2;
  766. }
  767. if($v['status'] == 3 && $v['issell'] == 0)
  768. {
  769. $v['status_desc']='待上架';
  770. }elseif($v['status'] == 3 && $v['issell'] == 1){
  771. $v['status_desc']='待茶室长确认';
  772. }elseif($v['status'] == 3 && $v['issell'] == 2){
  773. $v['status_desc']='已上架';
  774. }else{
  775. if($v['status'] == 1 && $v['flag']==1){
  776. $v['status_desc']='等待买方支付';
  777. }else if($v['status'] == 1 && $v['flag']==2){
  778. $v['status_desc']='待付款';
  779. }
  780. else if($v['status'] == 2 && $v['flag']==1){
  781. $v['status_desc']='待收款';
  782. }
  783. else if($v['status'] == 2 && $v['flag']==2){
  784. $v['status_desc']='等待卖方确认';
  785. }else{
  786. $v['status_desc']=config('tradeStatus')[$v['status']];
  787. }
  788. }
  789. }
  790. if ($data_list) {
  791. $return['data'] = $data_list;
  792. } else {
  793. $return['data'] = null;
  794. }
  795. $count = db('trade' )->where($map)->count();
  796. $return['total'] = ceil($count / 10);
  797. return $return;
  798. }
  799. /*统计交易列表*/
  800. function gettradelisttj()
  801. {
  802. $type = input('type', 0);
  803. $loginStu = db('studio')->where(['title' => $this->auth->login_studio])->find();
  804. $times=strtotime('today')-60*60*24*2;
  805. $map['ctime']=['gt',$times];
  806. $map['stuid'] = isset($loginStu['id'])? $loginStu['id'] : 0;
  807. if($type){
  808. $map['status']= $type;
  809. if($type == 3){
  810. $map['issell'] = [['egt',0],['elt',2]];
  811. }
  812. }else{
  813. $map['status']=['not in','0,-2'];
  814. }
  815. $p = $this->request->post('p');
  816. $data_list = db('trade')->where($map)->page($p, 10)->order("status asc,id desc")->select();
  817. foreach ($data_list as &$v) {
  818. $goods=db('goods')->where(['id'=>$v['goodsid']])->find();
  819. $v['goods']=$goods? $goods : ['image'=>''];
  820. $v['ctime1']=date('Y-m-d H:i:s',$v['ctime']);
  821. $v['pay_time1']=date('Y-m-d H:i:s',$v['pay_time']);
  822. $v['confirm_time1']=date('Y-m-d H:i:s',$v['confirm_time']);
  823. $v['selluser']=get_user_info($v['userid'],'nickname','mobile');
  824. $v['buyuser']=get_user_info($v['relevant_userid'],'nickname','mobile');
  825. $v['studio_name']= isset($loginStu['name'])? $loginStu['name'] : '';
  826. $v['flag']=0;
  827. if($v['userid']== $this->auth->id)
  828. {
  829. $v['flag']=1;
  830. }elseif($v['relevant_userid']== $this->auth->id)
  831. {
  832. $v['flag']=2;
  833. }
  834. if($v['status'] == 3 && $v['issell'] == 0)
  835. {
  836. $v['status_desc']='待上架';
  837. }elseif($v['status'] == 3 && $v['issell'] == 1){
  838. $v['status_desc']='待茶室长确认';
  839. }elseif($v['status'] == 3 && $v['issell'] == 2){
  840. $v['status_desc']='已上架';
  841. }else{
  842. if($v['status'] == 1 && $v['flag']==1){
  843. $v['status_desc']='等待买方支付';
  844. }else if($v['status'] == 1 && $v['flag']==2){
  845. $v['status_desc']='待付款';
  846. }
  847. else if($v['status'] == 2 && $v['flag']==1){
  848. $v['status_desc']='待收款';
  849. }
  850. else if($v['status'] == 2 && $v['flag']==2){
  851. $v['status_desc']='等待卖方确认';
  852. }else{
  853. $v['status_desc']=config('tradeStatus')[$v['status']];
  854. }
  855. }
  856. }
  857. if ($data_list) {
  858. $return['data'] = $data_list;
  859. } else {
  860. $return['data'] = null;
  861. }
  862. $count = db('trade' )->where($map)->count();
  863. $return['total'] = ceil($count / 10);
  864. $datetime = strtotime(date('Y-m-d'));
  865. $return['counts'] = [
  866. 'today_count'=> db('trade' )->where($map)->where('ctime','>=', $datetime)->count(),
  867. 'today_total'=>db('trade' )->where($map)->where('ctime','>=', $datetime)->sum('nums')
  868. ];
  869. return $return;
  870. }
  871. function toselltrade()
  872. {
  873. $id=$this->request->request('id');
  874. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>1])->find();
  875. if(empty($trade))
  876. {
  877. $this->error('订单不存在,或状态已发生改变');
  878. }
  879. db()->startTrans();
  880. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time()]);
  881. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['issell'=>2,'create_time'=>strtotime('today'),'sendtime'=>time()]);
  882. if($res && $res1)
  883. {
  884. bonusorder($trade['relevant_userid'],$trade['nums']);
  885. addtrade($trade['goodsid']);
  886. db()->commit();
  887. $this->success('审核完成');
  888. }else{
  889. db()->rollback();
  890. $this->error('审核失败');
  891. }
  892. }
  893. function toupdatetrade()
  894. {
  895. $id=$this->request->request('id');
  896. $type=$this->request->request('type');
  897. $isadd=$this->request->request('isadd');
  898. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>0])->find();
  899. if(empty($trade))
  900. {
  901. $this->error('订单不存在,或状态已发生改变');
  902. }
  903. if($type == 1)
  904. {//代售
  905. db()->startTrans();
  906. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['issell'=>1]);
  907. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time()]);
  908. if($res && $res1)
  909. {
  910. db()->commit();
  911. $this->success('代售完成');
  912. }else{
  913. db()->rollback();
  914. $this->error('代售失败');
  915. }
  916. }else{
  917. $addressinfo=db('user_address')->where(['userid'=>$this->auth->id])->order('isdefault desc')->find();
  918. if(empty($addressinfo))
  919. {
  920. $this->error("地址不存在,请添加地址");
  921. }
  922. if($trade['issell']>0)
  923. {
  924. $this->error('已代售不可重复操作');
  925. }
  926. $goods=db('goods')->where(['id'=>$trade['goodsid']])->find();
  927. db()->startTrans();
  928. $res=db('trade')->where(['id'=>$id])->update(['status'=>4,'issell'=>-1,'sendtime'=>time()]);
  929. $data=[
  930. 'orderNo'=>date('YmdHis').rand(100,999),
  931. 'orderStatus'=>1,
  932. 'totalMoney'=>$trade['nums'],
  933. 'deliverMoney'=>0,
  934. 'userid'=>$this->auth->id,
  935. 'userName'=>$addressinfo['userName'],
  936. 'userPhone'=>$addressinfo['userPhone'],
  937. 'userAddress'=>$addressinfo['area'].$addressinfo['address'],
  938. 'addressId'=>$addressinfo['id'],
  939. 'remark'=>'',
  940. 'createTime'=>date('Y-m-d H:i:s'),
  941. 'needPay'=>0,
  942. 'utime'=>time(),
  943. 'type'=>0,
  944. 'tradeid'=>$id,
  945. ];
  946. $oids=db('order')->insertGetId($data);
  947. $ordGod=[
  948. 'orderid'=>$oids,
  949. 'goodsid'=>$trade['goodsid'],
  950. 'nums'=>1,
  951. 'price'=>$trade['nums'],
  952. 'attr'=>'',
  953. 'title'=>$goods['title'],
  954. 'image'=>$goods['image'],
  955. ];
  956. $ogids=db('order_goods')->insertGetId($ordGod);
  957. $logid=db('order_log')->insertGetId(['orderid'=>$oids,'title'=>'订单消息','content'=>'您的订单已生成,请耐心等待发货','userid'=>$this->auth->id,'ctime'=>time()]);
  958. if($res && $oids && $ogids && $logid)
  959. {
  960. db('goods')->where(['id'=>$goods['id']])->update(['on_sale'=>0]);
  961. db()->commit();
  962. $this->success('发货成功');
  963. }else{
  964. db()->rollback();
  965. $this->error('发货失败');
  966. }
  967. }
  968. }
  969. /*转售给平台 add by wes */
  970. function resaletrade()
  971. {
  972. $id=$this->request->request('id');
  973. $trade=db('trade')->where(['id'=>$id,'status'=>3,'issell'=>2])->find();
  974. if(empty($trade))
  975. {
  976. $this->error('订单商品不存在,或未上架无法转售');
  977. }
  978. if($trade['on_resale'] == 1) {
  979. $this->error('订单商品已转售');
  980. }
  981. $studio=db('studio')->where(['title'=>$this->auth->login_studio])->find();
  982. $startTime = isset($studio['start'])? $studio['start'] : 0;
  983. $endTime = isset($studio['end'])? $studio['end'] : 0;
  984. $startTime = $startTime? strtotime(date('Y-m-d').' '.$startTime) : 0;
  985. $endTime = $endTime? strtotime(date('Y-m-d').' '.$endTime) : 0;
  986. $expired = $startTime > time()? $startTime - time() : 0;
  987. if(time()>= $startTime && time() <= $endTime){
  988. $this->error('抱歉,请在营业时间以外转售');
  989. }
  990. db()->startTrans();
  991. $totalUsdt = CoinRate::transfer($trade['pre_price']);
  992. $res=db('trade')->where(['id'=>$id,'status'=>3])->update(['on_resale'=>1,'status'=>4,'release_total_usdt'=> $totalUsdt]);
  993. $res1=db('goods')->where(['id'=>$trade['goodsid']])->update(['time1'=>time(),'on_resale'=> 1]);
  994. if($res && $res1)
  995. {
  996. db()->commit();
  997. $this->success('转售平台完成');
  998. }else{
  999. db()->rollback();
  1000. $this->error('转售平台失败');
  1001. }
  1002. }
  1003. /*申诉*/
  1004. function toappeal()
  1005. {
  1006. $id=$this->request->request('id');
  1007. $appeal_text=$this->request->request('appeal_text');
  1008. if(empty($appeal_text))
  1009. {
  1010. $this->error('缺少参数');
  1011. }
  1012. $res=db('trade')->where(['id'=>$id])->update(['status'=>-1,'isappeal'=>1,'appeal_text'=>$appeal_text]);
  1013. if($res)
  1014. {
  1015. $this->success('申诉提交完成');
  1016. }else{
  1017. $this->error('申诉失败');
  1018. }
  1019. }
  1020. function toyuyue()
  1021. {
  1022. $type=$this->request->request('type');
  1023. if(empty($type))
  1024. {
  1025. $this->error('请选择价值范围');
  1026. }
  1027. $sid=$this->request->request('sid');
  1028. if(empty($sid))
  1029. {
  1030. $this->error('请选择商家');
  1031. }else{
  1032. $map['stuid']=$sid;
  1033. $gmap['stuid']=$sid;
  1034. }
  1035. $studio=db('studio')->where(['id'=>$sid])->find();
  1036. $isappoint=db('studio_user')->where(['sid'=>$studio['id'],'userid'=>$this->auth->id])->find();
  1037. if($isappoint)
  1038. {
  1039. $this->error('预约已关闭');
  1040. }
  1041. $bcf=db('bonus_config')->where(['id'=>5])->find();
  1042. // $yuyue_count=db('trade')->where(['stuid'=>$sid,'type'=>1,'status'=>['neq',-2],'relevant_userid'=>$this->auth->id,'appoint_time'=>['gt',strtotime('today')]])->count();
  1043. // if($yuyue_count >= $bcf['cap'])
  1044. // {
  1045. // $this->error('本商家今日预约已达上限');
  1046. // }
  1047. #价格区间
  1048. $appointPrice = '';
  1049. if($type == 5)
  1050. {
  1051. $map['endnums']=['egt',config('price_range')[$type]];
  1052. $gmap['price1']=['egt',config('price_range')[$type]];
  1053. $appointPrice = '大于'.config('price_range')[$type];
  1054. }else{
  1055. $price_range=explode('-',config('price_range')[$type]);
  1056. $map['endnums'][]=['egt',$price_range[0]];
  1057. $map['endnums'][]=['lt',$price_range[1]];
  1058. $gmap['price1'][]=['egt',$price_range[0]];
  1059. $gmap['price1'][]=['lt',$price_range[1]];
  1060. $appointPrice = "¥{$price_range[0]}~$price_range[1]";
  1061. }
  1062. /*下架的不算*/
  1063. $godis =[];
  1064. $ggids=db('goods')->where(['on_sale'=>['neq',1]])->column('id');
  1065. // 过滤自身已预约中和预约成功的订单商品
  1066. $ggids2=db('trade')->where(['status'=>0,'relevant_userid'=> $this->auth->id])->whereIn('yuyue_status',[1,2])->column('goodsid');
  1067. if($ggids)
  1068. {
  1069. if($ggids2)
  1070. {
  1071. $godis=array_merge($ggids,$ggids2);
  1072. }else{
  1073. $godis=$ggids;
  1074. }
  1075. $map['goodsid']=['not in',$godis];
  1076. $gmap['id']=['not in',$godis];
  1077. }else{
  1078. if($ggids2)
  1079. {
  1080. $map['goodsid']=['not in',$ggids2];
  1081. $gmap['id']=['not in',$godis];
  1082. }
  1083. }
  1084. $map['relevant_userid']=['neq',$this->auth->id];/*不匹配自身订单*/
  1085. $map['status']=3;
  1086. $map['issell']=2;
  1087. $map['isout']=0;
  1088. $info=db('trade')->where($map)->find();
  1089. if(empty($info))
  1090. {
  1091. $gmap['on_sale']=1;
  1092. $gmap['on_resale']=2;
  1093. $gmap['istrade']=0;
  1094. $gmap['userid']=['neq',$this->auth->id];
  1095. $goods=db('goods')->where($gmap)->find();
  1096. if(empty($goods))
  1097. {
  1098. $this->error('没有商品可以预约,请更换价格范围');
  1099. }else{
  1100. db()->startTrans();
  1101. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  1102. $tradedata=[
  1103. 'orderNo'=>date('YmdHi').rand(100,999),
  1104. 'userid'=>$goods['userid'],
  1105. 'goodsid'=>$goods['id'],
  1106. 'nums'=>$goods['price1'],
  1107. 'pre_price'=>$goods['pre_price'],
  1108. 'ctime'=>time(),
  1109. 'create_time'=>strtotime('today'),
  1110. 'status'=>0,
  1111. 'yuyue_status'=> 1,
  1112. 'appoint_price'=> $appointPrice,
  1113. 'relevant_userid'=>$this->auth->id,
  1114. 'fee'=>$goods['price']*$feebcf['value']*0.01,
  1115. 'remark'=>date('Y-m-d H:i:s'),
  1116. 'catid'=>$goods['catid'],
  1117. 'stuid'=>$goods['stuid'],
  1118. 'appoint_time'=>time(),
  1119. 'type'=>1,
  1120. ];
  1121. $tradeid=db('trade')->insertGetId($tradedata);
  1122. //$res1=db('goods')->where(['id'=>$goods['id']])->update(['istrade'=>1]);
  1123. if($tradeid)
  1124. {
  1125. db()->commit();
  1126. $this->success('预约完成');
  1127. }else{
  1128. db()->rollback();
  1129. $this->error('预约失败');
  1130. }
  1131. }
  1132. }else{
  1133. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  1134. $tradedata=[
  1135. 'orderNo'=>date('YmdHi').rand(100,999),
  1136. 'userid'=>$info['relevant_userid'],
  1137. 'goodsid'=>$info['goodsid'],
  1138. 'nums'=>$info['endnums'],
  1139. 'pre_price'=>$info['pre_price'],
  1140. 'ctime'=>time(),
  1141. 'create_time'=>strtotime('today'),
  1142. 'status'=>0,
  1143. 'yuyue_status'=> 1,
  1144. 'appoint_price'=> $appointPrice,
  1145. 'relevant_userid'=>$this->auth->id,
  1146. 'fee'=>$info['endnums']*$feebcf['value']*0.01,
  1147. 'remark'=>date('Y-m-d H:i:s'),
  1148. 'catid'=>$info['catid'],
  1149. 'stuid'=>$info['stuid'],
  1150. 'appoint_time'=>time(),
  1151. 'type'=>1,
  1152. ];
  1153. $tradeid=db('trade')->insertGetId($tradedata);
  1154. if($tradeid)
  1155. {
  1156. $this->success('预约完成');
  1157. }else{
  1158. $this->error('预约失败');
  1159. }
  1160. }
  1161. }
  1162. function toyuyue1()
  1163. {
  1164. $this->error('功能未开放...');
  1165. $type=$this->request->request('type');
  1166. if(empty($type))
  1167. {
  1168. $this->error('请选择价值范围');
  1169. }
  1170. $sid=$this->request->request('sid');
  1171. if(empty($sid))
  1172. {
  1173. $this->error('请选择商家');
  1174. }else{
  1175. $map['stuid']=$sid;
  1176. $gmap['stuid']=$sid;
  1177. }
  1178. $studio=db('studio')->where(['id'=>$sid])->find();
  1179. $isappoint=db('studio_user')->where(['sid'=>$studio['id'],'userid'=>$this->auth->id])->find();
  1180. if($isappoint)
  1181. {
  1182. $this->error('预约已关闭');
  1183. }
  1184. $bcf=db('bonus_config')->where(['id'=>5])->find();
  1185. // $yuyue_count=db('trade')->where(['stuid'=>$sid,'type'=>1,'status'=>['neq',-2],'relevant_userid'=>$this->auth->id,'appoint_time'=>['gt',strtotime('today')]])->count();
  1186. // if($yuyue_count >= $bcf['cap'])
  1187. // {
  1188. // $this->error('本商家今日预约已达上限');
  1189. // }
  1190. #价格区间
  1191. if($type == 5)
  1192. {
  1193. $map['endnums']=['egt',config('price_range')[$type]];
  1194. $gmap['price1']=['egt',config('price_range')[$type]];
  1195. }else{
  1196. $price_range=explode('-',config('price_range')[$type]);
  1197. $map['endnums'][]=['egt',$price_range[0]];
  1198. $map['endnums'][]=['lt',$price_range[1]];
  1199. $gmap['price1'][]=['egt',$price_range[0]];
  1200. $gmap['price1'][]=['lt',$price_range[1]];
  1201. }
  1202. /*下架的不算*/
  1203. $godis =[];
  1204. $ggids=db('goods')->where(['on_sale'=>['neq',1]])->column('id');
  1205. $ggids2=db('trade')->where(['status'=>0])->column('goodsid');
  1206. if($ggids)
  1207. {
  1208. if($ggids2)
  1209. {
  1210. $godis=array_merge($ggids,$ggids2);
  1211. }else{
  1212. $godis=$ggids;
  1213. }
  1214. $map['goodsid']=['not in',$godis];
  1215. $gmap['id']=['not in',$godis];
  1216. }else{
  1217. if($ggids2)
  1218. {
  1219. $map['goodsid']=['not in',$ggids2];
  1220. $gmap['id']=['not in',$godis];
  1221. }
  1222. }
  1223. $map['relevant_userid']=['neq',$this->auth->id];/*不匹配自身订单*/
  1224. $map['status']=3;
  1225. $map['issell']=2;
  1226. $map['isout']=0;
  1227. $info=db('trade')->where($map)->find();
  1228. if(empty($info))
  1229. {
  1230. $gmap['userid']=['neq',$this->auth->id];
  1231. $gmap['on_sale']=1;
  1232. $gmap['on_resale']=2;
  1233. $gmap['istrade']=0;
  1234. $gmap['userid']=['neq',$this->auth->id];
  1235. $goods=db('goods')->where($gmap)->find();
  1236. if(empty($goods))
  1237. {
  1238. $this->error('没有商品可以预约,请更换价格范围');
  1239. }else{
  1240. db()->startTrans();
  1241. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  1242. $tradedata=[
  1243. 'orderNo'=>date('YmdHi').rand(100,999),
  1244. 'userid'=>$goods['userid'],
  1245. 'goodsid'=>$goods['id'],
  1246. 'nums'=>$goods['price1'],
  1247. 'ctime'=>time(),
  1248. 'create_time'=>strtotime('today'),
  1249. 'status'=>0,
  1250. 'relevant_userid'=>$this->auth->id,
  1251. 'fee'=>$goods['price']*$feebcf['value']*0.01,
  1252. 'remark'=>date('Y-m-d H:i:s'),
  1253. 'catid'=>$goods['catid'],
  1254. 'stuid'=>$goods['stuid'],
  1255. 'appoint_time'=>time(),
  1256. 'type'=>1,
  1257. ];
  1258. $tradeid=db('trade')->insertGetId($tradedata);
  1259. $res1=db('goods')->where(['id'=>$goods['id']])->update(['istrade'=>1]);
  1260. if($tradeid && $res1)
  1261. {
  1262. db()->commit();
  1263. $this->success('预约完成');
  1264. }else{
  1265. db()->rollback();
  1266. $this->error('预约失败');
  1267. }
  1268. }
  1269. }else{
  1270. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  1271. $tradedata=[
  1272. 'orderNo'=>date('YmdHi').rand(100,999),
  1273. 'userid'=>$info['relevant_userid'],
  1274. 'goodsid'=>$info['goodsid'],
  1275. 'nums'=>$info['endnums'],
  1276. 'ctime'=>time(),
  1277. 'create_time'=>strtotime('today'),
  1278. 'status'=>0,
  1279. 'relevant_userid'=>$this->auth->id,
  1280. 'fee'=>$info['endnums']*$feebcf['value']*0.01,
  1281. 'remark'=>date('Y-m-d H:i:s'),
  1282. 'catid'=>$info['catid'],
  1283. 'stuid'=>$info['stuid'],
  1284. 'appoint_time'=>time(),
  1285. 'type'=>1,
  1286. ];
  1287. $tradeid=db('trade')->insertGetId($tradedata);
  1288. if($tradeid)
  1289. {
  1290. $this->success('预约完成');
  1291. }else{
  1292. $this->error('预约失败');
  1293. }
  1294. }
  1295. }
  1296. function toconfirmyuyue(){
  1297. $id=$this->request->request('id');
  1298. $status=$this->request->request('status');
  1299. $tradeinfo=db('trade')->where(['id'=>$id,'status'=>0])->find();
  1300. if(empty($tradeinfo)){
  1301. $this->error('预约记录不存在');
  1302. }
  1303. if($tradeinfo['yuyue_status'] != 1){
  1304. $this->error('预约记录已处理');
  1305. }
  1306. if(!in_array($status,[2,3])){
  1307. $this->error('操作类型错误');
  1308. }
  1309. db()->startTrans();
  1310. $res = db('trade')->where(['id'=>$id,'status'=>0])->update(['yuyue_status'=> $status]);
  1311. $res1=db('goods')->where(['id'=>$tradeinfo['goodsid']])->update(['istrade'=>1]);
  1312. if($res && $res1){
  1313. db()->commit();
  1314. $this->error('审核成功');
  1315. }else{
  1316. db()->rollback();
  1317. $this->error('审核失败');
  1318. }
  1319. }
  1320. function toownchange()
  1321. {
  1322. $goodsid=$this->request->request('goodsid');
  1323. $goods=db('goods')->where(['id'=>$goodsid])->find();
  1324. if(empty($goods))
  1325. {
  1326. $this->error('要操作的商品不存在');
  1327. }
  1328. $username=$this->request->request('username');
  1329. if(empty($username))
  1330. {
  1331. $this->error('请输入更改会员账户或手机号');
  1332. }
  1333. $user=db('user')->where(['username|mobile'=>$username])->find();
  1334. if($user['id'] == $goods['userid'])
  1335. {
  1336. $this->error('更改的所属人不可与原所属人一致');
  1337. }
  1338. $res=db('goods')->where(['id'=>$goodsid])->update(['userid'=>$user['id']]);
  1339. if($res)
  1340. {
  1341. db('trade')->where(['goodsid'=>$goodsid,'status'=>['in','1,2,3']])->update(['relevant_userid'=>$user['id']]);
  1342. $this->success('更改成功');
  1343. }else{
  1344. $this->error('更改失败');
  1345. }
  1346. }
  1347. // 拆分
  1348. function tosongoods()
  1349. {
  1350. $gid=input('id');
  1351. $goodsinfo=db('goods')->where(['id'=>$gid])->find();
  1352. $tradeinfo=db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>['in','1,2,-1']])->find();
  1353. if($tradeinfo)
  1354. {
  1355. $this->error('此商品交易中,不可拆分');
  1356. }
  1357. $tradeinfo1=db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>3,'issell'=>0])->find();
  1358. if($tradeinfo)
  1359. {
  1360. $this->error('此商品还未代售,不可拆分');
  1361. }
  1362. // if($goodsinfo['topprice']<=$goodsinfo['price1'])
  1363. // {
  1364. $sum=0;
  1365. $sumpreprice=0;
  1366. for($i=1;$i<=(int)$goodsinfo['son_nums'];$i++)
  1367. {
  1368. $prePrice = $goodsinfo['price1'] - $goodsinfo['price'];
  1369. if($i < (int)$goodsinfo['son_nums'])
  1370. {
  1371. $price=round(($goodsinfo['price1']/$goodsinfo['son_nums']),0);
  1372. $preprice=$prePrice>0?round(($prePrice/$goodsinfo['son_nums']),0):0;
  1373. $sum+=$price;
  1374. $sumpreprice+=$preprice;
  1375. }else{
  1376. $price=$goodsinfo['price1']-$sum;
  1377. $preprice=$prePrice>0?$prePrice-$sumpreprice:0;
  1378. }
  1379. $data=[
  1380. 'catid'=>$goodsinfo['catid'],
  1381. 'title'=>$goodsinfo['title'].$i,
  1382. 'description'=>$goodsinfo['description']? $goodsinfo['description'].'-'.$i : 'G'.$goodsinfo['id'].'-'.$i,
  1383. 'content'=>$goodsinfo['content'],
  1384. 'image'=>$goodsinfo['image'],
  1385. 'images'=>$goodsinfo['images'],
  1386. 'on_sale'=>1,
  1387. 'price'=>$goodsinfo['price'],
  1388. 'price1'=>$goodsinfo['price']+$preprice,
  1389. 'pre_price'=>$preprice,
  1390. 'topprice'=>$goodsinfo['topprice'],
  1391. 'son_nums'=>$goodsinfo['son_nums'],
  1392. 'utime'=>time(),
  1393. 'isnew'=>1,
  1394. 'istrade'=>0,
  1395. 'stuid'=>$goodsinfo['stuid'],
  1396. 'userid'=>$goodsinfo['userid'],
  1397. ];
  1398. $ids=db('goods')->insertGetId($data);
  1399. }
  1400. db('goods')->where(['id'=>$goodsinfo['id']])->update(['on_sale'=>0,'price1'=>0,'price'=>0,'pre_price'=>0]);
  1401. db('trade')->where(['goodsid'=>$goodsinfo['id'],'status'=>3])->update(['isout'=>1]);
  1402. // }
  1403. $this->success('拆分成功');
  1404. }
  1405. function toassign(){
  1406. $gid=input('id');
  1407. $mobile=input('mobile','');
  1408. if(empty($mobile)){
  1409. $this->error('请填写指派用户手机账号');
  1410. }
  1411. $studio=db('studio')->where(['title'=>$this->auth->login_studio])->find();
  1412. if(time()>=strtotime(date("Y-m-d {$studio['start']}")) && time()<=strtotime(date("Y-m-d {$studio['end']}"))){
  1413. $this->error('请在店铺营业时间外指派');
  1414. }
  1415. $goods=db('goods')->where(['id'=>$gid])->find();
  1416. if(empty($goods) || $goods['on_sale'] != 1){
  1417. $this->error('此商品不存在或未上架,不可指派');
  1418. }
  1419. if($goods['on_resale'] == 1){
  1420. $this->error('此商品已转售,不可指派');
  1421. }
  1422. $tradeinfo=db('trade')->where(['goodsid'=>$gid,'status'=>['in','1,2,-1']])->find();
  1423. if($tradeinfo || $goods['istrade'] != 0 || $tradeinfo['on_resale'] == 1)
  1424. {
  1425. $this->error('此商品交易中,不可指派');
  1426. }
  1427. $tradeinfo1=db('trade')->where(['goodsid'=>$gid,'status'=>3,'issell'=>0])->find();
  1428. if($tradeinfo1)
  1429. {
  1430. $this->error('此商品还未代售,不可指派');
  1431. }
  1432. $userInfo = \app\common\model\User::where(['mobile'=> $mobile,'status'=>1])->find();
  1433. if(empty($userInfo)){
  1434. $this->error('指派用户不存在或不可用,请核对或更换账号');
  1435. }
  1436. if($userInfo['id'] == $this->auth->id){
  1437. //$this->error('抱歉不能指派给自己');
  1438. }
  1439. $feebcf=db('bonus_config')->where(['id'=>2])->find();
  1440. db()->startTrans();
  1441. $tradedata=[
  1442. 'orderNo'=>date('YmdHi').rand(100,999),
  1443. 'userid'=>$goods['userid'],
  1444. 'goodsid'=>$goods['id'],
  1445. 'nums'=>$goods['price1'],
  1446. 'pre_price'=>$goods['pre_price'],
  1447. 'ctime'=>time(),
  1448. 'create_time'=>strtotime('today'),
  1449. 'status'=>1,
  1450. 'relevant_userid'=> $userInfo['id'],
  1451. 'fee'=>$goods['price1']*$feebcf['value']*0.01,
  1452. 'remark'=>date('Y-m-d H:i:s'),
  1453. 'catid'=>$goods['catid'],
  1454. 'stuid'=>$goods['stuid'],
  1455. 'appoint_time'=>time(),
  1456. 'type'=>1,
  1457. ];
  1458. $tradeid=db('trade')->insertGetId($tradedata);
  1459. $res1=db('goods')->where(['id'=>$goods['id']])->update(['istrade'=>1]);
  1460. if($tradeid && $res1)
  1461. {
  1462. db()->commit();
  1463. $this->success('指派成功',['id'=>$tradeid]);
  1464. }else{
  1465. db()->rollback();
  1466. $this->error('指派失败');
  1467. }
  1468. }
  1469. }