ShopCoor.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\controller;
  3. use app\data\model\DataMerchants;
  4. use app\data\model\ShopCoordination as Model;
  5. use app\data\model\DataUser;
  6. use Carbon\Carbon;
  7. use think\admin\Controller;
  8. use app\data\model\SystemUser;
  9. use think\admin\service\AdminService;
  10. use think\db\Query;
  11. use think\facade\Db;
  12. use think\Request;
  13. /**
  14. * 外协订单管理
  15. * Class Xw
  16. * @package app\admin\controller\ShopCoor
  17. * http://www.zjnk.com/fileserver/News/04b7854f-bb0a-458b-b359-ce948acfea09.JPG
  18. */
  19. class ShopCoor extends Controller
  20. {
  21. /**
  22. * 列表
  23. * @auth true
  24. * @menu true
  25. */
  26. public function index(Request $request){
  27. if(AdminService::getUserId()==10000){
  28. $admin_id = [];
  29. }else{
  30. $admin_id['admin_id']=AdminService::getUserId();
  31. }
  32. $this->title='外协订单管理';
  33. $this->assign('status',\app\data\model\ShopPurchase::getStatus());
  34. $result = Model::mQuery()
  35. ->with(['merchant'])
  36. ->when($request['name'],function (Query $query)use ($request){
  37. $query->hasWhere('merchant',function (Query $query)use ($request){
  38. $query->whereLike('name',"%{$request['name']}%");
  39. });
  40. })->where($admin_id)
  41. ->equal('status')
  42. ->dateBetween('create_time');
  43. // ->layTable();
  44. $this->amount = $result->db()->sum('amount');
  45. $result->page();
  46. }
  47. /**
  48. * 详情
  49. * @auth true
  50. * @menu true
  51. */
  52. public function detail(Request $request,Model $purchase){
  53. DataUser::$copyRealName=true;
  54. $this->assign('row',$purchase
  55. ->with(['merchant','items','offers','offers.user'])
  56. ->where('id',$request['id'])->findOrFail());
  57. $this->fetch('form');
  58. }
  59. /**
  60. * 发布
  61. * @auth true
  62. * @menu true
  63. */
  64. public function add(Request $request,\think\Validate $validate){
  65. $this->title='发布订单';
  66. $this->assign('vo',null);
  67. if($request->isGet()) {
  68. $this->fetch();
  69. }else{
  70. $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
  71. if(!$merchant){
  72. $this->error('您不是商家无法发布');
  73. }
  74. $data=$request->post();
  75. $item=$data['item']??[];
  76. foreach ($item as $i){
  77. $make=$validate->check($i,[
  78. 'name'=>['require',],
  79. 'spec'=>['require',],
  80. 'number'=>['require','integer'],
  81. // 'enclosure'=>['require',],
  82. // 'price'=>['require','float'],
  83. ]);
  84. if(!$make){
  85. $this->error('请填写正确的商品信息');
  86. }
  87. }
  88. app()->db->startTrans();
  89. unset($data['item']);
  90. $data['admin_id']=AdminService::getUserId();
  91. $data['longitude']=$merchant['longitude']??null;
  92. $data['latitude']=$merchant['latitude']??null;
  93. // $data['amount']=min(array_column($item,'price'));
  94. $data['type'] =1;
  95. $order= \app\data\model\ShopCoordination::create($data);
  96. $order->items()->saveAll($item);
  97. app()->db->commit();
  98. $this->success('保存成功');
  99. }
  100. }
  101. /**
  102. * 发布
  103. * @auth true
  104. * @menu true
  105. */
  106. public function edit(Request $request,Model $purchase,\think\Validate $validate){
  107. $this->title='发布订单';
  108. $this->assign('vo',null);
  109. if($request->isGet()) {
  110. $row = $purchase
  111. ->with(['merchant','items','offers','favourite','ppoffer'])
  112. ->where('id',$request['id'])->findOrFail();
  113. if (!$row['merchant']){
  114. $array = ['company'=>'平台发布'];
  115. $row['merchant'] = $array;
  116. }
  117. // var_dump($row['form']);die;
  118. // $row['fm'] = explode(',',$row['form']);
  119. //$row['images'] = $row['file'];
  120. if($row['file'] == '' || $row['file'] == null){
  121. $row['file'] = [];
  122. }
  123. $this->assign('row',$row);
  124. $this->fetch('edit');
  125. }else{
  126. $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
  127. if(!$merchant){
  128. $this->error('您不是商家无法发布');
  129. }
  130. $data=$request->post();
  131. $item=$data['item']??[];
  132. foreach ($item as $i){
  133. $make=$validate->check($i,[
  134. 'name'=>['require',],
  135. 'spec'=>['require',],
  136. 'number'=>['require','integer'],
  137. // 'enclosure'=>['require',],
  138. // 'price'=>['require','float'],
  139. ]);
  140. if(!$make){
  141. $this->error('请填写正确的商品信息');
  142. }
  143. }
  144. app()->db->startTrans();
  145. unset($data['item']);
  146. $data['admin_id']=AdminService::getUserId();
  147. $data['longitude']=$merchant['longitude']??null;
  148. $data['latitude']=$merchant['latitude']??null;
  149. // $data['amount']=min(array_column($item,'price'));
  150. $data['type'] =1;
  151. if(!$data['offer_id']){
  152. unset($data['offer_id']);
  153. }
  154. if(isset($data['file'])){
  155. // $data['file'] = $data['images'];
  156. }else{
  157. $data['file'] = null;
  158. }
  159. // $order= \app\data\model\ShopCoordination::create($data);
  160. $order=Model::update($data);
  161. foreach ($item as &$v){
  162. if(isset($v['enclosure'])){
  163. $v['enclosure'] = $v['enclosure'];
  164. }else{
  165. $v['enclosure'] = [];
  166. }
  167. }
  168. $order->items()->saveAll($item);
  169. app()->db->commit();
  170. $this->success('保存成功');
  171. }
  172. }
  173. /**
  174. * 取消订单
  175. * @auth true
  176. * @menu true
  177. */
  178. public function cancellation(Request $request, \app\data\model\ShopCoordination $purchase){
  179. $purchase_=$purchase->where('id',$request['id'])->findOrFail();
  180. $purchase_['status']=0;
  181. $purchase_->save();
  182. $this->success('取消成功');
  183. }
  184. }