Trade.php 42 KB

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