ShopProduction.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. $row['images'] = $row['file'];
  121. if($row['images'] == '' || $row['images'] == null){
  122. $row['images'] = [];
  123. }
  124. $this->assign('row',$row);
  125. $this->fetch('edit');
  126. }elseif ($this->request->isPost()){
  127. $merchant=DataMerchants::getByAdmin(AdminService::getUserId());
  128. if(!$merchant){
  129. $this->error('您不是商家无法操作');
  130. }
  131. $data=$request->post();
  132. $item=$data['item']??[];
  133. foreach ($item as $i){
  134. $make=$validate->check($i,[
  135. 'name'=>['require',],
  136. 'spec'=>['require',],
  137. 'number'=>['require','integer'],
  138. // 'standard'=>['require',],
  139. // 'price'=>['require','float'],
  140. // 'enclosure'=>['require',],
  141. ]);
  142. if(!$make){
  143. $this->error('请填写正确的商品信息');
  144. }
  145. }
  146. // print_r($data);
  147. // exit();
  148. app()->db->startTrans();
  149. unset($data['item']);
  150. $data['admin_id']=AdminService::getUserId();
  151. $data['longitude']=$merchant['longitude']??null;
  152. $data['latitude']=$merchant['latitude']??null;
  153. // $data['amount']=min(array_column($item,'price'));
  154. $data['type'] =1;
  155. // print_r($data);
  156. // exit();
  157. //$order= \app\data\model\ShopProduction::create($data);
  158. if(!$data['offer_id']){
  159. unset($data['offer_id']);
  160. }
  161. if(isset($data['images'])){
  162. $data['file'] = $data['images'];
  163. }else{
  164. $data['file'] = null;
  165. }
  166. // if(count($data['file']) <= 0){
  167. // $data['file'] = null;
  168. // }
  169. $order=Model::update($data);
  170. // $order->items()->saveAll($item);
  171. app()->db->commit();
  172. $item_ids=[];
  173. foreach ($item as &$v){
  174. $item_data = [
  175. 'production_id'=>$data['id'],
  176. 'name'=>$v['name'],
  177. 'spec'=>$v['spec'],
  178. 'number'=>$v['number'],
  179. // 'standard'=>$v['standard'],
  180. // 'price'=>$v['price'],
  181. ];
  182. if(isset($v['enclosure'])){
  183. $item_data['enclosure'] = $v['enclosure'];
  184. }else{
  185. $item_data['enclosure'] = [];
  186. }
  187. if(!empty($v['id'])){
  188. $itemModel=$order->items()->where('id',$v['id'])->find();
  189. if(!$itemModel){
  190. $this->error('商品不存在');
  191. }
  192. $itemModel->save($item_data);
  193. }else{
  194. $itemModel=$order->items()->save($item_data);
  195. }
  196. $item_ids[]=$itemModel['id'];
  197. }
  198. // $order = $order->items()->whereNotIn('id',$item_ids)->select()->each(function ($d){$d->delete();});
  199. // ShopProductionOffer::where('order_id',$data['id'])
  200. // ->whereNotIn('item_id',$item_ids)
  201. // ->select()->each(function ($m){$m->delete();});
  202. $this->success('保存成功');
  203. }
  204. }
  205. /**
  206. * 取消订单
  207. * @auth true
  208. * @menu true
  209. */
  210. public function cancellation(Request $request,Model $purchase){
  211. $purchase_=$purchase->where('id',$request['id'])->findOrFail();
  212. $purchase_['status']=0;
  213. $purchase_->save();
  214. $this->success('取消成功');
  215. }
  216. }