Purchase.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. <?php
  2. namespace app\data\controller\api\auth;
  3. use app\data\controller\api\Auth;
  4. use app\data\controller\api\Data;
  5. use app\data\model\DataCollectionLog;
  6. use app\data\model\DataPoolTalent;
  7. use app\data\model\DataRecruitment;
  8. use app\data\model\DataTeachingKnowledge;
  9. use app\data\model\DataUser;
  10. use app\data\model\DataUserApplyJobInfo;
  11. use app\data\model\DataUserApplyJobInfoChild;
  12. use app\data\model\DataUserLeaveMessage;
  13. use app\data\model\DataUserPurchaseAcceptance;
  14. use app\data\model\ShopCoordination;
  15. use app\data\model\ShopCoordinationOffer;
  16. use app\data\model\ShopCoordinationOfferAmount;
  17. use app\data\model\ShopOcean;
  18. use app\data\model\ShopOceanOffer;
  19. use app\data\model\ShopOceanOfferAmount;
  20. use app\data\model\ShopProduction;
  21. use app\data\model\ShopProductionOffer;
  22. use app\data\model\ShopProductionOfferAmount;
  23. use app\data\model\ShopPurchase;
  24. use app\data\model\ShopPurchaseItem;
  25. use app\data\model\ShopPurchaseOffer;
  26. use app\data\model\ShopPurchaseOfferAmount;
  27. use app\data\model\SystemUser;
  28. use app\data\service\MessageService;
  29. use think\admin\Storage;
  30. use think\db\Query;
  31. use think\exception\HttpResponseException;
  32. use think\admin\Controller;
  33. use hg\apidoc\annotation\Title;
  34. use hg\apidoc\annotation\Method;
  35. use hg\apidoc\annotation\Param;
  36. use hg\apidoc\annotation\Header;
  37. use hg\apidoc\annotation\Returned;
  38. use think\facade\Db;
  39. use think\facade\Validate;
  40. use think\Request;
  41. /**
  42. * @Title("用户端采购订单")
  43. */
  44. class Purchase extends Auth
  45. {
  46. protected $noNeedLogin=['list'];
  47. /**
  48. * @Title("列表")
  49. * @Method("get")
  50. * @Param ("page",desc="第几页")
  51. * @Param ("limit",desc="每页几条")
  52. * @Param ("tab",desc="1采购2生产3外协4海运")
  53. * @Param ("sort",desc="1按距离排序2按价格排序3按发布时间排序")
  54. * @Param ("sort_dir",desc="asc正序,desc倒序")
  55. * @Param ("longitude",desc="当前用户的经度")
  56. * @Param ("latitude",desc="当前用户的纬度")
  57. * @Returned ("order_no",desc="订单号")
  58. * @Returned ("real_name",desc="联系人")
  59. * @Returned ("mobile",desc="联系方式")
  60. * @Returned ("post_address",desc="交货地址")
  61. * @Returned ("post_time",desc="交货时间")
  62. * @Returned ("create_time",desc="发布时间")
  63. * @Returned ("images",desc="图片/视频")
  64. * @Returned ("file",desc="照片/视频")
  65. * @Returned ("file.type",desc="image或video")
  66. * @Returned ("file.src",desc="image或video的链接")
  67. * @Returned ("images",desc="图片视频")
  68. * @Returned ("images.type",desc="image或video")
  69. * @Returned ("images.src",desc="image或video的链接")
  70. * @Returned ("form",desc="包装形式")
  71. * @Returned ("wharf_to",desc="装货码头")
  72. * @Returned ("wharf_for",desc="到港码头")
  73. * @Returned ("standard_tech",desc="生产技术标准")
  74. * @Returned ("standard_check",desc="生产验收标准")
  75. * @Returned ("is_fav",desc="是否已收藏")
  76. * @Returned ("status",desc="1未报价 2已报价 3已匹配'")
  77. * @Returned ("offer_id",desc="匹配到的接单ID")
  78. * @Returned ("items.name",desc="产品名称")
  79. * @Returned ("items.spec",desc="规格")
  80. * @Returned ("items.number",desc="数量")
  81. * @Returned ("items.standard",desc="质量标准")
  82. * @Returned ("items.price",desc="价格")
  83. * @Returned ("items.enclosure",desc="附件")
  84. * @Returned ("items.enclosure.filename",desc="附件文件名")
  85. * @Returned ("items.enclosure.fileurl",desc="附件文件链接")
  86. * @Returned ("items.enclosure.icon",desc="icon图标")
  87. * @Returned ("items.weight",desc="重量")
  88. * @Returned ("items.status",desc="0订单已取消1未报价 2已报价 3已匹配'")
  89. * @Returned ("merchant.company",desc="公司名")
  90. * @Returned ("merchant.address",desc="公司地址")
  91. * @Returned ("user_order",desc="自己的报价信息")
  92. * @Returned ("user_order.amount",desc="最后一次报价金额")
  93. * @Returned ("user_order.amounts",desc="历史报价金额数组")
  94. * @Returned ("user_order.amounts.*.amount",desc="历史报价金额数组金额")
  95. * @Returned ("user_order.date",desc="货期/月")
  96. * @Returned ("user_order.desc",desc="描述")
  97. * @Returned ("user_order.is_have",desc="是否有货")
  98. * @Returned ("user_order.images",desc="图片")
  99. * @Returned ("user_order.create_time",desc="报价日期")
  100. */
  101. public function list(Request $request)
  102. {
  103. $this->_vali([
  104. 'longitude.require'=>'经度必须',
  105. 'latitude.require'=>'纬度必须',
  106. ],'get');
  107. switch ($request['tab']){
  108. case 1:
  109. case 2:
  110. case 3:
  111. case 4:
  112. $query=ShopPurchase::getQuery($request['tab']);
  113. break;
  114. default:
  115. $this->error('类型必须');
  116. }
  117. if($request['sort']==1){
  118. $query
  119. ->field("*,st_distance(point(`longitude`,`latitude`),point({$request['longitude']},{$request['latitude']})) as distance")
  120. ->order('distance',$request['sort_dir']);
  121. }elseif ($request['sort']==2){
  122. $query->order('amount',$request['sort_dir']);
  123. }else{
  124. $query->order('create_time desc');
  125. }
  126. $query->with(['merchant','items']);
  127. $list=$query
  128. ->paginate($request['limit']?:15)->toArray();
  129. if($request['tab'] == 1){
  130. foreach ($list['data'] as $k=>$v){
  131. $count = count($v['items']);
  132. $a = '';
  133. foreach ($v['items'] as $key=>$value){
  134. if($value['is_matched'] == 1){
  135. $a++;
  136. if($a == $count){
  137. unset($list['data'][$k]);
  138. }
  139. }
  140. }
  141. }
  142. $list['data'] = array_values($list['data']);
  143. }
  144. $this->success('',$list);
  145. }
  146. /**
  147. * @Title ("收藏取消收藏")
  148. * @Method("post")
  149. * @Param ("id",desc="订单ID")
  150. * @Param ("tab",desc="1采购2生产3外协4海运")
  151. * @Param ("action",desc="1关注0取消")
  152. */
  153. public function favourite(Request $request){
  154. $this->_vali([
  155. 'id.require'=>'id必须',
  156. 'tab.require'=>'tab必须',
  157. 'tab.in:1,2,3,4'=>'tab有误',
  158. ]);
  159. $model=ShopPurchase::getQuery($request['tab'])->findOrFail($request['id']);
  160. $user=$this->getUser();
  161. if($request['action']) {
  162. $model->favourite()->save([
  163. 'user_id' => $user['id'],
  164. ]);
  165. }else{
  166. $model->favourite()->where('user_id',$user['id'])->delete();
  167. }
  168. $this->success('操作成功');
  169. }
  170. /**
  171. * @Title ("采购订单详情")
  172. * @Param ("id",desc="订单ID")
  173. * @Returned ("参考列表")
  174. */
  175. public function show(Request $request){
  176. $this->_vali([
  177. 'id.require'=>'参数有误',
  178. ],'get');
  179. $model=ShopPurchase::show()->with(['items','merchant'])->findOrFail($request['id']);
  180. $user=$this->getUser();
  181. foreach ($model['items'] as $item){
  182. $item['user_order']=$item->offers()->with(['amounts'=>function($q)use($user){
  183. $q->where('user_id',$user['id']);// }])->where('id',$item['offer_id'])
  184. }])->where('user_id',$user['id'])
  185. ->find();
  186. if($item['offer_id']) {
  187. $item['status'] = 3;
  188. }else{
  189. $item['status'] = $item['user_order']?2:1;
  190. }
  191. }
  192. ShopPurchase::is_fav($model,$user);
  193. $model['huanxinID'] = SystemUser::mk()->where('id',$model['admin_id'])->value('huanxinID');
  194. $this->success('',$model);
  195. }
  196. /**
  197. * @Title ("生产订单详情")
  198. * @Param ("id",desc="订单ID")
  199. * @Returned ("参考列表")
  200. */
  201. public function production_show(Request $request){
  202. $this->_vali([
  203. 'id.require'=>'参数有误',
  204. ],'get');
  205. $model=ShopProduction::show()->with(['items','merchant'])->findOrFail($request['id']);
  206. $user=$this->getUser();
  207. $model['user_order']=$model->offers()->with(['amounts'=>function($q)use($user){$q->where('user_id',$user['id']);}])->where('user_id',$user['id'])->find();
  208. if($model['offer_id']) {
  209. $model['status'] = 3;
  210. }else{
  211. $model['status'] = $model['user_order']?2:1;
  212. }
  213. ShopPurchase::is_fav($model,$user);
  214. $model['huanxinID'] = SystemUser::mk()->where('id',$model['admin_id'])->value('huanxinID');
  215. $this->success('',$model);
  216. }
  217. /**
  218. * @Title ("海运订单详情")
  219. * @Param ("id",desc="订单ID")
  220. * @Returned ("参考列表")
  221. */
  222. public function ocean_show(Request $request){
  223. $this->_vali([
  224. 'id.require'=>'参数有误',
  225. ],'get');
  226. $model=ShopOcean::show()->with(['items','merchant'])->findOrFail($request['id']);
  227. $user=$this->getUser();
  228. $model['user_order']=$model->offers()->with(['amounts'=>function($q)use($user){$q->where('user_id',$user['id']);}])->where('user_id',$user['id'])->find();
  229. if($model['offer_id']) {
  230. $model['status'] = 3;
  231. }else{
  232. $model['status'] = $model['user_order']?2:1;
  233. }
  234. ShopPurchase::is_fav($model,$user);
  235. $model['huanxinID'] = SystemUser::mk()->where('id',$model['admin_id'])->value('huanxinID');
  236. $this->success('',$model);
  237. }
  238. /**
  239. * @Title ("外协订单详情")
  240. * @Param ("id",desc="订单ID")
  241. * @Returned ("参考列表")
  242. */
  243. public function coor_show(Request $request){
  244. $this->_vali([
  245. 'id.require'=>'参数有误',
  246. ],'get');
  247. $model=ShopCoordination::show()->with(['items','merchant'])->findOrFail($request['id']);
  248. $user=$this->getUser();
  249. $model['user_order']=$model->offers()->with(['amounts'=>function($q)use($user){$q->where('user_id',$user['id']);}])->where('user_id',$user['id'])->find();
  250. if($model['offer_id']) {
  251. $model['status'] = 3;
  252. }else{
  253. $model['status'] = $model['user_order']?2:1;
  254. }
  255. ShopPurchase::is_fav($model,$user);
  256. $model['huanxinID'] = SystemUser::mk()->where('id',$model['admin_id'])->value('huanxinID');
  257. $this->success('',$model);
  258. }
  259. protected function checkUnAmount(){
  260. $user=$this->getUser();
  261. $unAmount=ShopCoordinationOffer::hasWhere('coordination',function (Query $query) {
  262. $query->whereNull('offer_id');
  263. })
  264. ->whereNull('shop_coordination_offer.amount')
  265. ->where('user_id',$user['id'])
  266. ->with(['coordination'])
  267. ->find();
  268. if($unAmount){
  269. $this->error('您有外协订单未报价无法再次接单');
  270. }
  271. $unAmount=ShopOceanOffer::hasWhere('ocean',function (Query $query) {
  272. $query->whereNull('offer_id');
  273. })->whereNull('shop_ocean_offer.amount')
  274. ->where('user_id',$user['id'])
  275. ->with(['ocean'])
  276. ->find();
  277. if($unAmount){
  278. $this->error('您有海运订单未报价无法再次接单');
  279. }
  280. $unAmount=ShopProductionOffer::hasWhere('production',function (Query $query) {
  281. $query->whereNull('offer_id');
  282. })->whereNull('shop_production_offer.amount')
  283. ->where('user_id',$user['id'])
  284. ->with(['production'])
  285. ->find();
  286. if($unAmount){
  287. $this->error('您有生产订单未报价无法再次接单');
  288. }
  289. $unAmount=ShopPurchaseOffer::hasWhere('item',function (Query $query){
  290. $query->whereNull('offer_id');
  291. })
  292. ->whereNull('shop_purchase_offer.amount')
  293. ->where('user_id',$user['id'])
  294. ->with(['item'])
  295. ->find();
  296. if($unAmount){
  297. $this->error('您有采购订单未报价无法再次接单');
  298. }
  299. // $unAmount=ShopCoordinationOffer::whereNull('amount')->where('user_id',$user['id'])->find();
  300. // if($unAmount){
  301. // $this->error('您有外协订单未报价无法再次接单');
  302. // }
  303. // $unAmount=ShopOceanOffer::whereNull('amount')->where('user_id',$user['id'])->find();
  304. // if($unAmount){
  305. // $this->error('您有海运订单未报价无法再次接单');
  306. // }
  307. // $unAmount=ShopProductionOffer::whereNull('amount')->where('user_id',$user['id'])->find();
  308. // if($unAmount){
  309. // $this->error('您有生产订单未报价无法再次接单');
  310. // }
  311. // $unAmount=ShopPurchaseOffer::whereNull('amount')->where('user_id',$user['id'])->find();
  312. // if($unAmount){
  313. // $this->error('您有采购订单未报价无法再次接单');
  314. // }
  315. }
  316. /**
  317. * @Title ("采购订单接单")
  318. * @Method("post")
  319. * @Param ("id",desc="子订单ID")
  320. * @Param ("amount",desc="报价")
  321. * @Param ("date",desc="货期/月")
  322. * @Param ("desc",desc="描述")
  323. * @Param ("is_have",desc="1是0否有货")
  324. * @Param ("images",desc="图片数组")
  325. * @Param ("is_edit",desc="1是0否重新报价")
  326. */
  327. public function purchase_create(Request $request){
  328. $data=$this->_vali([
  329. 'id.require' => '参数有误',
  330. // 'amount.require' => '报价有误',
  331. 'amount.float' => '报价有误',
  332. // 'images.array' => '图片有误',
  333. 'images.max:9' => '图片最多9张',
  334. // 'images.require' => '参数有误',
  335. // 'date.require' => '货期必须',
  336. 'desc.require' => '报价说明必须填写',
  337. 'desc.max:250' => '报价说明最多250',
  338. ], 'post');
  339. if($request['is_edit']){
  340. $data=$this->_vali([
  341. 'id.require' => '参数有误',
  342. 'amount.require' => '报价必须填写',
  343. 'amount.float' => '报价有误',
  344. // 'images.array' => '图片有误',
  345. 'images.max:9' => '图片最多9张',
  346. // 'images.require' => '参数有误',
  347. // 'date.require' => '货期必须',
  348. 'desc.require' => '报价说明必须填写',
  349. 'desc.max:250' => '报价说明最多250',
  350. ], 'post');
  351. }else {
  352. $data = $this->_vali([
  353. 'id.require' => '参数有误',
  354. // 'amount.require' => '报价有误',
  355. 'amount.float' => '报价有误',
  356. // 'images.array' => '图片有误',
  357. 'images.max:9' => '图片最多9张',
  358. // 'images.require' => '参数有误',
  359. // 'date.require' => '货期必须',
  360. 'desc.require' => '报价说明必须填写',
  361. 'desc.max:250' => '报价说明最多250',
  362. ], 'post');
  363. }
  364. input('date')?$data['date']=input('date'):'';
  365. input('is_have')?$data['is_have']=input('is_have'):'';
  366. $model=ShopPurchaseItem::findOrFail($data['id']);
  367. $user=$this->getUser();
  368. if($model['offer_id']){
  369. $this->error('该订单已匹配无法接单或报价');
  370. }
  371. if(!$request['is_edit']) {
  372. $this->checkUnAmount();
  373. }
  374. $offer=$model->offers()->where('user_id',$user['id'])->find();
  375. if(!$offer) {
  376. if($request['is_edit']){
  377. $this->error('没有接单无法报价');
  378. }
  379. $data['item_id'] = $data['id'];
  380. unset($data['id']);
  381. $data['user_id'] = $user['id'];
  382. $data['order_id'] = $model['purchase_id'];
  383. $result = ShopPurchaseOffer::mk()->create($data);
  384. $offer['id'] = $result->id;
  385. // $offer = $model->offers()->save([
  386. // 'user_id' => $user['id'],
  387. // 'order_id'=>$model['purchase_id'],
  388. // ] + $data);
  389. }else{
  390. $data['id'] = $offer['id'];
  391. $offer->save($data);
  392. }
  393. $offer['amount'] = $data['amount'];
  394. if(!empty($offer['amount'])){
  395. ShopPurchaseOfferAmount::create([
  396. 'user_id'=>$user['id'],
  397. 'offer_id'=>$offer['id'],
  398. 'amount'=>$offer['amount'],
  399. ]);
  400. if($model->main->status==1){
  401. $model->main->status=2;
  402. $model->main->save();
  403. }
  404. }
  405. $this->success('保存成功',$model);
  406. }
  407. /**
  408. * @Title ("生产订单接单")
  409. * @Method("post")
  410. * @Param ("id",desc="订单ID")
  411. * @Param ("amount",desc="报价")
  412. * @Param ("date",desc="货期/月")
  413. * @Param ("desc",desc="描述")
  414. * @Param ("images",desc="图片数组")
  415. * @Param ("is_edit",desc="是否重新报价")
  416. */
  417. public function production_create(Request $request){
  418. if($request['is_edit']){
  419. $data=$this->_vali([
  420. 'id.require' => '参数有误',
  421. // 'amount.require' => '报价必须',
  422. 'amount.float' => '报价有误',
  423. // 'date.require' => '货期必须',
  424. // 'date.integer' => '货期有误',
  425. 'desc.require' => '描述必须',
  426. 'desc.max:250' => '描述最多250',
  427. 'images.array' => '图片有误',
  428. 'images.max:9' => '图片最多9张',
  429. ], 'post');
  430. }else {
  431. $data = $this->_vali([
  432. 'id.require' => '参数有误',
  433. 'amount.float' => '报价有误',
  434. // 'date.require' => '货期必须',
  435. // 'date.integer' => '货期有误',
  436. 'desc.require' => '描述必须',
  437. 'desc.max:250' => '描述最多250',
  438. 'images.array' => '图片有误',
  439. 'images.max:9' => '图片最多9张',
  440. ], 'post');
  441. }
  442. input('date')?$data['date']=input('date'):'';
  443. $model=ShopProduction::show()->findOrFail($data['id']);
  444. $user=$this->getUser();
  445. if($model['offer_id']){
  446. $this->error('该订单已匹配无法接单或报价');
  447. }
  448. if(!$request['is_edit']) {
  449. $this->checkUnAmount();
  450. }
  451. $offer=$model->offers()->where('user_id',$user['id'])->find();
  452. if(!$offer) {
  453. if($request['is_edit']){
  454. $this->error('没有接单无法报价');
  455. }
  456. $data['production_id'] = $data['id'];
  457. unset($data['id']);
  458. $data['user_id'] = $user['id'];
  459. $offer = ShopProductionOffer::mk()->create($data);
  460. // $offer = $model->offers()->save([
  461. // 'user_id' => $user['id'],
  462. // ] + $data);
  463. }else{
  464. $data['id'] = $offer['id'];
  465. // file_put_contents("123.txt", json_encode($data,true) . "\n" . "\n", FILE_APPEND);
  466. $offer->save($data);
  467. }
  468. //$offer['amount'] = $data['amount'];
  469. if(!empty($offer['amount'])){
  470. ShopProductionOfferAmount::create([
  471. 'user_id'=>$user['id'],
  472. 'offer_id'=>$offer['id'],
  473. 'amount'=>$offer['amount'],
  474. ]);
  475. if($model->status==1){
  476. $model->status=2;
  477. $model->save();
  478. }
  479. }
  480. $this->success('保存成功',$model);
  481. }
  482. /**
  483. * @Title ("海运订单接单")
  484. * @Method("post")
  485. * @Param ("id",desc="订单ID")
  486. * @Param ("amount",desc="报价")
  487. * @Param ("desc",desc="描述")
  488. * @Param ("images",desc="图片数组")
  489. * @Param ("is_edit",desc="是否重新报价")
  490. */
  491. public function ocean_create(Request $request){
  492. if($request['is_edit']){
  493. $data=$this->_vali([
  494. 'id.require' => '参数有误',
  495. // 'amount.require' => '报价必须',
  496. 'amount.float' => '报价有误',
  497. 'desc.require' => '描述必须',
  498. 'desc.max:250' => '描述最多250',
  499. 'images.array' => '图片有误',
  500. 'images.max:9' => '图片最多9张',
  501. ], 'post');
  502. }else {
  503. $data = $this->_vali([
  504. 'id.require' => '参数有误',
  505. 'amount.float' => '报价有误',
  506. 'desc.require' => '描述必须',
  507. 'desc.max:250' => '描述最多250',
  508. 'images.array' => '图片有误',
  509. 'images.max:9' => '图片最多9张',
  510. ], 'post');
  511. }
  512. $model=ShopOcean::show()->findOrFail($data['id']);
  513. $user=$this->getUser();
  514. if($model['offer_id']){
  515. $this->error('该订单已匹配无法接单或报价');
  516. }
  517. if(!$request['is_edit']) {
  518. $this->checkUnAmount();
  519. }
  520. $offer=$model->offers()->where('user_id',$user['id'])->find();
  521. if(!$offer) {
  522. if($request['is_edit']){
  523. $this->error('没有接单无法报价');
  524. }
  525. $data['ocea_id'] = $data['id'];
  526. unset($data['id']);
  527. $data['user_id'] = $user['id'];
  528. $offer = ShopOceanOffer::mk()->create($data);
  529. // $offer = $model->offers()->save([
  530. // 'user_id' => $user['id'],
  531. // ] + $data);
  532. }else{
  533. $data['id'] = $offer['id'];
  534. $offer->save($data);
  535. }
  536. if(!empty($offer['amount'])){
  537. ShopOceanOfferAmount::create([
  538. 'user_id'=>$user['id'],
  539. 'offer_id'=>$offer['id'],
  540. 'amount'=>$offer['amount'],
  541. ]);
  542. if($model->status==1){
  543. $model->status=2;
  544. $model->save();
  545. }
  546. }
  547. $this->success('保存成功',$model);
  548. }
  549. /**
  550. * @Title ("外协订单接单")
  551. * @Method("post")
  552. * @Param ("id",desc="订单ID")
  553. * @Param ("amount",desc="报价")
  554. * @Param ("desc",desc="描述")
  555. * @Param ("images",desc="图片数组")
  556. * @Param ("is_edit",desc="是否重新报价")
  557. */
  558. public function coor_create(Request $request){
  559. if($request['is_edit']){
  560. $data=$this->_vali([
  561. 'id.require' => '参数有误',
  562. // 'amount.require' => '报价必须',
  563. 'amount.float' => '报价有误',
  564. 'desc.require' => '描述必须',
  565. 'desc.max:250' => '描述最多250',
  566. 'images.array' => '图片有误',
  567. 'images.max:9' => '图片最多9张',
  568. ], 'post');
  569. }else {
  570. $data = $this->_vali([
  571. 'id.require' => '参数有误',
  572. 'amount.float' => '报价有误',
  573. 'desc.require' => '描述必须',
  574. 'desc.max:250' => '描述最多250',
  575. 'images.array' => '图片有误',
  576. 'images.max:9' => '图片最多9张',
  577. ], 'post');
  578. }
  579. $model=ShopCoordination::show()->findOrFail($data['id']);
  580. $user=$this->getUser();
  581. if($model['offer_id']){
  582. $this->error('该订单已匹配无法接单或报价');
  583. }
  584. if(!$request['is_edit']) {
  585. $this->checkUnAmount();
  586. }
  587. $offer=$model->offers()->where('user_id',$user['id'])->find();
  588. if(!$offer) {
  589. if($request['is_edit']){
  590. $this->error('没有接单无法报价');
  591. }
  592. $data['corrdination_id'] = $data['id'];
  593. unset($data['id']);
  594. $data['user_id'] = $user['id'];
  595. $offer = ShopCoordinationOffer::mk()->create($data);
  596. // $offer = $model->offers()->save([
  597. // 'user_id' => $user['id'],
  598. // ] + $data);
  599. }else{
  600. $data['id'] = $offer['id'];
  601. $offer->save($data);
  602. }
  603. if(!empty($offer['amount'])){
  604. ShopCoordinationOfferAmount::create([
  605. 'user_id'=>$user['id'],
  606. 'offer_id'=>$offer['id'],
  607. 'amount'=>$offer['amount'],
  608. ]);
  609. if($model->status==1){
  610. $model->status=2;
  611. $model->save();
  612. }
  613. }
  614. $this->success('保存成功',$model);
  615. }
  616. /**
  617. * @Title("接单需求")
  618. * @Param("page",desc="第几页")
  619. * @Param("limit",desc="每页数量")
  620. * @Param("keyword",desc="关键字")
  621. * @Returned("user",desc="用户对象")
  622. * @Returned("user.headimg",desc="用户头像")
  623. * @Returned("user.phone",desc="电话")
  624. * @Returned("addr",desc="地址")
  625. * @Returned("description",desc="需求说明")
  626. * @Returned("username",desc="用户名")
  627. * @Returned("imgs",desc="图片及资质证件图片,多个以英文逗号隔开")
  628. */
  629. public function needs(Request $request){
  630. $list=DataUserPurchaseAcceptance::with(['user'])
  631. // ->when($request['keyword'],function (Query $query)use ($request){
  632. // $query->whereExists(
  633. // DataUser::whereLike('nickname|real_name',"%{$request['keyword']}%")->where('id',Db::raw(DataUserPurchaseAcceptance::getTable().'.uuid'))->buildSql()
  634. // );
  635. // })
  636. ->when($request['keyword'],function ($query)use ($request){
  637. $query->whereLike('description',"%{$request['keyword']}%");
  638. })
  639. ->where('hidden',1)
  640. ->where('status',1)
  641. ->order('id','desc')
  642. ->paginate($request['limit']?:15);
  643. foreach ($list as $item){
  644. $item['username']=$item['user']['nickname']??'-';
  645. }
  646. $this->success('',$list);
  647. }
  648. /**
  649. *@Title("接单需求详情")
  650. *@Param("id",desc="ID")
  651. */
  652. public function needs_show(Request $request){
  653. $show=DataUserPurchaseAcceptance::with(['user'])
  654. ->where('status',1)
  655. ->where('id',$request['id'])
  656. ->findOrFail();
  657. $show['username']=$show['user']['nickname']??'-';
  658. $this->success('',$show);
  659. }
  660. /**
  661. * @Title ("发送留言")
  662. * @Method("post")
  663. * @Param ("id",desc="订单ID")
  664. * @Param ("tab",desc="1采购2生产3外协4海运")
  665. * @Param ("content",desc="内容")
  666. */
  667. public function msg_send(Request $request){
  668. $this->_vali([
  669. 'id.require'=>'id必须',
  670. 'tab.require'=>'tab必须',
  671. 'content.require'=>'tab必须',
  672. 'content.max:500'=>'内容过长',
  673. ]);
  674. $model=ShopPurchase::getQuery($request['tab'])->findOrFail($request['id']);
  675. /*$msg=$model->msgs()->save([
  676. 'content'=>$request['content'],
  677. 'user_id'=>$this->uuid,
  678. 'm_id' =>$model['admin_id'],
  679. ]);*/
  680. $msg=DataUserLeaveMessage::create([
  681. 'uuid'=>$this->uuid,
  682. 'uuid2'=>$model['admin_id'],
  683. 'content'=>$request['content'],
  684. 'type'=>DataUserLeaveMessage::getMsgType($model->getTable()),
  685. 'link_id'=>$model['id'],
  686. ]);
  687. $this->success('发送成功',$msg);
  688. }
  689. /**
  690. * @Title ("留言列表")
  691. * @Param ("id",desc="订单ID")
  692. * @Param ("tab",desc="1采购2生产3外协4海运")
  693. * @Returned ("user.headimg",desc="用户头像")
  694. * @Returned ("merchant.headimg",desc="商家头像")
  695. * @Returned ("is_mine",desc="是否是自己的留言")
  696. */
  697. public function msg_get(Request $request,DataUserLeaveMessage $message){
  698. $this->_vali([
  699. 'id.require'=>'id必须',
  700. 'tab.require'=>'tab必须',
  701. ],'get');
  702. $model=ShopPurchase::getQuery($request['tab'])->findOrFail($request['id']);
  703. $admin_id = $model['admin_id'];
  704. $messagelist = DataUserLeaveMessage::mk()
  705. ->where('type',DataUserLeaveMessage::getMsgType($model->getTable()))
  706. ->where('link_id',$model['id'])
  707. ->whereRaw("(uuid=$this->uuid and uuid2=$admin_id) or (uuid2=$this->uuid and uuid=$admin_id)")
  708. ->with(['user'=>function($query){
  709. $query->field('id,nickname,headimg');
  710. }])
  711. ->field('id,uuid,uuid2,content,create_at')
  712. ->select();
  713. // echo DataUserLeaveMessage::getLastSql();die;
  714. $this->success('成功',$messagelist);
  715. }
  716. }