PriceModelController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Imports\ExpressImport;
  4. use App\Imports\OwnerPriceLogisticDetailImport;
  5. use App\Owner;
  6. use App\OwnerOutStorageRule;
  7. use App\OwnerPriceExpress;
  8. use App\OwnerPriceExpressProvince;
  9. use App\OwnerPriceLogistic;
  10. use App\OwnerPriceOperation;
  11. use App\Services\common\ExportService;
  12. use App\Services\OwnerOutStorageRuleService;
  13. use App\Services\OwnerPriceOperationService;
  14. use Illuminate\Database\Eloquent\Builder;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\Gate;
  18. use Illuminate\Support\Facades\Validator;
  19. use Maatwebsite\Excel\Facades\Excel;
  20. class PriceModelController extends Controller
  21. {
  22. public function storageIndex()
  23. {
  24. if(!Gate::allows('计费模型-仓储')){ return redirect('denied'); }
  25. $models = app('OwnerStoragePriceModelService')->paginate(["unit"]);
  26. return response()->view('maintenance.priceModel.storage.index',compact("models"));
  27. }
  28. public function storageCreate()
  29. {
  30. if(!Gate::allows('计费模型-仓储-录入')){ return redirect('denied'); }
  31. $units = app('UnitService')->getSelection();
  32. return response()->view('maintenance.priceModel.storage.create',compact("units"));
  33. }
  34. public function storageStore(Request $request)
  35. {
  36. if(!Gate::allows('计费模型-仓储-录入')){ return redirect('denied'); }
  37. $this->storageValidator($request->input())->validate();
  38. app('OwnerStoragePriceModelService')->create($request->input());
  39. return response()->redirectTo('maintenance/priceModel/storage')->with('successTip',"创建成功!");
  40. }
  41. public function storageEdit($id)
  42. {
  43. if(!Gate::allows('计费模型-仓储-编辑')){ return redirect('denied'); }
  44. $model = app('OwnerStoragePriceModelService')->find($id);
  45. $units = app('UnitService')->getSelection();
  46. return response()->view('maintenance.priceModel.storage.create',compact("units","model"));
  47. }
  48. public function storageUpdate(Request $request)
  49. {
  50. if(!Gate::allows('计费模型-仓储-编辑')){ return redirect('denied'); }
  51. app('OwnerStoragePriceModelService')->update(["id"=>$request->input("id")],[
  52. "counting_type" => $request->input("counting_type"),
  53. "using_type" => $request->input("using_type"),
  54. "minimum_area" => $request->input("minimum_area"),
  55. "price" => $request->input("price"),
  56. "discount_type" => $request->input("discount_type"),
  57. "discount_value"=> $request->input("discount_value"),
  58. "unit_id" => $request->input("unit_id"),
  59. ]);
  60. return response()->redirectTo('maintenance/priceModel/storage')->with('successTip',"更新成功!");
  61. }
  62. public function storageDestroy($id)
  63. {
  64. app("OwnerStoragePriceModelService")->destroy($id);
  65. return ["success"=>true];
  66. }
  67. private function storageValidator(array $params)
  68. {
  69. return Validator::make($params,[
  70. 'counting_type'=>['required'],
  71. 'using_type'=>['required'],
  72. 'minimum_area'=>['nullable','numeric','min:0'],
  73. 'discount_value'=>['nullable','numeric','min:0'],
  74. 'price'=>['required','numeric','min:0'],
  75. 'discount_type'=>['required'],
  76. 'unit_id'=>['required','integer'],
  77. ],[
  78. 'required'=>':attribute 为必填项',
  79. 'min'=>':attribute 不得小于0',
  80. 'integer'=>':attribute 未选择',
  81. ],[
  82. 'counting_type' =>"计费类型",
  83. 'using_type' =>"用仓类型",
  84. 'minimum_area' =>"最低起租面积",
  85. 'price' =>"单价",
  86. 'discount_type' =>"减免类型",
  87. 'discount_value'=>"减免值",
  88. 'unit_id' =>"单位",
  89. ]);
  90. }
  91. public function operationIndex(Request $request){
  92. if(!Gate::allows('计费模型-作业-查询')){ return redirect('denied'); }
  93. $features = app("FeatureService")->getMapArray();
  94. OwnerPriceOperation::$features = $features;
  95. $models = app('OwnerPriceOperationService')->paginate($request->input(),["ownerPriceOperationOwners","ownerInStorageRule"=>function($query){$query->with("unit");}])->append("featureFormat");
  96. $owners = app("OwnerService")->getSelection();
  97. return response()->view('maintenance.priceModel.operation.index',compact("models","owners"));
  98. }
  99. /* 获取出库模型规则 */
  100. public function operationGetOutStorageRule(Request $request)
  101. {
  102. if(!Gate::allows('计费模型-作业-查询')){ return ["success"=>false,"data"=>"无权操作"]; }
  103. /** @var OwnerOutStorageRuleService $service */
  104. $service = app('OwnerOutStorageRuleService');
  105. $ownerOutStorageRules = $service->get(["owner_price_operation_id"=>$request->input("id")],["unit"],true)->append("featureFormat");
  106. return ["success"=>true,"data"=>$ownerOutStorageRules];
  107. }
  108. /* 修改出库模型规则 */
  109. public function updateOutStorageRule(Request $request)
  110. {
  111. if(!Gate::allows('计费模型-作业-编辑')){ return ["success"=>false,"data"=>"无权操作"]; }
  112. /** @var OwnerOutStorageRuleService $service */
  113. $service = app('OwnerOutStorageRuleService');
  114. $row = $service->update(["id"=>$request->input("id")],[
  115. "amount"=>$request->input("amount"),
  116. "unit_id"=>$request->input("unit_id"),
  117. "priority"=>$request->input("priority"),
  118. "unit_price"=>$request->input("unit_price")]);
  119. if ($row == 1) return ["success"=>true];
  120. return ["success"=>false,"data"=>"受影响数据数为:".$row];
  121. }
  122. public function createOutStorageRule(Request $request)
  123. {
  124. if(!Gate::allows('计费模型-作业-编辑')){ return ["success"=>false,"data"=>"无权操作"]; }
  125. /** @var OwnerOutStorageRuleService $service */
  126. $service = app('OwnerOutStorageRuleService');
  127. switch ($request->input("strategy")){
  128. case "起步":
  129. $c = $service->isExist(["owner_price_operation_id"=>$request->input("owner_price_operation_id"),"strategy"=>"起步"]);
  130. if ($c > 0)return ["success"=>false,"data"=>"已存在起步策略"];
  131. break;
  132. case "默认":
  133. $c = $service->isExist(["owner_price_operation_id"=>$request->input("owner_price_operation_id"),"strategy"=>"默认"]);
  134. if ($c > 0)return ["success"=>false,"data"=>"已存在默认策略"];
  135. break;
  136. }
  137. $data = $service->create($request->input());
  138. $data->load("unit");
  139. return ["success"=>true,"data"=>$data];
  140. }
  141. public function getFeatures(Request $request)
  142. {
  143. return ["success"=>true,"data"=>app("FeatureService")->translationFeature($request->input("feature"))];
  144. }
  145. public function addFeature(Request $request)
  146. {
  147. if(!Gate::allows('计费模型-作业-编辑')){ return ["success"=>false,"data"=>"无权操作"]; }
  148. $id = $request->input("id");
  149. $features = $request->input("features");
  150. if (!$id || !$features)return ["success"=>false,"data"=>"非法参数"];
  151. $result = app("FeatureService")->analysisFeature($features);
  152. $feature = $result["feature"];
  153. $stack = [];
  154. if ($feature && ($feature[0]=='|' || $feature[0]=='&'))$feature=substr($feature,1);
  155. for ($i=0;$i<strlen($feature);$i++){
  156. if ($feature[$i] == '(')array_unshift($stack,'(');
  157. if ($feature[$i] == ')'){
  158. if (count($stack) == 0)return ["success"=>false,"data"=>"组标记错误,起始与结束标记必须对应"];
  159. array_shift($stack);
  160. }
  161. }
  162. if (count($stack) > 0)return ["success"=>false,"data"=>"组标记错误,起始与结束标记必须对应"];
  163. $row = app('OwnerOutStorageRuleService')->update(["id"=>$id],["feature"=>$feature]);
  164. if ($row != 1)return ["success"=>false,"data"=>"影响了“".$row."”行"];
  165. OwnerOutStorageRule::$features = $result["map"];
  166. $rule = app('OwnerOutStorageRuleService')->find($id)->append("featureFormat");
  167. return ["success"=>true,"data"=>["featureFormat"=>$rule->featureFormat,"feature"=>$feature]];
  168. }
  169. public function getFeature(Request $request)
  170. {
  171. $features = $request->input("features");
  172. if (!$features)return ["success"=>false,"data"=>"非法参数"];
  173. $result = app("FeatureService")->analysisFeature($features);
  174. $feature = $result["feature"];
  175. $stack = [];
  176. if ($feature && ($feature[0]=='|' || $feature[0]=='&'))$feature=substr($feature,1);
  177. for ($i=0;$i<strlen($feature);$i++){
  178. if ($feature[$i] == '(')array_unshift($stack,'(');
  179. if ($feature[$i] == ')'){
  180. if (count($stack) == 0)return ["success"=>false,"data"=>"组标记错误,起始与结束标记必须对应"];
  181. array_shift($stack);
  182. }
  183. }
  184. if (count($stack) > 0)return ["success"=>false,"data"=>"组标记错误,起始与结束标记必须对应"];
  185. return ["success"=>true,"data"=>$feature];
  186. }
  187. public function operationDestroy($id)
  188. {
  189. if(!Gate::allows('计费模型-作业-删除')){ return ["success"=>false,"data"=>"无权操作"]; }
  190. $row = app("OwnerPriceOperationService")->destroy($id);
  191. if ($row == 1)return ["success"=>true];
  192. return ["success"=>false,"data"=>"影响了“".$row."”行"];
  193. }
  194. public function operationCreate(){
  195. if(!Gate::allows('计费模型-作业-录入')){ return redirect('denied'); }
  196. $owners = app("OwnerService")->getSelection();
  197. $units = app('UnitService')->getSelection();
  198. return response()->view('maintenance.priceModel.operation.create',compact("owners","units"));
  199. }
  200. public function operationStore(Request $request)
  201. {
  202. if(!Gate::allows('计费模型-作业-录入')){ return redirect('denied'); }
  203. $request->offsetSet("rules",json_decode($request->input("rules"),true));
  204. $request->offsetSet("owner_id",explode(',',$request->input("owner_id")));
  205. $this->operationValidator($request->input())->validate();
  206. //录入主表
  207. /** @var OwnerPriceOperationService $service */
  208. $service = app("OwnerPriceOperationService");
  209. $ownerPriceOperation = $service->create([
  210. "operation_type" => $request->input("operation_type"),
  211. "strategy" => $request->input("strategy"),
  212. "name" => $request->input("name"),
  213. "priority" => $request->input("priority"),
  214. "remark" => $request->input("remark"),
  215. "feature" => $request->input("feature"),
  216. ]);
  217. //录入子表
  218. if ($request->input("operation_type") == '入库') $service->insertRule([
  219. "owner_price_operation_id" => $ownerPriceOperation->id,
  220. "amount" => $request->input("rules")[0]["amount"],
  221. "unit_id" => $request->input("rules")[0]["unit_id"],
  222. "unit_price" => $request->input("rules")[0]["unit_price"],
  223. ],"入库");
  224. else{
  225. $insert = [];
  226. foreach ($request->input("rules") as $rule){
  227. $insert[] = [
  228. "owner_price_operation_id" => $ownerPriceOperation->id,
  229. "amount" => $rule["amount"],
  230. "unit_id" => $rule["unit_id"],
  231. "unit_price" => $rule["unit_price"],
  232. "strategy" => $rule["strategy"],
  233. "feature" => $rule["feature"],
  234. "priority" => $rule["priority"],
  235. ];
  236. }
  237. $service->insertRule($insert);
  238. }
  239. //录入中间表
  240. /** @var OwnerPriceOperation $model */
  241. if ($request->input("owner_id"))$model->ownerPriceOperationOwners()->sync($request->input("owner_id"));
  242. return response()->redirectTo("maintenance/priceModel/operation")->with("successTip","创建“".$request->input("name")."”成功");
  243. }
  244. public function operationEdit($id)
  245. {
  246. if(!Gate::allows('计费模型-作业-编辑')){ return redirect('denied'); }
  247. $model = app('OwnerPriceOperationService')->find($id,true,["ownerPriceOperationOwners"]);
  248. $owners = app("OwnerService")->getSelection();
  249. $units = app('UnitService')->getSelection();
  250. return response()->view('maintenance.priceModel.operation.create',compact("owners","units","model"));
  251. }
  252. public function operationUpdate($id,Request $request)
  253. {
  254. if(!Gate::allows('计费模型-作业-编辑')){ return redirect('denied'); }
  255. $request->offsetSet("rules",json_decode($request->input("rules"),true));
  256. $request->offsetSet("owner_id",explode(',',$request->input("owner_id")));
  257. $this->operationValidator($request->input(),$id)->validate();
  258. /** @var OwnerPriceOperationService $service */
  259. $service = app("OwnerPriceOperationService");
  260. $model = $service->find($id);
  261. $service->findUpdate($model,[
  262. "name" => $request->input("name"),
  263. "priority" => $request->input("priority"),
  264. "remark" => $request->input("remark"),
  265. "feature" => $request->input("feature"),
  266. ]);
  267. $service->destroyRule($id, $model->operation_type);
  268. //录入子表
  269. if ($request->input("operation_type") == '入库') $service->insertRule([
  270. "owner_price_operation_id" => $model->id,
  271. "amount" => $request->input("rules")[0]["amount"],
  272. "unit_id" => $request->input("rules")[0]["unit_id"],
  273. "unit_price" => $request->input("rules")[0]["unit_price"],
  274. ],"入库");
  275. else{
  276. $insert = [];
  277. foreach ($request->input("rules") as $rule){
  278. $insert[] = [
  279. "owner_price_operation_id" => $model->id,
  280. "amount" => $rule["amount"],
  281. "unit_id" => $rule["unit_id"],
  282. "unit_price" => $rule["unit_price"],
  283. "strategy" => $rule["strategy"],
  284. "feature" => $rule["feature"],
  285. "priority" => $rule["priority"],
  286. ];
  287. }
  288. $service->insertRule($insert);
  289. }
  290. //录入中间表
  291. /** @var OwnerPriceOperation $model */
  292. if ($request->input("owner_id"))$model->ownerPriceOperationOwners()->sync($request->input("owner_id"));
  293. return response()->redirectTo("maintenance/priceModel/operation")->with("successTip","修改“".$request->input("name")."”成功");
  294. }
  295. private function operationValidator(array $params, $id= null)
  296. {
  297. return Validator::make($params,[
  298. //required_with:id
  299. 'operation_type'=>['required'],
  300. 'owner_id'=>[function ($attribute, $value, $fail)use($params,$id) {
  301. if ($params["strategy"] == '默认'){
  302. $owners = Owner::query()->whereIn("id",$value)->withCount(["ownerPriceOperations"=>function($query)use($params,$id){
  303. if ($id)$query->where('id',"!=",$id);
  304. $query->where("strategy","默认")->where("operation_type",$params["operation_type"]);
  305. }])->get();
  306. $err = [];
  307. foreach ($owners as $owner){
  308. if ($owner->owner_price_operations_count > 0)$err[] = $owner->name;
  309. }
  310. if (count($err)>0)$fail("(".implode(',',$err).') 已经绑定'.$params["operation_type"].'的默认策略');
  311. }
  312. }],
  313. 'strategy'=>['required'],
  314. 'name'=>['required',$id?"unique:owner_price_operations,name,$id":'unique:owner_price_operations,name'],
  315. 'priority'=>['required','integer','min:0','max:100'],
  316. 'rules.*.strategy'=>['required_if:operation_type,出库'],
  317. 'rules.*.amount'=>['required',"integer"],
  318. 'rules.*.unit_id'=>['required','integer'],
  319. 'rules.*.unit_price'=>['required','numeric',"min:0"],
  320. 'rules.*.priority'=>['required_if:operation_type,出库','integer','min:0','max:100'],
  321. ],[
  322. 'required'=>':attribute 为必填项',
  323. 'min'=>':attribute 不得小于0',
  324. 'integer'=>':attribute 必须为整数',
  325. 'numeric'=>':attribute 必须为数字',
  326. 'max'=>':attribute 超出最大值',
  327. 'required_if'=>':attribute 操作类型为出库时不得为空',
  328. ],[
  329. 'operation_type' =>"操作类型",
  330. 'strategy' =>"计费策略",
  331. 'name' =>"名称",
  332. 'priority' =>"优先级",
  333. ]);
  334. }
  335. public function expressIndex(){
  336. if(!Gate::allows('计费模型-快递-查询')){ return redirect('denied'); }
  337. $models = app('OwnerPriceExpressService')->paginate();
  338. return response()->view('maintenance.priceModel.express.index',compact("models"));
  339. }
  340. public function expressGetDetail(Request $request)
  341. {
  342. if(!Gate::allows('计费模型-快递-查询')){ return ["success"=>false,"data"=>"无权操作"]; }
  343. $model = new OwnerPriceExpress();
  344. $model->id = $request->input("id");
  345. $model->load(["details"=>function($query){$query->with("province");}]);
  346. return ["success"=>true,"data"=>$model->details];
  347. }
  348. public function expressUpdateDetail(Request $request)
  349. {
  350. if(!Gate::allows('计费模型-快递-编辑')){ return ["success"=>false,"data"=>"无权操作"]; }
  351. $detail = $request->input("detail");
  352. if ($detail["id"]){
  353. app('OwnerPriceExpressService')->updateDetail(["id"=>$detail["id"]],[
  354. "additional_weight_price" => $detail["additional_weight_price"],
  355. "initial_weight_price" => $detail["initial_weight_price"],
  356. ]);
  357. }else{
  358. $row = app('OwnerPriceExpressService')->isExistDetail(["owner_price_express_id"=>$request->input("id"),"province_id"=>$detail["province_id"]]);
  359. if ($row>0)return ["success"=>false,"data"=>"已存在该省份计费模型"];
  360. /** @var OwnerPriceExpressProvince $detail */
  361. $detail = app('OwnerPriceExpressService')->createDetail([
  362. "owner_price_express_id" => $request->input("id"),
  363. "province_id" => $detail["province_id"],
  364. "additional_weight_price" => $detail["additional_weight_price"],
  365. "initial_weight_price" => $detail["initial_weight_price"],
  366. ]);
  367. $detail->load("province");
  368. }
  369. return ["success"=>true,"data"=>$detail];
  370. }
  371. public function expressDestroyDetail(Request $request)
  372. {
  373. if(!Gate::allows('计费模型-快递-删除')){ return ["success"=>false,"data"=>"无权操作"]; }
  374. $id = $request->input("id");
  375. if (!$id)return ["success"=>false,"data"=>"非法参数"];
  376. app("OwnerPriceExpressService")->destroyDetail($id);
  377. return ["success"=>true];
  378. }
  379. public function expressImport(Request $request){
  380. if(!Gate::allows('计费模型-快递-录入')){ return ["success"=>false,"data"=>"无权操作"]; }
  381. $fileSuffix=$request->file('file')->getClientOriginalExtension();
  382. if ($fileSuffix != 'xlsx' && $fileSuffix != 'xls' && $fileSuffix != 'csv')
  383. return ['success'=>false,'data'=>'不支持该文件类型'];
  384. ini_set('max_execution_time',2500);
  385. ini_set('memory_limit','1526M');
  386. $fileSuffix = ucwords($fileSuffix);
  387. /** @var OwnerPriceExpress $model */
  388. $model = app('OwnerPriceExpressService')->find($request->input("id"),["details"]);
  389. Excel::import(new ExpressImport($model),$request->file('file')->path(),null,$fileSuffix);
  390. if (Cache::has('express'))return Cache::pull('express');
  391. return ["success"=>false,"data"=>"导入发生错误,数据无响应"];
  392. }
  393. public function expressCreate(){
  394. if(!Gate::allows('计费模型-快递-录入')){ return redirect('denied'); }
  395. $logistics = app("LogisticService")->getSelection();
  396. $owners = app("OwnerService")->getSelection();
  397. $units = app("UnitService")->getSelection();
  398. return response()->view('maintenance.priceModel.express.create',compact("logistics","owners","units"));
  399. }
  400. public function expressStore(Request $request)
  401. {
  402. if(!Gate::allows('计费模型-快递-录入')){ return redirect('denied'); }
  403. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  404. $request->offsetSet("logistic_id",explode(",",$request->input("logistic_id")));
  405. $this->expressValidator($request->input())->validate();
  406. /** @var OwnerPriceExpress $model */
  407. $model = app("OwnerPriceExpressService")->create([
  408. "name" => $request->input("name"),
  409. "initial_weight_unit_id" => $request->input("initial_weight_unit_id"),
  410. "additional_weight_unit_id" => $request->input("additional_weight_unit_id"),
  411. ]);
  412. $model->owners()->sync($request->input("owner_id"));
  413. $model->logistics()->sync($request->input("logistic_id"));
  414. return response()->redirectTo("maintenance/priceModel/express")->with("successTip","录入“".$request->input("name")."”成功");
  415. }
  416. public function expressEdit($id)
  417. {
  418. if(!Gate::allows('计费模型-快递-编辑')){ return redirect('denied'); }
  419. /** @var OwnerPriceExpress $model */
  420. $model = app('OwnerPriceExpressService')->find($id)->append(["owner_id","logistic_id"]);
  421. $owners = app("OwnerService")->getSelection();
  422. $logistics = app('LogisticService')->getSelection();
  423. $units = app('UnitService')->getSelection();
  424. return response()->view('maintenance.priceModel.express.create',compact("owners","logistics","model","units"));
  425. }
  426. public function expressUpdate($id,Request $request)
  427. {
  428. if(!Gate::allows('计费模型-快递-编辑')){ return redirect('denied'); }
  429. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  430. $request->offsetSet("logistic_id",explode(",",$request->input("logistic_id")));
  431. $this->expressValidator($request->input(),$id)->validate();
  432. /** @var OwnerPriceExpress $model */
  433. app("OwnerPriceExpressService")->update(["id"=>$id],[
  434. "name" => $request->input("name"),
  435. "initial_weight_unit_id" => $request->input("initial_weight_unit_id"),
  436. "additional_weight_unit_id" => $request->input("additional_weight_unit_id"),
  437. ]);
  438. $model = new OwnerPriceExpress();
  439. $model->id = $id;
  440. $model->owners()->sync($request->input("owner_id"));
  441. $model->logistics()->sync($request->input("logistic_id"));
  442. return response()->redirectTo("maintenance/priceModel/express")->with("successTip","修改“".$request->input("name")."”成功");
  443. }
  444. private function expressValidator(array $params, $id=null)
  445. {
  446. return Validator::make($params,[
  447. 'name'=>['required',$id?"unique:owner_price_expresses,name,$id":'unique:owner_price_expresses,name'],
  448. 'initial_weight_unit_id'=>['required'],
  449. 'additional_weight_unit_id'=>['required'],
  450. 'owner_id'=>[function ($attribute, $value, $fail)use($id) {
  451. $owners = app("OwnerPriceExpressService")->getExistOwnerName($value,$id);
  452. if ($owners)$fail("(".implode(',',$owners).') 已经绑定计费模型');
  453. }],
  454. 'logistic_id'=>[function ($attribute, $value, $fail)use($id) {
  455. $logistics = app("OwnerPriceExpressService")->getExistLogisticName($value,$id);
  456. if ($logistics)$fail("(".implode(',',$logistics).') 已经绑定计费模型');
  457. }],
  458. ],[
  459. 'required'=>':attribute 为必填项',
  460. 'unique' => ':attribute 已存在',
  461. ],[
  462. 'name' =>"名称",
  463. 'initial_weight_unit_id' =>"首重单位",
  464. 'additional_weight_unit_id' =>"续重单位",
  465. ]);
  466. }
  467. public function expressDestroy($id)
  468. {
  469. if(!Gate::allows('计费模型-快递-删除')){ return ["success"=>false,"data"=>"无权操作"]; };
  470. app("OwnerPriceExpressService")->destroy($id);
  471. return ["success"=>true];
  472. }
  473. public function logisticIndex()
  474. {
  475. if(!Gate::allows('计费模型-物流-查询')){ return redirect('denied'); }
  476. $models = app("OwnerPriceLogisticService")->paginate()->append(["unit_range_json","other_unit_range_json"]);
  477. return response()->view('maintenance.priceModel.logistic.index',compact("models"));
  478. }
  479. public function logisticCreate()
  480. {
  481. if(!Gate::allows('计费模型-物流-录入')){ return redirect('denied'); }
  482. $owners = app("OwnerService")->getSelection();
  483. $logistics = app('LogisticService')->getSelection();
  484. $units = app('UnitService')->getSelection();
  485. return response()->view('maintenance.priceModel.logistic.create',compact("owners","logistics","units"));
  486. }
  487. public function logisticStore(Request $request)
  488. {
  489. if(!Gate::allows('计费模型-物流-录入')){ return redirect('denied'); }
  490. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  491. $request->offsetSet("logistic_id",explode(",",$request->input("logistic_id")));
  492. $this->logisticValidator($request->input())->validate();
  493. /** @var OwnerPriceLogistic $model */
  494. $model = app("OwnerPriceLogisticService")->create([
  495. "name" => $request->input("name"),
  496. "pick_up_price" => $request->input("pick_up_price"),
  497. "fuel_price" => $request->input("fuel_price"),
  498. "service_price" => $request->input("service_price"),
  499. "unit_id" => $request->input("unit_id"),
  500. "unit_range" => $request->input("unit_range"),
  501. "other_unit_id" => $request->input("other_unit_id"),
  502. "other_unit_range" => $request->input("other_unit_range"),
  503. ]);
  504. $model->owners()->sync($request->input("owner_id"));
  505. $model->logistics()->sync($request->input("logistic_id"));
  506. return response()->redirectTo("maintenance/priceModel/logistic")->with("successTip","创建“".$request->input("name")."”成功");
  507. }
  508. public function logisticEdit($id)
  509. {
  510. if(!Gate::allows('计费模型-物流-编辑')){ return redirect('denied'); }
  511. $owners = app("OwnerService")->getSelection();
  512. $logistics = app('LogisticService')->getSelection();
  513. $units = app('UnitService')->getSelection();
  514. $model = app("OwnerPriceLogisticService")->find($id)->append(["owner_id","logistic_id"]);
  515. return response()->view('maintenance.priceModel.logistic.create',compact("owners","logistics","units","model"));
  516. }
  517. public function logisticUpdate($id, Request $request)
  518. {
  519. if(!Gate::allows('计费模型-物流-编辑')){ return redirect('denied'); }
  520. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  521. $request->offsetSet("logistic_id",explode(",",$request->input("logistic_id")));
  522. $this->logisticValidator($request->input(),$id)->validate();
  523. app("OwnerPriceLogisticService")->update(["id"=>$id],[
  524. "name" => $request->input("name"),
  525. "pick_up_price" => $request->input("pick_up_price"),
  526. "fuel_price" => $request->input("fuel_price"),
  527. "service_price" => $request->input("service_price"),
  528. "unit_id" => $request->input("unit_id"),
  529. "unit_range" => $request->input("unit_range"),
  530. "other_unit_id" => $request->input("other_unit_id"),
  531. "other_unit_range" => $request->input("other_unit_range"),
  532. ]);
  533. $model = new OwnerPriceLogistic();
  534. $model->id = $id;
  535. $model->owners()->sync($request->input("owner_id"));
  536. $model->logistics()->sync($request->input("logistic_id"));
  537. return response()->redirectTo("maintenance/priceModel/logistic")->with("successTip","修改“".$request->input("name")."”成功");
  538. }
  539. public function logisticGetDetail(Request $request)
  540. {
  541. if(!Gate::allows('计费模型-物流-查询')){ return ["success"=>false,"data"=>"无权操作"]; }
  542. $model = new OwnerPriceLogistic();
  543. $model->id = $request->input("id");
  544. $model->load(["details"=>function($query){
  545. /** @var Builder $query */
  546. $query->with(["unit","province","city"]);
  547. }]);
  548. return ["success"=>true,"data"=>$model->details];
  549. }
  550. public function expressExport($id)
  551. {
  552. if(!Gate::allows('计费模型-物流-查询')){ return ["success"=>false,"data"=>"无权操作"]; }
  553. $model = app("OwnerPriceExpressService")->find($id,[
  554. "owners","logistics","initialWeightUnit","additionalWeightUnit","details"=>function($query){
  555. /** @var Builder $query */
  556. $query->with("province");
  557. }]);
  558. $row = ["客户","首重单位","续重单位","承运商"];
  559. $list = [[
  560. implode(",",array_column($model->owners->toArray(),"name")),
  561. $model->initialWeightUnit ? $model->initialWeightUnit->name : '',
  562. $model->additionalWeightUnit ? $model->additionalWeightUnit->name : '',
  563. implode(",",array_column($model->logistics->toArray(),"name")),
  564. ],[
  565. "价格名称","省","首重价格","续重价格",
  566. ]];
  567. foreach ($model->details as $detail){
  568. $list[] = [
  569. $model->name,
  570. $detail->province ? $detail->province->name : '',
  571. $detail->initial_weight_price,
  572. $detail->additional_weight_price,
  573. ];
  574. }
  575. return app(ExportService::class)->json($row,$list,"快递计费模型");
  576. }
  577. public function logisticImport(Request $request)
  578. {
  579. if(!Gate::allows('计费模型-物流-录入')){ return ["success"=>false,"data"=>"无权操作"]; }
  580. $fileSuffix=$request->file('file')->getClientOriginalExtension();
  581. if ($fileSuffix != 'xlsx' && $fileSuffix != 'xls' && $fileSuffix != 'csv')
  582. return ['success'=>false,'data'=>'不支持该文件类型'];
  583. ini_set('max_execution_time',2500);
  584. ini_set('memory_limit','1526M');
  585. $fileSuffix = ucwords($fileSuffix);
  586. /** @var OwnerPriceLogistic $model */
  587. $model = app('OwnerPriceLogisticService')->find($request->input("id"),["unit","otherUnit","details"]);
  588. Excel::import(new OwnerPriceLogisticDetailImport($model),$request->file('file')->path(),null,$fileSuffix);
  589. if (Cache::has('logistic'))return Cache::pull('logistic');
  590. return ["success"=>false,"data"=>"导入发生错误,数据无响应"];
  591. }
  592. public function logisticUpdateDetail(Request $request)
  593. {
  594. if(!Gate::allows('计费模型-物流-编辑')){ return ["success"=>false,"data"=>"无权操作"]; }
  595. $detail = $request->input("detail");
  596. if ($detail["id"]){
  597. app('OwnerPriceLogisticService')->updateDetail(["id"=>$detail["id"]],[
  598. "unit_price" => $detail["unit_price"],
  599. "delivery_fee" => $detail["delivery_fee"],
  600. "initial_fee" => $detail["initial_fee"],
  601. "initial_amount" => $detail["initial_amount"],
  602. "rate" => $detail["rate"],
  603. ]);
  604. }else{
  605. $row = app('OwnerPriceLogisticService')->isExistDetail([
  606. "owner_price_logistic_id"=>$request->input("id"),
  607. "unit_id"=>$detail["unit_id"],
  608. "range"=>$detail["range"],
  609. "province_id"=>$detail["province_id"],
  610. "city_id"=>$detail["city_id"],
  611. ]);
  612. if ($row>0)return ["success"=>false,"data"=>"已存在该计费模型"];
  613. /** @var OwnerPriceExpressProvince $detail */
  614. $detail = app('OwnerPriceLogisticService')->createDetail([
  615. "owner_price_logistic_id"=>$request->input("id"),
  616. "unit_id"=>$detail["unit_id"],
  617. "range"=>$detail["range"],
  618. "province_id"=>$detail["province_id"],
  619. "city_id"=>$detail["city_id"],
  620. "unit_price" => $detail["unit_price"],
  621. "delivery_fee" => $detail["delivery_fee"],
  622. "initial_fee" => $detail["initial_fee"],
  623. "initial_amount" => $detail["initial_amount"],
  624. "rate" => $detail["rate"],
  625. ]);
  626. $detail->load("province","unit","city");
  627. }
  628. return ["success"=>true,"data"=>$detail];
  629. }
  630. private function logisticValidator(array $params,$id = null)
  631. {
  632. return Validator::make($params,[
  633. 'name'=>['required',$id?"unique:owner_price_logistics,name,$id":'unique:owner_price_logistics,name'],
  634. 'pick_up_price'=>['nullable','numeric','min:0'],
  635. 'fuel_price'=>['nullable','numeric','min:0'],
  636. 'service_price'=>['nullable','numeric','min:0'],
  637. 'unit_id'=>['required'],
  638. 'unit_range'=>['required',function ($attribute, $value, $fail) {
  639. $bool = app("OwnerPriceLogisticService")->checkRange($value);
  640. if (!$bool)$fail("格式错误,值必须为连续的且最后一个值不允许封闭");
  641. }],
  642. 'other_unit_id'=>['required'],
  643. 'other_unit_range'=>['required',function ($attribute, $value, $fail) {
  644. $bool = app("OwnerPriceLogisticService")->checkRange($value);
  645. if (!$bool)$fail("格式错误,值必须为连续的且最后一个值不允许封闭");
  646. }],
  647. ],[
  648. 'required'=>':attribute 为必填项',
  649. 'unique' => ':attribute 已存在',
  650. 'numeric' => ':attribute 必须为数字',
  651. 'min' => ':attribute 不得为负',
  652. ],[
  653. 'name' =>"名称",
  654. 'pick_up_price' =>"提货费",
  655. 'fuel_price' =>"燃油附加费",
  656. 'service_price' =>"信息服务费",
  657. 'unit_id' =>"单位一",
  658. 'unit_range' =>"区间值",
  659. 'other_unit_id' =>"单位二",
  660. 'other_unit_range' =>"区间值",
  661. ]);
  662. }
  663. public function directLogisticIndex(){
  664. if(!Gate::allows('计费模型-直发-查询')){ return redirect('denied'); }
  665. return response()->view('maintenance.priceModel.directLogistic.index');
  666. }
  667. public function directLogisticCreate(){
  668. if(!Gate::allows('计费模型-直发-录入')){ return redirect('denied'); }
  669. return response()->view('maintenance.priceModel.directLogistic.create');
  670. }
  671. }