ShopProduction.php 7.2 KB

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