Purchase.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. $input = input();
  205. if(!isset($input['standard_tech'])){
  206. $purchase_data['standard_tech'] = '';
  207. }
  208. if(!isset($input['standard_check'])){
  209. $purchase_data['standard_tech'] = '';
  210. }
  211. Db::startTrans();
  212. try {
  213. if($request['id']) {
  214. $order=ShopProduction::where('admin_id',$admin_id['id'])->where('id',$request['id'])->findOrFail();
  215. $order->save($purchase_data);
  216. $item_ids=[];
  217. foreach ($data['item'] as $item){
  218. if(!empty($item['id'])){
  219. $itemModel=$order->items()->where('id',$item['id'])->findOrFail();
  220. $itemModel->save($item);
  221. }else{
  222. $itemModel=$order->items()->save($item);
  223. }
  224. $item_ids[]=$itemModel['id'];
  225. }
  226. $order->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  227. }else{
  228. $order = ShopProduction::create($purchase_data);
  229. $order->items()->saveAll($data['item']);
  230. }
  231. // 提交事务
  232. Db::commit();
  233. $this->success('订单已发布');
  234. } catch (ErrorException $e) {
  235. // 回滚事务
  236. Db::rollback();
  237. $this->success('发布失败');
  238. }
  239. }
  240. /**
  241. * @Title ("海运订单发布或编辑")
  242. * @Method ("post")
  243. * @Param ("id",desc="如果是编辑则传ID")
  244. * @Param ("real_name",desc="联系人")
  245. * @Param ("mobile",desc="联系方式")
  246. * @Param ("form",desc="包装形式")
  247. * @Param ("wharf_to",desc="装货码头")
  248. * @Param ("wharf_for",desc="交货码头")
  249. * @Param ("file",desc="图片/视频,[{type:'image或video',src:'http://xxxxx'}]")
  250. * @Param ("item",desc="['id:修改则传ID','name 商品名称','weight 重量']")
  251. * @return void
  252. */
  253. public function ocean_order(\think\Validate $validate,Request $request){
  254. $request->offsetSet('file',json_decode($request['file'],true)?:[]);
  255. $request->offsetSet('item',json_decode($request['item'],true)?:[]);
  256. $admin_id = $this->uuid();
  257. $data=$this->_vali([
  258. 'real_name.require'=>'联系人不能为空',
  259. 'mobile.require'=>'联系方式不能为空',
  260. 'form.require'=>'包装形式必须',
  261. 'wharf_to.require'=>'装货码头必须',
  262. 'wharf_for.require'=>'交货码头必须',
  263. // 'file.array'=>'图片必须是数组',
  264. 'item.require'=>'商品必须',
  265. 'item.array'=>'商品必须是数组',
  266. ]);
  267. $data['item'] = json_decode($request['item'],true);
  268. if(input('file')){
  269. $data['file'] = json_decode($request['file'],true);
  270. foreach ($data['file']??[] as $image){
  271. $validate->failException()->check($image,[
  272. 'type|图片类型'=>['require','in:image,video'],
  273. 'src|图片链接'=>['require','url'],
  274. ]);
  275. }
  276. }
  277. foreach ($data['item'] as $item){
  278. $validate->failException()->check($item,[
  279. 'name|名称'=>['require'],
  280. 'weight|数量吨位'=>['require'],
  281. ]);
  282. }
  283. $purchase_data = [
  284. 'admin_id'=>$admin_id->id,
  285. 'real_name'=>$data['real_name'],
  286. 'mobile'=>$data['mobile'],
  287. 'form'=>$data['form'],
  288. 'wharf_to'=>$data['wharf_to'],
  289. 'wharf_for'=>$data['wharf_for'],
  290. 'file'=>$data['file']??[],
  291. 'longitude'=>$admin_id->merchant->longitude,
  292. 'latitude'=>$admin_id->merchant->latitude,
  293. ];
  294. Db::startTrans();
  295. try {
  296. if($request['id']){
  297. $order_id=ShopOcean::where('admin_id',$admin_id['id'])->where('id',$request['id'])->findOrFail();
  298. $order_id->save($purchase_data);
  299. $item_ids=[];
  300. foreach ($data['item'] as $item){
  301. if(!empty($item['id'])){
  302. $itemModel=$order_id->items()->where('id',$item['id'])->findOrFail();
  303. $itemModel->save($item);
  304. }else{
  305. $itemModel=$order_id->items()->save($item);
  306. }
  307. $item_ids[]=$itemModel['id'];
  308. }
  309. $order_id->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  310. }else {
  311. $order_id = ShopOcean::mk()->create($purchase_data);
  312. $order_id->items()->saveAll($data['item']);
  313. }
  314. // 提交事务
  315. Db::commit();
  316. $this->success('已发布');
  317. } catch (ErrorException $e) {
  318. // 回滚事务
  319. Db::rollback();
  320. $this->success('发布失败');
  321. }
  322. }
  323. /**
  324. * @Title ("外协订单发布或编辑")
  325. * @Method ("post")
  326. * @Param ("id",desc="如果是编辑则传ID")
  327. * @Param ("real_name",desc="联系人")
  328. * @Param ("mobile",desc="联系方式")
  329. * @Param ("standard_tech",desc="生产技术标准")
  330. * @Param ("standard_check",desc="生产验收标准")
  331. * @Param ("post_time",desc="交货时间")
  332. * @Param ("post_address",desc="交货地址")
  333. * @Param ("file",desc="图片/视频,[{type:'image或video',src:'http://xxxxx'}]")
  334. * @Param ("item",desc="二维数组,[{'id:修改则传ID',name:'产品名称',spec:'规格',number:'数量',enclosure:[{filename:'文件名',fileurl:'文件链接'}]}]")
  335. * @return void
  336. */
  337. public function coordination_order(\think\Validate $validate,Request $request){
  338. $request->offsetSet('file',json_decode($request['file'],true)?:[]);
  339. $request->offsetSet('item',json_decode($request['item'],true)?:[]);
  340. $admin_id = $this->uuid();
  341. $data=$this->_vali([
  342. 'real_name.require'=>'联系人不能为空',
  343. 'mobile.require'=>'联系方式不能为空',
  344. // 'standard_tech.require'=>'生产技术标准',
  345. // 'standard_check.require'=>'生产验收标准',
  346. 'post_time.require'=>'交货时间不能为空',
  347. 'post_address.require'=>'交货单地址不能为空',
  348. 'item.require'=>'商品参数不能为空',
  349. // 'file.array'=>'图片必须是数组',
  350. ]);
  351. $data['item'] = json_decode($request['item'],true);
  352. if(input('file')) {
  353. $data['file'] = json_decode($request['file'],true);
  354. foreach ($data['file'] ?? [] as $image) {
  355. $validate->failException()->check($image, [
  356. 'type|图片类型' => ['require', 'in:image,video'],
  357. 'src|图片链接' => ['require', 'url'],
  358. ]);
  359. }
  360. }
  361. foreach ($data['item'] as $item){
  362. $validate->failException()->check($item,[
  363. 'name|名称'=>['require'],
  364. 'spec|规格'=>['require'],
  365. 'number|数量'=>['require','integer'],
  366. // 'enclosure|附件'=>['array',],
  367. ]);
  368. // if(!empty($item['enclosure'])){
  369. // foreach ($item['enclosure'] as $enclosure){
  370. // $validate->failException()->check($enclosure,[
  371. // 'filename|文件名'=>['require'],
  372. // 'fileurl|文件地址'=>['require','url',],
  373. // ]);
  374. // }
  375. // }
  376. }
  377. $purchase_data = [
  378. 'admin_id'=>$admin_id->id,
  379. 'real_name'=>$data['real_name'],
  380. 'mobile'=>$data['mobile'],
  381. // 'standard_tech'=>$data['standard_tech'],
  382. // 'standard_check'=>$data['standard_check'],
  383. 'post_time'=>$data['post_time'],
  384. 'post_address'=>$data['post_address'],
  385. 'longitude'=>$admin_id->merchant->longitude,
  386. 'latitude'=>$admin_id->merchant->latitude,
  387. 'file'=>$data['file']??[],
  388. 'create_time'=>date('Y-m-d H:i:s')
  389. ];
  390. input('standard_tech')?$purchase_data['standard_tech']=input('standard_tech'):'';
  391. input('standard_check')?$purchase_data['standard_check']=input('standard_check'):'';
  392. Db::startTrans();
  393. try {
  394. if($request['id']){
  395. $order=ShopCoordination::where('id',$request['id'])->where('admin_id',$admin_id['id'])->findOrFail();
  396. $order->save($purchase_data);
  397. $item_ids=[];
  398. foreach ($data['item'] as $item){
  399. if(!empty($item['id'])){
  400. $itemModel=$order->items()->where('id',$item['id'])->findOrFail();
  401. $itemModel->save($item);
  402. }else{
  403. $itemModel=$order->items()->save($item);
  404. }
  405. $item_ids[]=$itemModel['id'];
  406. }
  407. $order->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  408. }else {
  409. $order = ShopCoordination::mk()->create($purchase_data);
  410. $order->items()->saveAll($data['item']);
  411. }
  412. // 提交事务
  413. Db::commit();
  414. $this->success('已发布');
  415. } catch (ErrorException $e) {
  416. // 回滚事务
  417. Db::rollback();
  418. $this->success('发布失败');
  419. }
  420. }
  421. public function order_no($key,$uuid){
  422. $time_str = date('YmdHi');
  423. $order_no = $key.$time_str . sprintf("%04d", $uuid);
  424. return $order_no;
  425. }
  426. public function uuid(){
  427. $purchase_model = new Common($this->app);
  428. $uuid = $purchase_model->uuid();
  429. return $uuid;
  430. }
  431. /**
  432. * @Title("采购订单列表")
  433. * @Param ("page",desc="第几页")
  434. * @Param ("limit",desc="每页几条")
  435. * @Param ("status",desc="1未报价2已报价3已匹配")
  436. * @Param ("tab",desc="1采购2生产3外协4海运")
  437. * @Returned("参考用户端采购订单列表")
  438. */
  439. public function list(Request $request){
  440. $status=$request['status'];
  441. $tab=$request['tab']?:1;
  442. $query=ShopPurchase::getQuery($tab);
  443. $list=$query
  444. ->where('admin_id',$this->uuid()->id)
  445. ->when($tab,function ($query) use ($tab,$status){
  446. if ($tab==1){
  447. $query ->with(['items','items.offers']);
  448. $query->when($status,function ($query)use($status){
  449. if($status == 1){
  450. $query->whereExists('select * from shop_purchase_item where shop_purchase_item.purchase_id=shop_purchase.id and not exists(select * from shop_purchase_offer where shop_purchase_offer.item_id=shop_purchase_item.id)');
  451. }elseif ($status == 2){
  452. $query->whereExists('select * from shop_purchase_item where shop_purchase_item.purchase_id=shop_purchase.id and exists(select * from shop_purchase_offer where shop_purchase_offer.item_id=shop_purchase_item.id)');
  453. }elseif($status == 3){
  454. $query->whereExists('select * from shop_purchase_item where shop_purchase_item.purchase_id=shop_purchase.id and offer_id is not null');
  455. }else{
  456. $this->error('未知参数');
  457. }
  458. });
  459. }else{
  460. $query ->with(['items','offers']);
  461. }
  462. })
  463. ->when($tab!=1&&$status,['status'=>$status])
  464. ->order('create_time','desc')
  465. ->paginate($request['limit']?:15);
  466. $this->success('',$list);
  467. }
  468. /**
  469. *@Title("详情")
  470. * @Param ("tab",desc="1采购2生产3外协4海运")
  471. * @Param ("id",desc="订单ID")
  472. * @Returned("is_matched",desc="是否已匹配")
  473. * @Returned("参考用户端采购订单列表")
  474. */
  475. public function show(Request $request){
  476. DataUser::$copyRealName=true;
  477. $data=$this->_vali([
  478. 'id.require'=>'ID必须',
  479. 'tab.require'=>'tab必须',
  480. ],'get');
  481. if($data['tab']==1) {
  482. $model = ShopPurchase::getQuery($data['tab'])
  483. ->with(['items','items.offers','items.offers.user'])
  484. ->findOrFail($data['id']);
  485. foreach ($model['items'] as &$v){
  486. $arr = [];
  487. foreach ($v['offers'] as $aa=>&$a){
  488. if($a['id']!=$v['offer_id'] && $v['offer_id']!=''){
  489. // unset($v['offers'][$aa]);
  490. }else{
  491. $arr[] = $v['offers'][$aa];
  492. // return $v['offers'][$aa];
  493. }
  494. $a['amounts'] = ShopPurchaseOfferAmount::mk()
  495. ->where('user_id',$a['user_id'])
  496. ->where('offer_id',$a['id'])
  497. ->order('id asc')
  498. ->select();
  499. }
  500. $v['offers'] = $arr;
  501. }
  502. }else{
  503. $model = ShopPurchase::getQuery($data['tab'])
  504. ->with(['items','offers','offers.user'])
  505. ->findOrFail($data['id']);
  506. }
  507. //return json($model);
  508. $this->success('',$model);
  509. }
  510. /**
  511. *@Title("取消订单")
  512. * @Param ("tab",desc="1采购2生产3外协4海运")
  513. * @Param ("id",desc="订单ID")
  514. */
  515. public function cancel(Request $request){
  516. $data=$this->_vali([
  517. 'id.require'=>'ID必须',
  518. 'tab.require'=>'tab必须',
  519. ],'get');
  520. $admin=$this->uuid();
  521. $model=ShopPurchase::getQuery($data['tab'])->where('admin_id',$admin['id'])->findOrFail($data['id']);
  522. $model['status']=0;
  523. $model->save();
  524. $this->success('操作成功');
  525. }
  526. /**
  527. *@Title("提醒完成")
  528. * @Param ("user_id",desc="用户ID")
  529. * @Param ("order_no",desc="订单号")
  530. */
  531. public function reminder(Request $request){
  532. $admin_id = $this->uuid();
  533. $data=$this->_vali([
  534. 'user_id.require'=>'user_id必须',
  535. 'order_no.require'=>'订单号必须'
  536. ],'get');
  537. $alias = DataUser::mk()->where('id',$data['user_id'])->value('jgalias');
  538. // $content = '您报价的订单已匹配完成,请及时查看';
  539. $content = '商家“'.$admin_id->nickname.'”提醒您要及时完成生产订单哦,订单号:'.$data['order_no'];
  540. $alias ? jgpush($content,$alias) : '';
  541. setusermessage($data['user_id'],'匹配通知',$content);
  542. $this->success('操作成功');
  543. }
  544. /**
  545. * @Title("匹配订单")
  546. * @Method("POST")
  547. * @Param ("tab",desc="1采购2生产3外协4海运")
  548. * @Param ("id",desc="订单ID")
  549. * @Param ("offer_id",desc="接单ID")
  550. */
  551. public function match(Request $request){
  552. $data=$this->_vali([
  553. 'id.require'=>'ID必须',
  554. 'tab.require'=>'tab必须',
  555. 'offer_id.require'=>'offer必须',
  556. ]);
  557. $admin=$this->uuid();
  558. $tab = $data['tab'];
  559. $id = $data['id'];
  560. Db::startTrans();
  561. // $admin['id'] = '10010';
  562. $model=ShopPurchase::getItem($data['tab'],$admin['id'])->lock(true)
  563. ->where(function ($query) use ($tab,$id){
  564. // if ($tab==1){
  565. // $query->where('purchase_id',$id);
  566. // }else{
  567. $query->where('id',$id);
  568. // }
  569. })
  570. ->findOrEmpty();
  571. $model_id = $model['id'];
  572. //dump($model->toArray());die;
  573. if($model['is_matched']){
  574. Db::rollback();
  575. $this->error('该订单已匹配');
  576. }
  577. // $hasMatch=ShopPurchase::getItem($data['tab'])->where('offer_id',$data['offer_id'])->value('id');
  578. $hasMatch=ShopPurchase::getItem($data['tab'])->where('id',$data['id'])->value('offer_id');
  579. if($hasMatch){
  580. Db::rollback();
  581. $this->error('该接单已被其他订单匹配');
  582. }
  583. $offer=ShopPurchase::getOffer($data['tab'])->lock(true)
  584. // ->where('user_id',$data['offer_id'])
  585. ->when($tab,function ($querys) use ($tab,$model_id){
  586. switch ($tab){
  587. case 1:
  588. $querys->where('item_id',$model_id);
  589. break;
  590. case 2:
  591. $querys->where('production_id',$model_id);
  592. break;
  593. case 3:
  594. $querys->where('corrdination_id',$model_id);
  595. break;
  596. case 4:
  597. $querys->where('ocea_id',$model_id);
  598. break;
  599. }
  600. })
  601. ->findOrEmpty();
  602. if($request['tab']==1){
  603. if ($offer->item_id != $model['id']) {
  604. Db::rollback();
  605. $this->error('该接单不属于此订单');
  606. }
  607. }else {
  608. if ($offer->order_id != $model['id']) {
  609. Db::rollback();
  610. $this->error('该接单不属于此订单');
  611. }
  612. }
  613. $model['offer_id']=$data['offer_id'];
  614. if($data['tab']==1){
  615. $main=$model->main;
  616. $main['status']=3;
  617. $main->save();
  618. }else{
  619. $model['status']=3;
  620. }
  621. $model->save();
  622. Db::commit();
  623. $url = setword($data['tab']+1,$data['id']);
  624. ShopPurchase::getItem($data['tab'])->where('id',$model['id'])->update(['down_word'=>$url]);
  625. $this->success('匹配成功');
  626. }
  627. /**
  628. * @Title ("发送留言")
  629. * @Method("post")
  630. * @Param ("id",desc="订单ID")
  631. * @Param ("tab",desc="1采购2生产3外协4海运")
  632. * @Param ("user_id",desc="发送给的用户id")
  633. * @Param ("content",desc="内容")
  634. */
  635. public function msg_send(Request $request){
  636. $this->_vali([
  637. 'id.require'=>'id必须',
  638. 'user_id.require'=>'用户必须',
  639. 'tab.require'=>'tab必须',
  640. 'content.require'=>'tab必须',
  641. 'content.max:500'=>'内容过长',
  642. ]);
  643. $model=ShopPurchase::getQuery($request['tab'])->findOrFail($request['id']);
  644. $user=DataUser::where('id',$request['user_id'])->findOrFail();
  645. $msg=DataUserLeaveMessage::create([
  646. 'uuid'=>$this->uuid()->id,
  647. 'uuid2'=>$user['id'],
  648. 'content'=>$request['content'],
  649. 'type'=>DataUserLeaveMessage::getMsgType($model->getTable()),
  650. 'link_id'=>$model['id'],
  651. ]);
  652. $this->success('发送成功',$msg);
  653. }
  654. /**
  655. * @Title ("留言列表")
  656. * @Param ("id",desc="订单ID")
  657. * @Param ("tab",desc="1采购2生产3外协4海运")
  658. * @Param ("user_id",desc="要查看的用户id")
  659. * @Returned ("user.headimg",desc="用户头像")
  660. * @Returned ("merchant.headimg",desc="商家头像")
  661. * @Returned ("is_mine",desc="是否是自己的留言")
  662. */
  663. public function msg_get(Request $request){
  664. $this->_vali([
  665. 'id.require'=>'id必须',
  666. 'tab.require'=>'tab必须',
  667. ],'get');
  668. $model=ShopPurchase::getQuery($request['tab'])->findOrFail($request['id']);
  669. $admin_id = $model['admin_id'];
  670. $user_id = $request['user_id'];
  671. $messagelist = DataUserLeaveMessage::mk()
  672. ->where('type',DataUserLeaveMessage::getMsgType($model->getTable()))
  673. ->whereRaw("(uuid=$user_id and uuid2=$admin_id) or (uuid2=$user_id and uuid=$admin_id)")
  674. ->where('link_id',$model['id'])
  675. ->field('id,uuid,uuid2,content,create_at')
  676. ->select()->toArray();
  677. $this->success('成功',$messagelist);
  678. }
  679. /**
  680. * @Title("获取订单配置")
  681. * @Returned("packing",desc="包装形式数组")
  682. */
  683. public function config(){
  684. $config=[];
  685. $config['packing']=systemConfig('merchant_config.packing')?:[];
  686. $this->success('',$config);
  687. }
  688. }