Purchase.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. namespace app\data\controller\api\business;
  3. use app\data\model\DataUser;
  4. use app\data\model\DataUserLeaveMessage;
  5. use app\data\model\ShopCoordination;
  6. use app\data\model\ShopCoordinationItem;
  7. use app\data\model\ShopOcean;
  8. use app\data\model\ShopOceanItem;
  9. use app\data\model\ShopProduction;
  10. use app\data\model\ShopProductionItem;
  11. use app\data\model\ShopPurchase;
  12. use app\data\model\ShopPurchaseItem;
  13. use app\data\model\ShopPurchaseOffer;
  14. use app\data\model\ShopPurchaseOfferAmount;
  15. use app\data\model\SystemUser;
  16. use think\admin\Controller;
  17. use think\Exception;
  18. use think\exception\ErrorException;
  19. use think\facade\Db;
  20. use hg\apidoc\annotation\Title;
  21. use hg\apidoc\annotation\Method;
  22. use hg\apidoc\annotation\Param;
  23. use hg\apidoc\annotation\Returned;
  24. use think\facade\Validate;
  25. use think\Request;
  26. /**
  27. * 商家端发布订单
  28. * Class Goods
  29. * @package app\data\controller\api
  30. */
  31. class Purchase extends Controller
  32. {
  33. /**
  34. * @Title ("采购订单发布和编辑")
  35. * @Method ("post")
  36. * @Param ("id",desc="如果是编辑则传ID")
  37. * @Param ("real_name",desc="联系人")
  38. * @Param ("mobile",desc="联系方式")
  39. * @Param ("post_time",desc="交货时间")
  40. * @Param ("post_address",desc="交货地址")
  41. * @Param ("images",desc="图片数组,[{type:'image或video',src:'http://xxxxx'}]")
  42. * @Param ("item",desc="['id:修改则传ID','name 商品名称','spec 规格','number 数量','standard 验收标准','price 价格']")
  43. * @return void
  44. */
  45. public function purchase_order(\think\Validate $validate,Request $request){
  46. $request->offsetSet('images',json_decode($request['images'],true)?:[]);
  47. $request->offsetSet('item',json_decode($request['item'],true)?:[]);
  48. $admin_id = $this->uuid();
  49. $data=$this->_vali([
  50. 'real_name.require'=>'联系人不能为空',
  51. 'mobile.require'=>'联系方式不能为空',
  52. 'post_time.require'=>'交货时间不能为空',
  53. 'post_address.require'=>'交货单地址不能为空',
  54. // 'images.require'=>'图片不能为空',
  55. // 'images.array'=>'图片格式有误',
  56. // 'images.min:1'=>'图片必须',
  57. 'item.require'=>'商品参数不能为空',
  58. ]);
  59. $images = $this->request->post('images');
  60. if(isset($images) && $images!=null){
  61. $data['images'] = json_decode($request['images'],true);
  62. foreach ($data['images'] as $image){
  63. $validate->failException()->check($image,[
  64. 'type|图片类型'=>['require','in:image,video'],
  65. 'src|图片链接'=>['require','url'],
  66. ]);
  67. }
  68. }
  69. $data['item'] = json_decode($request['item'],true);
  70. foreach ($data['item'] as $item){
  71. $validate->failException()->check($item,[
  72. 'name|名称'=>['require'],
  73. 'spec|规格'=>['require'],
  74. 'number|数量'=>['require','integer'],
  75. // 'standard|验收标准'=>['require'],
  76. // 'price|价格'=>['require','float'],
  77. ]);
  78. }
  79. $purchase_data = [
  80. 'admin_id'=>$admin_id->id,
  81. 'real_name'=>$data['real_name'],
  82. 'mobile'=>$data['mobile'],
  83. // 'images'=>$data['images'],
  84. 'post_time'=>$data['post_time'],
  85. 'post_address'=>$data['post_address'],
  86. 'create_time'=>date('Y-m-d H:i:s'),
  87. 'longitude'=>$admin_id->merchant->longitude,
  88. 'latitude'=>$admin_id->merchant->latitude,
  89. 'amount'=>min(array_column($data['item'],'price')),
  90. ];
  91. isset($data['images'])?$purchase_data['images']=$data['images']:'';
  92. Db::startTrans();
  93. try {
  94. if($request['id']){
  95. $order_id=ShopPurchase::where('admin_id',$admin_id['id'])->where('id',$request['id'])->findOrFail();
  96. $order_id->save($purchase_data);
  97. }else {
  98. $order_id = ShopPurchase::create($purchase_data);
  99. }
  100. $item_ids=[];
  101. foreach ($data['item'] as &$v){
  102. $item_data = [
  103. 'purchase_id'=>$order_id['id'],
  104. 'name'=>$v['name'],
  105. 'spec'=>$v['spec'],
  106. 'number'=>$v['number'],
  107. 'standard'=>$v['standard'],
  108. 'price'=>$v['price'],
  109. ];
  110. if(!empty($v['id'])){
  111. $itemModel=$order_id->items()->where('id',$v['id'])->find();
  112. if(!$itemModel){
  113. $this->error('商品不存在');
  114. }
  115. $itemModel->save($item_data);
  116. }else{
  117. $itemModel=$order_id->items()->save($item_data);
  118. }
  119. $item_ids[]=$itemModel['id'];
  120. }
  121. $order_id->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  122. ShopPurchaseOffer::where('order_id',$order_id['id'])
  123. ->whereNotIn('item_id',$item_ids)
  124. ->select()->each(function ($m){$m->delete();});
  125. // 提交事务
  126. Db::commit();
  127. $this->success('采购订单发布成功');
  128. } catch (ErrorException $e) {
  129. // 回滚事务
  130. Db::rollback();
  131. $this->error('发布失败');
  132. }
  133. }
  134. /**
  135. * @Title ("生产订单发布或编辑")
  136. * @Method ("post")
  137. * @Param ("id",desc="如果是编辑则传ID")
  138. * @Param ("real_name",desc="联系人")
  139. * @Param ("mobile",desc="联系方式")
  140. * @Param ("standard_tech",desc="生产技术标准")
  141. * @Param ("standard_check",desc="生产验收标准")
  142. * @Param ("post_time",desc="交货时间")
  143. * @Param ("post_address",desc="交货地址")
  144. * @Param ("file",desc="图片/视频,[{type:'image或video',src:'http://xxxxx'}]")
  145. * @Param ("item",desc="二维数组,[{'id:修改则传ID',name:'产品名称',spec:'规格',number:'数量',enclosure:[{filename:'文件名',fileurl:'文件链接'}]}]")
  146. * @return void
  147. */
  148. public function production_order(\think\Validate $validate,Request $request){
  149. $admin_id = $this->uuid();
  150. $request->offsetSet('file',json_decode($request['file'],true)?:[]);
  151. $request->offsetSet('item',json_decode($request['item'],true)?:[]);
  152. $data=$this->_vali([
  153. 'real_name.require'=>'联系人不能为空',
  154. 'mobile.require'=>'联系方式不能为空',
  155. // 'standard_tech.require'=>'生产技术标准必须',
  156. // 'standard_check.require'=>'生产验收标准必须',
  157. 'post_time.require'=>'交货时间不能为空',
  158. 'post_address.require'=>'交货单地址不能为空',
  159. 'item.require'=>'商品参数不能为空',
  160. //'file.array'=>'图片视频有误',
  161. ]);
  162. // exit;
  163. $data['item'] = json_decode($request['item'],true);
  164. if(input('file')){
  165. $data['file'] = json_decode($request['file'],true);
  166. foreach ($data['file']??[] as $image){
  167. $validate->failException()->check($image,[
  168. 'type|图片类型'=>['require','in:image,video'],
  169. 'src|图片链接'=>['require','url'],
  170. ]);
  171. }
  172. }
  173. foreach ($data['item'] as $item){
  174. $validate->failException()->check($item,[
  175. 'name|名称'=>['require'],
  176. 'spec|规格'=>['require'],
  177. 'number|数量'=>['require','integer'],
  178. // 'enclosure|附件'=>['array',],
  179. ]);
  180. // if(!empty($item['enclosure'])){
  181. // foreach ($item['enclosure'] as $enclosure){
  182. // $validate->failException()->check($enclosure,[
  183. // 'filename|文件名'=>['require'],
  184. // 'fileurl|文件地址'=>['require','url',],
  185. // ]);
  186. // }
  187. // }
  188. }
  189. $purchase_data = [
  190. 'admin_id'=>$admin_id->id,
  191. 'real_name'=>$data['real_name'],
  192. 'mobile'=>$data['mobile'],
  193. // 'standard_tech'=>$data['standard_tech'],
  194. // 'standard_check'=>$data['standard_check'],
  195. 'post_time'=>$data['post_time'],
  196. 'post_address'=>$data['post_address'],
  197. 'longitude'=>$admin_id->merchant->longitude,
  198. 'latitude'=>$admin_id->merchant->latitude,
  199. 'create_time'=>date('Y-m-d H:i:s'),
  200. 'file'=>$data['file']??[],
  201. ];
  202. input('standard_tech')?$purchase_data['standard_tech']=input('standard_tech'):'';
  203. input('standard_check')?$purchase_data['standard_check']=input('standard_check'):'';
  204. Db::startTrans();
  205. try {
  206. if($request['id']) {
  207. $order=ShopProduction::where('admin_id',$admin_id['id'])->where('id',$request['id'])->findOrFail();
  208. $order->save($purchase_data);
  209. $item_ids=[];
  210. foreach ($data['item'] as $item){
  211. if(!empty($item['id'])){
  212. $itemModel=$order->items()->where('id',$item['id'])->findOrFail();
  213. $itemModel->save($item);
  214. }else{
  215. $itemModel=$order->items()->save($item);
  216. }
  217. $item_ids[]=$itemModel['id'];
  218. }
  219. $order->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  220. }else{
  221. $order = ShopProduction::create($purchase_data);
  222. $order->items()->saveAll($data['item']);
  223. }
  224. // 提交事务
  225. Db::commit();
  226. $this->success('订单已发布');
  227. } catch (ErrorException $e) {
  228. // 回滚事务
  229. Db::rollback();
  230. $this->success('发布失败');
  231. }
  232. }
  233. /**
  234. * @Title ("海运订单发布或编辑")
  235. * @Method ("post")
  236. * @Param ("id",desc="如果是编辑则传ID")
  237. * @Param ("real_name",desc="联系人")
  238. * @Param ("mobile",desc="联系方式")
  239. * @Param ("form",desc="包装形式")
  240. * @Param ("wharf_to",desc="装货码头")
  241. * @Param ("wharf_for",desc="交货码头")
  242. * @Param ("file",desc="图片/视频,[{type:'image或video',src:'http://xxxxx'}]")
  243. * @Param ("item",desc="['id:修改则传ID','name 商品名称','weight 重量']")
  244. * @return void
  245. */
  246. public function ocean_order(\think\Validate $validate,Request $request){
  247. $request->offsetSet('file',json_decode($request['file'],true)?:[]);
  248. $request->offsetSet('item',json_decode($request['item'],true)?:[]);
  249. $admin_id = $this->uuid();
  250. $data=$this->_vali([
  251. 'real_name.require'=>'联系人不能为空',
  252. 'mobile.require'=>'联系方式不能为空',
  253. 'form.require'=>'包装形式必须',
  254. 'wharf_to.require'=>'装货码头必须',
  255. 'wharf_for.require'=>'交货码头必须',
  256. // 'file.array'=>'图片必须是数组',
  257. 'item.require'=>'商品必须',
  258. 'item.array'=>'商品必须是数组',
  259. ]);
  260. $data['item'] = json_decode($request['item'],true);
  261. if(input('file')){
  262. $data['file'] = json_decode($request['file'],true);
  263. foreach ($data['file']??[] as $image){
  264. $validate->failException()->check($image,[
  265. 'type|图片类型'=>['require','in:image,video'],
  266. 'src|图片链接'=>['require','url'],
  267. ]);
  268. }
  269. }
  270. foreach ($data['item'] as $item){
  271. $validate->failException()->check($item,[
  272. 'name|名称'=>['require'],
  273. 'weight|数量吨位'=>['require'],
  274. ]);
  275. }
  276. $purchase_data = [
  277. 'admin_id'=>$admin_id->id,
  278. 'real_name'=>$data['real_name'],
  279. 'mobile'=>$data['mobile'],
  280. 'form'=>$data['form'],
  281. 'wharf_to'=>$data['wharf_to'],
  282. 'wharf_for'=>$data['wharf_for'],
  283. 'file'=>$data['file']??[],
  284. 'longitude'=>$admin_id->merchant->longitude,
  285. 'latitude'=>$admin_id->merchant->latitude,
  286. ];
  287. Db::startTrans();
  288. try {
  289. if($request['id']){
  290. $order_id=ShopOcean::where('admin_id',$admin_id['id'])->where('id',$request['id'])->findOrFail();
  291. $order_id->save($purchase_data);
  292. $item_ids=[];
  293. foreach ($data['item'] as $item){
  294. if(!empty($item['id'])){
  295. $itemModel=$order_id->items()->where('id',$item['id'])->findOrFail();
  296. $itemModel->save($item);
  297. }else{
  298. $itemModel=$order_id->items()->save($item);
  299. }
  300. $item_ids[]=$itemModel['id'];
  301. }
  302. $order_id->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  303. }else {
  304. $order_id = ShopOcean::mk()->create($purchase_data);
  305. $order_id->items()->saveAll($data['item']);
  306. }
  307. // 提交事务
  308. Db::commit();
  309. $this->success('已发布');
  310. } catch (ErrorException $e) {
  311. // 回滚事务
  312. Db::rollback();
  313. $this->success('发布失败');
  314. }
  315. }
  316. /**
  317. * @Title ("外协订单发布或编辑")
  318. * @Method ("post")
  319. * @Param ("id",desc="如果是编辑则传ID")
  320. * @Param ("real_name",desc="联系人")
  321. * @Param ("mobile",desc="联系方式")
  322. * @Param ("standard_tech",desc="生产技术标准")
  323. * @Param ("standard_check",desc="生产验收标准")
  324. * @Param ("post_time",desc="交货时间")
  325. * @Param ("post_address",desc="交货地址")
  326. * @Param ("file",desc="图片/视频,[{type:'image或video',src:'http://xxxxx'}]")
  327. * @Param ("item",desc="二维数组,[{'id:修改则传ID',name:'产品名称',spec:'规格',number:'数量',enclosure:[{filename:'文件名',fileurl:'文件链接'}]}]")
  328. * @return void
  329. */
  330. public function coordination_order(\think\Validate $validate,Request $request){
  331. $request->offsetSet('file',json_decode($request['file'],true)?:[]);
  332. $request->offsetSet('item',json_decode($request['item'],true)?:[]);
  333. $admin_id = $this->uuid();
  334. $data=$this->_vali([
  335. 'real_name.require'=>'联系人不能为空',
  336. 'mobile.require'=>'联系方式不能为空',
  337. // 'standard_tech.require'=>'生产技术标准',
  338. // 'standard_check.require'=>'生产验收标准',
  339. 'post_time.require'=>'交货时间不能为空',
  340. 'post_address.require'=>'交货单地址不能为空',
  341. 'item.require'=>'商品参数不能为空',
  342. // 'file.array'=>'图片必须是数组',
  343. ]);
  344. $data['item'] = json_decode($request['item'],true);
  345. if(input('file')) {
  346. $data['file'] = json_decode($request['file'],true);
  347. foreach ($data['file'] ?? [] as $image) {
  348. $validate->failException()->check($image, [
  349. 'type|图片类型' => ['require', 'in:image,video'],
  350. 'src|图片链接' => ['require', 'url'],
  351. ]);
  352. }
  353. }
  354. foreach ($data['item'] as $item){
  355. $validate->failException()->check($item,[
  356. 'name|名称'=>['require'],
  357. 'spec|规格'=>['require'],
  358. 'number|数量'=>['require','integer'],
  359. // 'enclosure|附件'=>['array',],
  360. ]);
  361. // if(!empty($item['enclosure'])){
  362. // foreach ($item['enclosure'] as $enclosure){
  363. // $validate->failException()->check($enclosure,[
  364. // 'filename|文件名'=>['require'],
  365. // 'fileurl|文件地址'=>['require','url',],
  366. // ]);
  367. // }
  368. // }
  369. }
  370. $purchase_data = [
  371. 'admin_id'=>$admin_id->id,
  372. 'real_name'=>$data['real_name'],
  373. 'mobile'=>$data['mobile'],
  374. // 'standard_tech'=>$data['standard_tech'],
  375. // 'standard_check'=>$data['standard_check'],
  376. 'post_time'=>$data['post_time'],
  377. 'post_address'=>$data['post_address'],
  378. 'longitude'=>$admin_id->merchant->longitude,
  379. 'latitude'=>$admin_id->merchant->latitude,
  380. 'file'=>$data['file']??[],
  381. 'create_time'=>date('Y-m-d H:i:s')
  382. ];
  383. input('standard_tech')?$purchase_data['standard_tech']=input('standard_tech'):'';
  384. input('standard_check')?$purchase_data['standard_check']=input('standard_check'):'';
  385. Db::startTrans();
  386. try {
  387. if($request['id']){
  388. $order=ShopCoordination::where('id',$request['id'])->where('admin_id',$admin_id['id'])->findOrFail();
  389. $order->save($purchase_data);
  390. $item_ids=[];
  391. foreach ($data['item'] as $item){
  392. if(!empty($item['id'])){
  393. $itemModel=$order->items()->where('id',$item['id'])->findOrFail();
  394. $itemModel->save($item);
  395. }else{
  396. $itemModel=$order->items()->save($item);
  397. }
  398. $item_ids[]=$itemModel['id'];
  399. }
  400. $order->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  401. }else {
  402. $order = ShopCoordination::mk()->create($purchase_data);
  403. $order->items()->saveAll($data['item']);
  404. }
  405. // 提交事务
  406. Db::commit();
  407. $this->success('已发布');
  408. } catch (ErrorException $e) {
  409. // 回滚事务
  410. Db::rollback();
  411. $this->success('发布失败');
  412. }
  413. }
  414. public function order_no($key,$uuid){
  415. $time_str = date('YmdHi');
  416. $order_no = $key.$time_str . sprintf("%04d", $uuid);
  417. return $order_no;
  418. }
  419. public function uuid(){
  420. $purchase_model = new Common($this->app);
  421. $uuid = $purchase_model->uuid();
  422. return $uuid;
  423. }
  424. /**
  425. * @Title("采购订单列表")
  426. * @Param ("page",desc="第几页")
  427. * @Param ("limit",desc="每页几条")
  428. * @Param ("status",desc="1未报价2已报价3已匹配")
  429. * @Param ("tab",desc="1采购2生产3外协4海运")
  430. * @Returned("参考用户端采购订单列表")
  431. */
  432. public function list(Request $request){
  433. $status=$request['status'];
  434. $tab=$request['tab']?:1;
  435. $query=ShopPurchase::getQuery($tab);
  436. $list=$query
  437. ->where('admin_id',$this->uuid()->id)
  438. ->when($tab,function ($query) use ($tab){
  439. if ($tab==1){
  440. $query ->with(['items','items.offers']);
  441. }else{
  442. $query ->with(['items','offers']);
  443. }
  444. })
  445. ->when($status,['status'=>$status])
  446. ->order('id','desc')
  447. ->paginate($request['limit']?:15);
  448. $this->success('',$list);
  449. }
  450. /**
  451. *@Title("详情")
  452. * @Param ("tab",desc="1采购2生产3外协4海运")
  453. * @Param ("id",desc="订单ID")
  454. * @Returned("is_matched",desc="是否已匹配")
  455. * @Returned("参考用户端采购订单列表")
  456. */
  457. public function show(Request $request){
  458. DataUser::$copyRealName=true;
  459. $data=$this->_vali([
  460. 'id.require'=>'ID必须',
  461. 'tab.require'=>'tab必须',
  462. ],'get');
  463. if($data['tab']==1) {
  464. $model = ShopPurchase::getQuery($data['tab'])
  465. ->with(['items','items.offers','items.offers.user'])
  466. ->findOrFail($data['id']);
  467. foreach ($model['items'] as &$v){
  468. foreach ($v['offers'] as $aa=>&$a){
  469. if($a['id']!=$v['offer_id'] && $v['offer_id']!=''){
  470. unset($v['offers'][$aa]);
  471. }
  472. $a['amounts'] = ShopPurchaseOfferAmount::mk()
  473. ->where('user_id',$a['user_id'])
  474. ->where('offer_id',$a['id'])
  475. ->order('id asc')
  476. ->select();
  477. }
  478. }
  479. }else{
  480. $model = ShopPurchase::getQuery($data['tab'])
  481. ->with(['items','offers','offers.user'])
  482. ->findOrFail($data['id']);
  483. }
  484. $this->success('',$model);
  485. }
  486. /**
  487. *@Title("取消订单")
  488. * @Param ("tab",desc="1采购2生产3外协4海运")
  489. * @Param ("id",desc="订单ID")
  490. */
  491. public function cancel(Request $request){
  492. $data=$this->_vali([
  493. 'id.require'=>'ID必须',
  494. 'tab.require'=>'tab必须',
  495. ],'get');
  496. $admin=$this->uuid();
  497. $model=ShopPurchase::getQuery($data['tab'])->where('admin_id',$admin['id'])->findOrFail($data['id']);
  498. $model['status']=0;
  499. $model->save();
  500. $this->success('操作成功');
  501. }
  502. /**
  503. *@Title("提醒完成")
  504. * @Param ("user_id",desc="用户ID")
  505. */
  506. public function reminder(Request $request){
  507. $data=$this->_vali([
  508. 'user_id.require'=>'user_id必须'
  509. ],'get');
  510. $alias = DataUser::mk()->where('id',$data['user_id'])->value('jgalias');
  511. $content = '您报价的订单已匹配完成,请及时查看';
  512. $alias ? jgpush($content,$alias) : '';
  513. setusermessage($data['user_id'],'匹配通知',$content);
  514. $this->success('操作成功');
  515. }
  516. /**
  517. * @Title("匹配订单")
  518. * @Method("POST")
  519. * @Param ("tab",desc="1采购2生产3外协4海运")
  520. * @Param ("id",desc="订单ID")
  521. * @Param ("offer_id",desc="接单ID")
  522. */
  523. public function match(Request $request){
  524. $data=$this->_vali([
  525. 'id.require'=>'ID必须',
  526. 'tab.require'=>'tab必须',
  527. 'offer_id.require'=>'offer必须',
  528. ]);
  529. $admin=$this->uuid();
  530. $tab = $data['tab'];
  531. $id = $data['id'];
  532. Db::startTrans();
  533. // $admin['id'] = '10010';
  534. $model=ShopPurchase::getItem($data['tab'],$admin['id'])->lock(true)
  535. ->where(function ($query) use ($tab,$id){
  536. // if ($tab==1){
  537. // $query->where('purchase_id',$id);
  538. // }else{
  539. $query->where('id',$id);
  540. // }
  541. })
  542. ->findOrEmpty();
  543. $model_id = $model['id'];
  544. //dump($model->toArray());die;
  545. if($model['is_matched']){
  546. Db::rollback();
  547. $this->error('该订单已匹配');
  548. }
  549. // $hasMatch=ShopPurchase::getItem($data['tab'])->where('offer_id',$data['offer_id'])->value('id');
  550. $hasMatch=ShopPurchase::getItem($data['tab'])->where('id',$data['id'])->value('offer_id');
  551. if($hasMatch){
  552. Db::rollback();
  553. $this->error('该接单已被其他订单匹配');
  554. }
  555. $offer=ShopPurchase::getOffer($data['tab'])->lock(true)
  556. // ->where('user_id',$data['offer_id'])
  557. ->when($tab,function ($querys) use ($tab,$model_id){
  558. switch ($tab){
  559. case 1:
  560. $querys->where('item_id',$model_id);
  561. break;
  562. case 2:
  563. $querys->where('production_id',$model_id);
  564. break;
  565. case 3:
  566. $querys->where('corrdination_id',$model_id);
  567. break;
  568. case 4:
  569. $querys->where('ocea_id',$model_id);
  570. break;
  571. }
  572. })
  573. ->findOrEmpty();
  574. if($request['tab']==1){
  575. if ($offer->item_id != $model['id']) {
  576. Db::rollback();
  577. $this->error('该接单不属于此订单');
  578. }
  579. }else {
  580. if ($offer->order_id != $model['id']) {
  581. Db::rollback();
  582. $this->error('该接单不属于此订单');
  583. }
  584. }
  585. $model['offer_id']=$data['offer_id'];
  586. if($data['tab']==1){
  587. $main=$model->main;
  588. $main['status']=3;
  589. $main->save();
  590. }else{
  591. $model['status']=3;
  592. }
  593. $model->save();
  594. Db::commit();
  595. $url = setword($data['tab']+1,$data['id']);
  596. ShopPurchase::getItem($data['tab'])->where('id',$model['id'])->update(['down_word'=>$url]);
  597. $this->success('匹配成功');
  598. }
  599. /**
  600. * @Title ("发送留言")
  601. * @Method("post")
  602. * @Param ("id",desc="订单ID")
  603. * @Param ("tab",desc="1采购2生产3外协4海运")
  604. * @Param ("user_id",desc="发送给的用户id")
  605. * @Param ("content",desc="内容")
  606. */
  607. public function msg_send(Request $request){
  608. $this->_vali([
  609. 'id.require'=>'id必须',
  610. 'user_id.require'=>'用户必须',
  611. 'tab.require'=>'tab必须',
  612. 'content.require'=>'tab必须',
  613. 'content.max:500'=>'内容过长',
  614. ]);
  615. $model=ShopPurchase::getQuery($request['tab'])->findOrFail($request['id']);
  616. $user=DataUser::where('id',$request['user_id'])->findOrFail();
  617. $msg=DataUserLeaveMessage::create([
  618. 'uuid'=>$this->uuid()->id,
  619. 'uuid2'=>$user['id'],
  620. 'content'=>$request['content'],
  621. 'type'=>DataUserLeaveMessage::getMsgType($model->getTable()),
  622. 'link_id'=>$model['id'],
  623. ]);
  624. $this->success('发送成功',$msg);
  625. }
  626. /**
  627. * @Title ("留言列表")
  628. * @Param ("id",desc="订单ID")
  629. * @Param ("tab",desc="1采购2生产3外协4海运")
  630. * @Param ("user_id",desc="要查看的用户id")
  631. * @Returned ("user.headimg",desc="用户头像")
  632. * @Returned ("merchant.headimg",desc="商家头像")
  633. * @Returned ("is_mine",desc="是否是自己的留言")
  634. */
  635. public function msg_get(Request $request){
  636. $this->_vali([
  637. 'id.require'=>'id必须',
  638. 'tab.require'=>'tab必须',
  639. ],'get');
  640. $model=ShopPurchase::getQuery($request['tab'])->findOrFail($request['id']);
  641. $admin_id = $model['admin_id'];
  642. $user_id = $request['user_id'];
  643. $messagelist = DataUserLeaveMessage::mk()
  644. ->where('type',DataUserLeaveMessage::getMsgType($model->getTable()))
  645. ->whereRaw("(uuid=$user_id and uuid2=$admin_id) or (uuid2=$user_id and uuid=$admin_id)")
  646. ->where('link_id',$model['id'])
  647. ->field('id,uuid,uuid2,content,create_at')
  648. ->select()->toArray();
  649. $this->success('成功',$messagelist);
  650. }
  651. /**
  652. * @Title("获取订单配置")
  653. * @Returned("packing",desc="包装形式数组")
  654. */
  655. public function config(){
  656. $config=[];
  657. $config['packing']=systemConfig('merchant_config.packing')?:[];
  658. $this->success('',$config);
  659. }
  660. }