PriceModelController.php 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\AsyncResponse;
  4. use App\Imports\ExpressImport;
  5. use App\Imports\OwnerPriceDirectLogisticDetailImport;
  6. use App\Imports\OwnerPriceLogisticDetailImport;
  7. use App\Owner;
  8. use App\OwnerPriceDirectLogistic;
  9. use App\OwnerPriceDirectLogisticCar;
  10. use App\OwnerPriceExpress;
  11. use App\OwnerPriceExpressProvince;
  12. use App\OwnerPriceLogistic;
  13. use App\OwnerPriceLogisticDetail;
  14. use App\OwnerPriceOperation;
  15. use App\OwnerPriceOperationItem;
  16. use App\OwnerPriceSystem;
  17. use App\OwnerStoragePriceModel;
  18. use App\Services\common\BatchUpdateService;
  19. use App\Services\common\ExportService;
  20. use App\Services\common\QueryService;
  21. use App\Services\LogService;
  22. use App\Services\OwnerPriceOperationItemService;
  23. use App\Services\OwnerPriceOperationService;
  24. use Illuminate\Database\Eloquent\Builder;
  25. use Illuminate\Http\Request;
  26. use Illuminate\Support\Facades\Cache;
  27. use Illuminate\Support\Facades\DB;
  28. use Illuminate\Support\Facades\Gate;
  29. use Illuminate\Support\Facades\Validator;
  30. use Maatwebsite\Excel\Facades\Excel;
  31. use Oursdreams\Export\Export;
  32. class PriceModelController extends Controller
  33. {
  34. use AsyncResponse;
  35. public function storageIndex(Request $request)
  36. {
  37. if(!Gate::allows('计费模型-仓储')){ return redirect('denied'); }
  38. $models = app('OwnerStoragePriceModelService')->paginate($request->input("id"),["unit","owners","timeUnit"]);
  39. return response()->view('maintenance.priceModel.storage.index',compact("models"));
  40. }
  41. public function storageCreate()
  42. {
  43. if(!Gate::allows('计费模型-仓储-录入')){ return redirect('denied'); }
  44. $units = app('UnitService')->getSelection();
  45. $owners = app("OwnerService")->getIntersectPermitting();
  46. return response()->view('maintenance.priceModel.storage.create',compact("units","owners"));
  47. }
  48. public function storageStore(Request $request)
  49. {
  50. if(!Gate::allows('计费模型-仓储-录入')){ return redirect('denied'); }
  51. $this->storageValidator($request->input())->validate();
  52. /** @var OwnerStoragePriceModel $model */
  53. $model = app('OwnerStoragePriceModelService')->create($request->input());
  54. $result = $model->owners()->sync(explode(",",$request->input("owner_id")));
  55. app("OwnerService")->refreshRelevance($result["attached"],0);
  56. LogService::log(__METHOD__,"计费模型-创建仓储计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  57. return response()->redirectTo('maintenance/priceModel/storage')->with('successTip',"创建成功!");
  58. }
  59. public function storageEdit($id)
  60. {
  61. if(!Gate::allows('计费模型-仓储-编辑')){ return redirect('denied'); }
  62. $model = app('OwnerStoragePriceModelService')->find($id,["owners"]);
  63. $units = app('UnitService')->getSelection();
  64. $owners = app("OwnerService")->getIntersectPermitting();
  65. return response()->view('maintenance.priceModel.storage.create',compact("units","model","owners"));
  66. }
  67. public function storageUpdate(Request $request)
  68. {
  69. if(!Gate::allows('计费模型-仓储-编辑')){ return redirect('denied'); }
  70. $service = app('OwnerStoragePriceModelService');
  71. $id = request("id");
  72. $model = $service->find($id);
  73. $values = [
  74. "counting_type" => $request->input("counting_type"),
  75. "name" => $request->input("name"),
  76. "using_type" => $request->input("using_type"),
  77. "minimum_area" => $request->input("minimum_area"),
  78. "price" => $request->input("price"),
  79. "discount_type" => $request->input("discount_type"),
  80. "discount_value"=> $request->input("discount_value"),
  81. "unit_id" => $request->input("unit_id"),
  82. "time_unit_id" => $request->input("time_unit_id"),
  83. ];
  84. if ($model->operation){
  85. if ($model->operation=='C')$values["operation"]="U";
  86. $service->update(["id"=>$id],$values);
  87. $model = new OwnerStoragePriceModel();
  88. $model->id = $id;
  89. $result = $model->owners()->sync(explode(",",request("owner_id")));
  90. app("OwnerService")->refreshRelevance($result["attached"],0);
  91. app("OwnerService")->refreshRelevance($result["detached"],0,true);
  92. }else $service->copy($model,$values,explode(",",request("owner_id")));
  93. return response()->redirectTo('maintenance/priceModel/storage')->with('successTip',"更新成功!");
  94. }
  95. public function storageDestroy($id)
  96. {
  97. $model = app("OwnerStoragePriceModelService")->find($id);
  98. if ($model->operation && $model->target_id){
  99. app("OwnerStoragePriceModelService")->destroy($id);
  100. $id = $model->target_id;
  101. }
  102. app('OwnerStoragePriceModelService')->update(["id"=>$id],["operation"=>"D"]);
  103. $this->success();
  104. }
  105. private function storageValidator(array $params)
  106. {
  107. return Validator::make($params,[
  108. 'name'=>['required'],
  109. 'counting_type'=>['required'],
  110. 'using_type'=>['required'],
  111. 'minimum_area'=>['nullable','numeric','min:0'],
  112. 'discount_value'=>['nullable','numeric','min:0'],
  113. 'price.*'=>['required','numeric','min:0'],
  114. 'amount_interval.*'=>['required','integer','min:0'],
  115. 'discount_type'=>['required'],
  116. 'unit_id'=>['required','integer'],
  117. 'time_unit_id'=>['required','integer'],
  118. ],[
  119. 'required'=>':attribute 为必填项',
  120. 'min'=>':attribute 不得小于0',
  121. 'integer'=>':attribute 未选择',
  122. ],[
  123. 'name' =>"名称",
  124. 'counting_type' =>"计费类型",
  125. 'using_type' =>"用仓类型",
  126. 'minimum_area' =>"最低起租面积",
  127. 'price.*' =>"单价",
  128. 'amount_interval.*'=>"数量区间",
  129. 'discount_type' =>"减免类型",
  130. 'discount_value'=>"减免值",
  131. 'unit_id' =>"单位",
  132. 'time_unit_id' =>"计时单位",
  133. ]);
  134. }
  135. public function operationIndex(Request $request){
  136. if(!Gate::allows('计费模型-作业-查询')){ return redirect('denied'); }
  137. $features = app("FeatureService")->getMapArray();
  138. OwnerPriceOperation::$features = $features;
  139. $models = app('OwnerPriceOperationService')->paginate($request->input(),["owners","items.unit"])->append("featureFormat");
  140. $owners = app("OwnerService")->getIntersectPermitting();
  141. return response()->view('maintenance.priceModel.operation.index',compact("models","owners"));
  142. }
  143. /* 获取出库模型规则 */
  144. public function getItems(Request $request)
  145. {
  146. $this->gate("计费模型-作业-查询");
  147. /** @var OwnerPriceOperationItemService $service */
  148. $service = app('OwnerPriceOperationItemService');
  149. $items = $service->get(["owner_price_operation_id"=>$request->input("id")],["unit"],true)->append("featureFormat");
  150. $this->success($items);
  151. }
  152. /* 修改子规则 */
  153. public function updateItem(Request $request)
  154. {
  155. $this->gate("计费模型-作业-编辑");
  156. /** @var OwnerPriceOperationItemService $service */
  157. $service = app('OwnerPriceOperationItemService');
  158. /** @var \stdClass $item */
  159. $item = $service->find(request("id"));
  160. $model = app('OwnerPriceOperationService')->find($item->owner_price_operation_id);
  161. $obj = ["amount"=>$request->input("amount"),
  162. "unit_id"=>$request->input("unit_id"),
  163. "priority"=>$request->input("priority"),
  164. "unit_price"=>$request->input("unit_price")];
  165. if ($model->operation){
  166. if ($model->operation == 'C')$obj["operation"] = "U";
  167. $service->update(["id"=>request("id")],$obj);
  168. }else app('OwnerPriceOperationService')->copy($model,[],null,[request("id")=>$obj]);
  169. $this->success();
  170. }
  171. public function createItem(Request $request)
  172. {
  173. $this->gate("计费模型-作业-编辑");
  174. /** @var OwnerPriceOperationItemService $service */
  175. $service = app('OwnerPriceOperationItemService');
  176. $id = request("owner_price_operation_id");
  177. switch ($request->input("strategy")){
  178. case "起步":
  179. $c = $service->isExist(["owner_price_operation_id"=>$id,"strategy"=>"起步"]);
  180. if ($c > 0) $this->error("已存在起步策略");
  181. break;
  182. case "默认":
  183. $c = $service->isExist(["owner_price_operation_id"=>$id,"strategy"=>"默认"]);
  184. if ($c > 0)$this->error("已存在默认策略");
  185. break;
  186. }
  187. $model = app('OwnerPriceOperationService')->find($id);
  188. if ($model->operation){
  189. if ($model->operation == 'C')app('OwnerPriceOperationService')->update(["id"=>$id],["operation"=>"U"]);
  190. }else {
  191. $copy = app('OwnerPriceOperationService')->copy($model);
  192. $request->offsetSet("owner_price_operation_id",$copy->id);
  193. }
  194. $data = $service->create($request->input());
  195. $data->load("unit");
  196. $this->success($data);
  197. }
  198. public function getFeatures()
  199. {
  200. $this->success(app("FeatureService")->translationFeature(request("feature")));
  201. }
  202. public function addFeature(Request $request)
  203. {
  204. if(!Gate::allows('计费模型-作业-编辑')){ return ["success"=>false,"data"=>"无权操作"]; }
  205. $id = $request->input("id");
  206. $features = $request->input("features");
  207. if (!$id || !$features)return ["success"=>false,"data"=>"非法参数"];
  208. $result = app("FeatureService")->analysisFeature($features);
  209. $feature = $result["feature"];
  210. $stack = [];
  211. if ($feature && ($feature[0]=='|' || $feature[0]=='&'))$feature=substr($feature,1);
  212. for ($i=0;$i<strlen($feature);$i++){
  213. if ($feature[$i] == '(')array_unshift($stack,'(');
  214. if ($feature[$i] == ')'){
  215. if (count($stack) == 0)return ["success"=>false,"data"=>"组标记错误,起始与结束标记必须对应"];
  216. array_shift($stack);
  217. }
  218. }
  219. if (count($stack) > 0)return ["success"=>false,"data"=>"组标记错误,起始与结束标记必须对应"];
  220. $row = app('OwnerPriceOperationItemService')->update(["id"=>$id],["feature"=>$feature]);
  221. if ($row != 1)return ["success"=>false,"data"=>"影响了“".$row."”行"];
  222. LogService::log(__METHOD__,"计费模型-修改出库特征",json_encode($request->input()));
  223. OwnerPriceOperationItem::$features = $result["map"];
  224. $rule = app('OwnerPriceOperationItemService')->find($id)->append("featureFormat");
  225. return ["success"=>true,"data"=>["featureFormat"=>$rule->featureFormat,"feature"=>$feature]];
  226. }
  227. public function getFeature(Request $request)
  228. {
  229. $features = $request->input("features");
  230. if (!$features)return ["success"=>false,"data"=>"非法参数"];
  231. $result = app("FeatureService")->analysisFeature($features);
  232. $feature = $result["feature"];
  233. $stack = [];
  234. if ($feature && ($feature[0]=='|' || $feature[0]=='&'))$feature=substr($feature,1);
  235. for ($i=0;$i<strlen($feature);$i++){
  236. if ($feature[$i] == '(')array_unshift($stack,'(');
  237. if ($feature[$i] == ')'){
  238. if (count($stack) == 0)return ["success"=>false,"data"=>"组标记错误,起始与结束标记必须对应"];
  239. array_shift($stack);
  240. }
  241. }
  242. if (count($stack) > 0)return ["success"=>false,"data"=>"组标记错误,起始与结束标记必须对应"];
  243. if ($request->has("isFormat"))$this->success(["feature"=>$feature,"featureFormat"=>app("FeatureService")->formatFeature($result["map"], $feature)]);
  244. $this->success($feature);
  245. }
  246. public function operationDestroy($id)
  247. {
  248. $this->gate("计费模型-作业-删除");
  249. $model = app('OwnerPriceOperationService')->find($id);
  250. if ($model->operation && $model->target_id){
  251. app("OwnerPriceOperationService")->destroy($id);
  252. $id = $model->target_id;
  253. }
  254. app('OwnerPriceOperationService')->update(["id"=>$id],["operation"=>"D"]);
  255. $this->success();
  256. }
  257. public function operationCreate(){
  258. if(!Gate::allows('计费模型-作业-录入')){ return redirect('denied'); }
  259. $owners = app("OwnerService")->getIntersectPermitting();
  260. $units = app('UnitService')->getSelection();
  261. return response()->view('maintenance.priceModel.operation.create',compact("owners","units"));
  262. }
  263. public function operationStore(Request $request)
  264. {
  265. if(!Gate::allows('计费模型-作业-录入')){ return redirect('denied'); }
  266. $request->offsetSet("items",json_decode($request->input("items"),true));
  267. $request->offsetSet("owner_id",explode(',',$request->input("owner_id")));
  268. $this->operationValidator($request->input())->validate();
  269. //录入主表
  270. /** @var OwnerPriceOperationService $service */
  271. $service = app("OwnerPriceOperationService");
  272. $ownerPriceOperation = $service->create([
  273. "operation_type" => $request->input("operation_type"),
  274. "strategy" => $request->input("strategy"),
  275. "name" => $request->input("name"),
  276. "priority" => $request->input("priority"),
  277. "remark" => $request->input("remark"),
  278. "feature" => $request->input("feature"),
  279. "discount_count" => request("discount_count") ? implode(",",request("discount_count")) : null,
  280. "total_price" => request("total_price"),
  281. "total_discount_price"=> request("total_discount_price") ? implode(",",request("total_discount_price")) : null,
  282. ]);
  283. if ($request->input("items")){
  284. //录入子表
  285. $insert = [];
  286. foreach ($request->input("items") as $rule){
  287. if ($rule["discount_price"] ?? false){
  288. foreach ($rule["discount_price"] as $index=>$item){
  289. if ($index!=0 && !$item)$rule["discount_price"][$index] = $rule["discount_price"][$index-1];
  290. }
  291. }
  292. $insert[] = [
  293. "owner_price_operation_id" => $ownerPriceOperation->id,
  294. "amount" => $rule["amount"],
  295. "unit_id" => $rule["unit_id"],
  296. "unit_price" => $rule["unit_price"],
  297. "strategy" => $rule["strategy"],
  298. "feature" => $rule["feature"],
  299. "priority" => $rule["priority"],
  300. "discount_price" => implode(",",$rule["discount_price"]),
  301. ];
  302. }
  303. $service->insertItem($insert);
  304. }
  305. //录入中间表
  306. /** @var OwnerPriceOperation $ownerPriceOperation */
  307. if ($request->input("owner_id")){
  308. $result = $ownerPriceOperation->owners()->sync($request->input("owner_id"));
  309. app("OwnerService")->refreshRelevance($result["attached"],1);
  310. app("OwnerService")->refreshRelevance($result["detached"],1,true);
  311. }
  312. LogService::log(__METHOD__,"计费模型-录入作业计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  313. return response()->redirectTo("maintenance/priceModel/operation")->with("successTip","创建“".$request->input("name")."”成功");
  314. }
  315. public function operationEdit($id)
  316. {
  317. if(!Gate::allows('计费模型-作业-编辑')){ return redirect('denied'); }
  318. $model = app('OwnerPriceOperationService')->find($id,["items"]);
  319. $owners = app("OwnerService")->getIntersectPermitting();
  320. $units = app('UnitService')->getSelection();
  321. return response()->view('maintenance.priceModel.operation.create',compact("owners","units","model"));
  322. }
  323. public function operationUpdate($id,Request $request)
  324. {
  325. if(!Gate::allows('计费模型-作业-编辑')){ return redirect('denied'); }
  326. $request->offsetSet("items",json_decode($request->input("items"),true));
  327. $request->offsetSet("owner_id",explode(',',$request->input("owner_id")));
  328. $this->operationValidator($request->input(),$id)->validate();
  329. /** @var OwnerPriceOperationService $service */
  330. $service = app("OwnerPriceOperationService");
  331. /** @var \stdClass $model */
  332. $model = $service->find($id);
  333. $obj = ["name" => $request->input("name"),
  334. "priority" => $request->input("priority"),
  335. "remark" => $request->input("remark"),
  336. "feature" => $request->input("feature"),
  337. "discount_count" => implode(",",request("discount_count")),
  338. "total_price" => request("total_price"),
  339. "total_discount_price"=> implode(",",request("total_discount_price")) ?? null,
  340. ];
  341. if ($model->operation){
  342. $service->findUpdate($model,$obj);
  343. $service->destroyItem($id);
  344. if ($request->input("items")){
  345. //录入子表
  346. $insert = [];
  347. foreach ($request->input("items") as $rule){
  348. $insert[] = [
  349. "owner_price_operation_id" => $model->id,
  350. "amount" => $rule["amount"],
  351. "unit_id" => $rule["unit_id"],
  352. "unit_price" => $rule["unit_price"],
  353. "strategy" => $rule["strategy"],
  354. "feature" => $rule["feature"],
  355. "priority" => $rule["priority"],
  356. "discount_price" => implode(",",$rule["discount_price"]) ?? null,
  357. ];
  358. }
  359. $service->insertItem($insert);
  360. }
  361. //录入中间表
  362. /** @var OwnerPriceOperation $model */
  363. if ($request->input("owner_id")){
  364. $result = $model->owners()->sync(request("owner_id"));
  365. app("OwnerService")->refreshRelevance($result["attached"],1);
  366. app("OwnerService")->refreshRelevance($result["detached"],1,true);
  367. }
  368. }else $service->copy($model,$obj,request("owner_id"),request("items"),false);
  369. LogService::log(__METHOD__,"计费模型-修改作业计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  370. return response()->redirectTo("maintenance/priceModel/operation")->with("successTip","修改“".$request->input("name")."”成功");
  371. }
  372. private function operationValidator(array $params, $id= null)
  373. {
  374. return Validator::make($params,[
  375. 'operation_type'=>['required'],
  376. 'owner_id'=>[function ($attribute, $value, $fail)use($params,$id) {
  377. if ($params["strategy"] == '默认'){
  378. $owners = Owner::query()->whereIn("id",$value)->withCount(["ownerPriceOperations"=>function($query)use($params,$id){
  379. if ($id)$query->where('id',"!=",$id);
  380. $query->where("strategy","默认")->where("operation_type",$params["operation_type"]);
  381. }])->get();
  382. $err = [];
  383. foreach ($owners as $owner){
  384. if ($owner->owner_price_operations_count > 0)$err[] = $owner->name;
  385. }
  386. if (count($err)>0)$fail("(".implode(',',$err).') 已经绑定'.$params["operation_type"].'的默认策略');
  387. }
  388. }],
  389. 'strategy'=>['required'],
  390. 'name'=>['required'],
  391. 'priority'=>['sometimes','required','integer','min:0','max:100'],
  392. 'items.*.strategy'=>['required'],
  393. 'items.*.amount'=>["required","integer"],
  394. 'items.*.unit_id'=>['required','integer'],
  395. 'items.*.unit_price'=>['required','numeric',"min:0"],
  396. ],[
  397. 'required'=>':attribute 为必填项',
  398. 'min'=>':attribute 不得小于0',
  399. 'integer'=>':attribute 必须为整数',
  400. 'numeric'=>':attribute 必须为数字',
  401. 'max'=>':attribute 超出最大值',
  402. 'required_if'=>':attribute 操作类型为出库时不得为空',
  403. ],[
  404. 'operation_type' =>"操作类型",
  405. 'strategy' =>"计费策略",
  406. 'name' =>"名称",
  407. ]);
  408. }
  409. public function expressIndex(Request $request){
  410. if(!Gate::allows('计费模型-快递-查询')){ return redirect('denied'); }
  411. $models = app('OwnerPriceExpressService')->paginate($request->input("id"));
  412. return response()->view('maintenance.priceModel.express.index',compact("models"));
  413. }
  414. public function expressGetDetail(Request $request)
  415. {
  416. $this->gate("计费模型-快递-查询");
  417. $model = new OwnerPriceExpress();
  418. $model->id = $request->input("id");
  419. $model->load("details.province");
  420. $this->success($model->details);
  421. }
  422. public function expressUpdateDetail(Request $request)
  423. {
  424. $this->gate("计费模型-快递-编辑");
  425. $detail = $request->input("detail");
  426. $values = ["additional_weight_price" => $detail["additional_weight_price"],
  427. "initial_weight_price" => $detail["initial_weight_price"],
  428. ];
  429. if ($detail["id"]){
  430. /** @var \stdClass $item */
  431. $item = OwnerPriceExpressProvince::query()->with("ownerPriceExpress")->find($detail["id"]);
  432. if ($item->ownerPriceExpress->operation){
  433. app('OwnerPriceExpressService')->updateDetail(["id"=>$detail["id"]],$values);
  434. }else app("OwnerPriceExpressService")->copy($item->ownerPriceExpress,[],null,null,[$detail["id"]=>$values]);
  435. }else{
  436. $row = app('OwnerPriceExpressService')->isExistDetail(["owner_price_express_id"=>request("id"),"province_id"=>$detail["province_id"]]);
  437. if ($row>0)$this->error("已存在该省份计费模型");
  438. $values["province_id"]=$detail["province_id"];
  439. $model = app('OwnerPriceExpressService')->find(request("id"));
  440. if ($model->operation){
  441. $values["owner_price_express_id"]=request("id");
  442. }else{
  443. $model = app("OwnerPriceExpressService")->copy($model);
  444. $values["owner_price_express_id"]=$model->id;
  445. }
  446. /** @var OwnerPriceExpressProvince $detail */
  447. $detail = app('OwnerPriceExpressService')->createDetail($values);
  448. $detail->load("province");
  449. }
  450. LogService::log(__METHOD__,"计费模型-修改快递计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  451. $this->success($detail);
  452. }
  453. public function expressDestroyDetail()
  454. {
  455. $this->gate("计费模型-快递-删除");
  456. if (!request("id"))$this->error("非法参数");
  457. /** @var \stdClass $item */
  458. $item = OwnerPriceExpressProvince::query()->with("ownerPriceExpress")->find(request("id"));
  459. if ($item->ownerPriceExpress->operation){
  460. app("OwnerPriceExpressService")->destroyDetail(request("id"));
  461. }else{
  462. $model = app("OwnerPriceExpressService")->copy($item->ownerPriceExpress);
  463. OwnerPriceExpressProvince::query()->where("owner_price_express_id",$model->id)
  464. ->where("province_id",$item->province_id)->delete();
  465. }
  466. $this->success();
  467. }
  468. public function expressImport(){
  469. $this->gate("计费模型-快递-录入");
  470. $this->importExcel(new ExpressImport(request("id") ? app('OwnerPriceExpressService')->find(request("id"),["details"]) : null));
  471. }
  472. public function expressCreate(){
  473. if(!Gate::allows('计费模型-快递-录入')){ return redirect('denied'); }
  474. $logistics = app("LogisticService")->getSelection();
  475. $owners = app("OwnerService")->getIntersectPermitting();
  476. return response()->view('maintenance.priceModel.express.create',compact("logistics","owners"));
  477. }
  478. public function expressStore(Request $request)
  479. {
  480. if(!Gate::allows('计费模型-快递-录入')){ return redirect('denied'); }
  481. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  482. $request->offsetSet("logistic_id",explode(",",$request->input("logistic_id")));
  483. $this->expressValidator($request->input())->validate();
  484. /** @var OwnerPriceExpress $model */
  485. $model = app("OwnerPriceExpressService")->create([
  486. "name" => $request->input("name"),
  487. "initial_weight" => $request->input("initial_weight"),
  488. "additional_weight" => $request->input("additional_weight"),
  489. ]);
  490. $result = $model->owners()->sync($request->input("owner_id"));
  491. app("OwnerService")->refreshRelevance($result["attached"],2);
  492. app("OwnerService")->refreshRelevance($result["detached"],2,true);
  493. $model->logistics()->sync($request->input("logistic_id"));
  494. LogService::log(__METHOD__,"计费模型-录入快递计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  495. return response()->redirectTo("maintenance/priceModel/express")->with("successTip","录入“".$request->input("name")."”成功");
  496. }
  497. public function expressEdit($id)
  498. {
  499. if(!Gate::allows('计费模型-快递-编辑')){ return redirect('denied'); }
  500. /** @var OwnerPriceExpress $model */
  501. $model = app('OwnerPriceExpressService')->find($id)->append(["owner_id","logistic_id"]);
  502. $owners = app("OwnerService")->getIntersectPermitting();
  503. $logistics = app('LogisticService')->getSelection();
  504. return response()->view('maintenance.priceModel.express.create',compact("owners","logistics","model"));
  505. }
  506. public function expressUpdate($id,Request $request)
  507. {
  508. if(!Gate::allows('计费模型-快递-编辑')){ return redirect('denied'); }
  509. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  510. $request->offsetSet("logistic_id",explode(",",$request->input("logistic_id")));
  511. $model = app("OwnerPriceExpressService")->find($id);
  512. $values = ["name" => $request->input("name"),
  513. "initial_weight" => $request->input("initial_weight"),
  514. "additional_weight" => $request->input("additional_weight"),
  515. ];
  516. if ($model->operation){
  517. $this->expressValidator($request->input(),$id)->validate();
  518. /** @var OwnerPriceExpress $model */
  519. app("OwnerPriceExpressService")->update(["id"=>$id],$values);
  520. $model = new OwnerPriceExpress();
  521. $model->id = $id;
  522. $result = $model->owners()->sync($request->input("owner_id"));
  523. app("OwnerService")->refreshRelevance($result["attached"],2);
  524. app("OwnerService")->refreshRelevance($result["detached"],2,true);
  525. $model->logistics()->sync($request->input("logistic_id"));
  526. }else app("OwnerPriceExpressService")->copy($model,$values,$request->input("owner_id"),$request->input("logistic_id"));
  527. LogService::log(__METHOD__,"计费模型-修改快递计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  528. return response()->redirectTo("maintenance/priceModel/express")->with("successTip","修改“".$request->input("name")."”成功");
  529. }
  530. private function expressValidator(array $params, $id=null)
  531. {
  532. return Validator::make($params,[
  533. 'name'=>['required'],
  534. 'initial_weight'=>['required','numeric','min:0'],
  535. 'additional_weight'=>['required','numeric','min:0'],
  536. 'owner_id'=>[function ($attribute, $value, $fail)use($id,$params) {
  537. $owners = app("OwnerPriceExpressService")->getExistOwnerName($value,$params["logistic_id"] ?? [],$id);
  538. if ($owners)$fail("(".implode(',',$owners).') 已经绑定计费模型');
  539. }],
  540. 'amount_interval.*'=>['required','integer','min:0'],
  541. 'weight_interval.*'=>['array'],
  542. 'weight_interval.*.*'=>['required','numeric','min:0'],
  543. 'items.*.province_id'=>['required'],
  544. 'items.*.additional_weight_price.*.*'=>['required','numeric',"min:0"],
  545. 'items.*.initial_weight_price.*.*'=>['required','numeric',"min:0"],
  546. ],[
  547. 'required'=>':attribute 为必填项',
  548. 'unique' => ':attribute 已存在',
  549. 'min' => ':attribute 不得小于0',
  550. ],[
  551. 'name' =>"名称",
  552. 'initial_weight' =>"首重",
  553. 'additional_weight' =>"续重",
  554. 'amount_interval.*' =>"数量区间",
  555. 'weight_interval.*' =>"重量区间",
  556. 'weight_interval.*.*' =>"重量区间",
  557. "items.*.province_id"=>"省份",
  558. "items.*.additional_weight_price.*.*"=>"续重价格",
  559. "items.*.initial_weight_price.*.*"=>"首重价格",
  560. ]);
  561. }
  562. public function expressDestroy($id)
  563. {
  564. $this->gate("计费模型-快递-删除");
  565. $model = app("OwnerPriceExpressService")->find($id);
  566. if ($model->operation && $model->target_id){
  567. app("OwnerPriceExpressService")->destroy($id);
  568. $id = $model->target_id;
  569. }
  570. app("OwnerPriceExpressService")->update(["id"=>$id],["operation"=>"D"]);
  571. $this->success();
  572. }
  573. public function logisticIndex(Request $request)
  574. {
  575. if(!Gate::allows('计费模型-物流-查询')){ return redirect('denied'); }
  576. $models = app("OwnerPriceLogisticService")->paginate($request->input("id"))->append(["unit_range_json","other_unit_range_json"]);
  577. return response()->view('maintenance.priceModel.logistic.index',compact("models"));
  578. }
  579. public function logisticCreate()
  580. {
  581. if(!Gate::allows('计费模型-物流-录入')){ return redirect('denied'); }
  582. $owners = app("OwnerService")->getIntersectPermitting();
  583. $logistics = app('LogisticService')->getSelection();
  584. $units = app('UnitService')->getSelection();
  585. return response()->view('maintenance.priceModel.logistic.create',compact("owners","logistics","units"));
  586. }
  587. public function logisticStore(Request $request)
  588. {
  589. if(!Gate::allows('计费模型-物流-录入')){ return redirect('denied'); }
  590. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  591. $request->offsetSet("logistic_id",explode(",",$request->input("logistic_id")));
  592. $this->logisticValidator($request->input())->validate();
  593. /** @var OwnerPriceLogistic $model */
  594. $model = app("OwnerPriceLogisticService")->create([
  595. "name" => $request->input("name"),
  596. "pick_up_price" => $request->input("pick_up_price"),
  597. "fuel_price" => $request->input("fuel_price"),
  598. "service_price" => $request->input("service_price"),
  599. "unit_id" => $request->input("unit_id"),
  600. "unit_range" => $request->input("unit_range"),
  601. "other_unit_id" => $request->input("other_unit_id"),
  602. "other_unit_range" => $request->input("other_unit_range"),
  603. ]);
  604. $result = $model->owners()->sync($request->input("owner_id"));
  605. app("OwnerService")->refreshRelevance($result["attached"],3);
  606. app("OwnerService")->refreshRelevance($result["detached"],3,true);
  607. $model->logistics()->sync($request->input("logistic_id"));
  608. LogService::log(__METHOD__,"计费模型-录入物流计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  609. return response()->redirectTo("maintenance/priceModel/logistic")->with("successTip","创建“".$request->input("name")."”成功");
  610. }
  611. public function logisticEdit($id)
  612. {
  613. if(!Gate::allows('计费模型-物流-编辑')){ return redirect('denied'); }
  614. $owners = app("OwnerService")->getIntersectPermitting();
  615. $logistics = app('LogisticService')->getSelection(['id','name'],'物流');
  616. $units = app('UnitService')->getSelection();
  617. $model = app("OwnerPriceLogisticService")->find($id)->append(["owner_id","logistic_id"]);
  618. return response()->view('maintenance.priceModel.logistic.create',compact("owners","logistics","units","model"));
  619. }
  620. public function logisticUpdate($id, Request $request)
  621. {
  622. if(!Gate::allows('计费模型-物流-编辑')){ return redirect('denied'); }
  623. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  624. $request->offsetSet("logistic_id",explode(",",$request->input("logistic_id")));
  625. $this->logisticValidator($request->input(),$id)->validate();
  626. $values = ["name" => $request->input("name"),
  627. "pick_up_price" => $request->input("pick_up_price"),
  628. "fuel_price" => $request->input("fuel_price"),
  629. "service_price" => $request->input("service_price"),
  630. "unit_id" => $request->input("unit_id"),
  631. "unit_range" => $request->input("unit_range"),
  632. "other_unit_id" => $request->input("other_unit_id"),
  633. "other_unit_range" => $request->input("other_unit_range"),
  634. ];
  635. $model = app("OwnerPriceLogisticService")->find($id);
  636. if ($model->operation){
  637. app("OwnerPriceLogisticService")->update(["id"=>$id],$values);
  638. $model = new OwnerPriceLogistic();
  639. $model->id = $id;
  640. $result = $model->owners()->sync($request->input("owner_id"));
  641. app("OwnerService")->refreshRelevance($result["attached"],3);
  642. app("OwnerService")->refreshRelevance($result["detached"],3,true);
  643. $model->logistics()->sync($request->input("logistic_id"));
  644. }else app("OwnerPriceLogisticService")->copy($model,$values,$request->input("owner_id"),$request->input("logistic_id"));
  645. LogService::log(__METHOD__,"计费模型-修改物流计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  646. return response()->redirectTo("maintenance/priceModel/logistic")->with("successTip","修改“".$request->input("name")."”成功");
  647. }
  648. public function logisticGetDetail(Request $request)
  649. {
  650. $this->gate("计费模型-物流-查询");
  651. $model = new OwnerPriceLogistic();
  652. $model->id = $request->input("id");
  653. $model->load(["details"=>function($query){
  654. /** @var Builder $query */
  655. $query->with(["unit","province","city"]);
  656. }]);
  657. $this->success($model->details);
  658. }
  659. public function expressExport($id)
  660. {
  661. if(!Gate::allows('计费模型-快递-查询')){ return ["success"=>false,"data"=>"无权操作"]; }
  662. $model = app("OwnerPriceExpressService")->find($id,[
  663. "owners","logistics","details"=>function($query){
  664. /** @var Builder $query */
  665. $query->with("province");
  666. }]);
  667. $row = ["客户","首重","续重","承运商"];
  668. $list = [[
  669. implode(",",array_column($model->owners->toArray(),"name")),
  670. $model->initial_weight,
  671. $model->additional_weight,
  672. implode(",",array_column($model->logistics->toArray(),"name")),
  673. ],[
  674. "价格名称","省","首重价格","续重价格",
  675. ]];
  676. foreach ($model->details as $detail){
  677. $list[] = [
  678. $model->name,
  679. $detail->province ? $detail->province->name : '',
  680. $detail->initial_weight_price,
  681. $detail->additional_weight_price,
  682. ];
  683. }
  684. return Export::make($row,$list,"快递计费模型");
  685. }
  686. public function logisticImport(Request $request)
  687. {
  688. if(!Gate::allows('计费模型-物流-录入')){ return ["success"=>false,"data"=>"无权操作"]; }
  689. $fileSuffix=$request->file('file')->getClientOriginalExtension();
  690. if ($fileSuffix != 'xlsx' && $fileSuffix != 'xls' && $fileSuffix != 'csv')
  691. return ['success'=>false,'data'=>'不支持该文件类型'];
  692. ini_set('max_execution_time',2500);
  693. ini_set('memory_limit','1526M');
  694. $fileSuffix = ucwords($fileSuffix);
  695. if (!$request->has("id")){
  696. Excel::import(new OwnerPriceLogisticDetailImport(null),$request->file('file')->path(),null,$fileSuffix);
  697. }else{
  698. /** @var OwnerPriceLogistic $model */
  699. $model = app('OwnerPriceLogisticService')->find($request->input("id"),["unit","otherUnit","details"]);
  700. Excel::import(new OwnerPriceLogisticDetailImport($model),$request->file('file')->path(),null,$fileSuffix);
  701. }
  702. if (Cache::has('logistic'))return Cache::pull('logistic');
  703. return ["success"=>false,"data"=>"导入发生错误,数据无响应"];
  704. }
  705. public function logisticExport($id)
  706. {
  707. $this->gate("计费模型-物流-查询");
  708. $model = app("OwnerPriceLogisticService")->find($id,[
  709. "owners","logistics","unit","otherUnit","details"=>function($query){
  710. /** @var Builder $query */
  711. $query->with(["province","unit","city"]);
  712. }]);
  713. $row = ["客户","价格名称",
  714. "单位一区间/".($model->unit ? $model->unit->name : ''),
  715. "单位二区间/".($model->otherUnit ? $model->otherUnit->name : ''),
  716. "提货费","燃油附加费","信息服务费","创建时间","承运商"];
  717. $range = "";
  718. foreach (explode(",",$model->unit_range) as $str){
  719. $range .= $str."\r\n";
  720. }
  721. $otherRange = "";
  722. foreach (explode(",",$model->other_unit_range) as $str){
  723. $otherRange .= $str."\r\n";
  724. }
  725. $list = [[
  726. implode(",",array_column($model->owners->toArray(),"name")),
  727. $model->name,
  728. $range,
  729. $otherRange,
  730. $model->pick_up_price,
  731. $model->fuel_price,
  732. $model->service_price,
  733. $model->created_at,
  734. implode(",",array_column($model->logistics->toArray(),"name")),
  735. ],[
  736. "单位","区间","省份","市","单价","送货费","起始计费","起始计数","费率"
  737. ]];
  738. foreach ($model->details as $detail){
  739. $list[] = [
  740. $detail->unit ? $detail->unit->name : '',
  741. $detail->range,
  742. $detail->province ? $detail->province->name : '',
  743. $detail->city ? $detail->city->name : '',
  744. $detail->unit_price,
  745. $detail->delivery_fee,
  746. $detail->initial_fee,
  747. $detail->initial_amount,
  748. $detail->rate ? $detail->rate."%" : '',
  749. ];
  750. }
  751. return Export::make($row,$list,"物流计费模型");
  752. }
  753. public function logisticUpdateDetail(Request $request)
  754. {
  755. $this->gate("计费模型-物流-编辑");
  756. $detail = $request->input("detail");
  757. if ($detail["id"]){
  758. $item = OwnerPriceLogisticDetail::query()->with("ownerPriceLogistic")->first();
  759. $obj = [
  760. "unit_price" => $detail["unit_price"],
  761. "delivery_fee" => $detail["delivery_fee"],
  762. "initial_fee" => $detail["initial_fee"],
  763. "initial_amount" => $detail["initial_amount"],
  764. "rate" => $detail["rate"],
  765. ];
  766. if ($item->ownerPriceLogistic->operation){
  767. app('OwnerPriceLogisticService')->updateDetail(["id"=>$detail["id"]],$obj);
  768. }else app('OwnerPriceLogisticService')->copy($item->ownerPriceLogistic,[],null,null,[$detail["id"]=>$obj]);
  769. }else{
  770. $row = app('OwnerPriceLogisticService')->isExistDetail([
  771. "owner_price_logistic_id"=>$request->input("id"),
  772. "unit_id"=>$detail["unit_id"],
  773. "range"=>$detail["range"],
  774. "province_id"=>$detail["province_id"],
  775. "city_id"=>$detail["city_id"],
  776. ]);
  777. if ($row>0)$this->error("已存在该计费模型");
  778. $model = app('OwnerPriceLogisticService')->find(request("id"));
  779. $id = request("id");
  780. if(!$model->operation)$id = app('OwnerPriceLogisticService')->copy($model)->id;
  781. /** @var OwnerPriceExpressProvince $detail */
  782. $detail = app('OwnerPriceLogisticService')->createDetail([
  783. "owner_price_logistic_id"=>$id,
  784. "unit_id"=>$detail["unit_id"],
  785. "range"=>$detail["range"],
  786. "province_id"=>$detail["province_id"],
  787. "city_id"=>$detail["city_id"],
  788. "unit_price" => $detail["unit_price"],
  789. "delivery_fee" => $detail["delivery_fee"],
  790. "initial_fee" => $detail["initial_fee"],
  791. "initial_amount" => $detail["initial_amount"],
  792. "rate" => $detail["rate"],
  793. ]);
  794. $detail->load("province","unit","city");
  795. }
  796. LogService::log(__METHOD__,"计费模型-修改物流计费详情",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  797. $this->success($detail);
  798. }
  799. public function logisticDestroyDetail(Request $request)
  800. {
  801. $this->gate("计费模型-物流-删除");
  802. $id = $request->input("id");
  803. if (!$id) $this->error("非法参数");
  804. /** @var \stdClass $item */
  805. $item = OwnerPriceLogisticDetail::query()->with("ownerPriceLogistic")->find($id);
  806. if ($item->ownerPriceLogistic->operation){
  807. app("OwnerPriceLogisticService")->destroyDetail($id);
  808. }else{
  809. $model = app("OwnerPriceLogisticService")->copy($item->ownerPriceLogistic);
  810. OwnerPriceLogisticDetail::query()->where("owner_price_logistic_id",$model->id)
  811. ->where("unit_id",$item->unit_id)->where("range",$item->range)
  812. ->where("province_id",$item->province_id)->where("city_id",$item->city_id)
  813. ->delete();
  814. }
  815. LogService::log(__METHOD__,"计费模型-删除物流计费详情",$id);
  816. $this->success();
  817. }
  818. public function logisticDestroy($id)
  819. {
  820. $this->gate("计费模型-物流-删除");
  821. if (!$id)$this->error("非法参数");
  822. $model = app("OwnerPriceLogisticService")->find($id);
  823. if ($model->operation && $model->target_id){
  824. app("OwnerPriceLogisticService")->destroy($id);
  825. $id = $model->target_id;
  826. }
  827. app("OwnerPriceLogisticService")->update(["id"=>$id],["operation"=>"D"]);
  828. $this->success();
  829. }
  830. private function logisticValidator(array $params,$id = null)
  831. {
  832. return Validator::make($params,[
  833. 'name'=>['required',$id?"unique:owner_price_logistics,name,$id":'unique:owner_price_logistics,name'],
  834. 'pick_up_price'=>['nullable','numeric','min:0'],
  835. 'fuel_price'=>['nullable','numeric','min:0'],
  836. 'service_price'=>['nullable','numeric','min:0'],
  837. 'unit_id'=>['required'],
  838. 'unit_range'=>['required',function ($attribute, $value, $fail) {
  839. $bool = app("OwnerPriceLogisticService")->checkRange($value);
  840. if (!$bool)$fail("格式错误,值必须为连续的且最后一个值不允许封闭");
  841. }],
  842. 'other_unit_id'=>['required'],
  843. 'other_unit_range'=>['required',function ($attribute, $value, $fail) {
  844. $bool = app("OwnerPriceLogisticService")->checkRange($value);
  845. if (!$bool)$fail("格式错误,值必须为连续的且最后一个值不允许封闭");
  846. }],
  847. 'owner_id'=>[function ($attribute, $value, $fail)use($id,$params) {
  848. $owners = app("OwnerPriceExpressService")->getExistOwnerName($value,$params["logistic_id"] ?? [],$id,"ownerPriceLogistics");
  849. if ($owners)$fail("(".implode(',',$owners).') 已经绑定计费模型');
  850. }],
  851. 'items.*.unit_id'=>['sometimes','required'],
  852. 'items.*.range'=>['sometimes','required'],
  853. 'items.*.province_id'=>['sometimes','required'],
  854. 'items.*.city_id'=>['sometimes','required'],
  855. 'items.*.unit_price'=>['sometimes','nullable','numeric',"min:0"],
  856. 'items.*.delivery_fee'=>['sometimes','nullable','numeric',"min:0"],
  857. 'items.*.initial_fee'=>['sometimes','nullable','numeric',"min:0"],
  858. 'items.*.initial_amount'=>['sometimes','nullable','numeric',"min:0"],
  859. 'items.*.rate'=>['sometimes','nullable','numeric',"min:0"],
  860. ],[
  861. 'required'=>':attribute 为必填项',
  862. 'unique' => ':attribute 已存在',
  863. 'numeric' => ':attribute 必须为数字',
  864. 'min' => ':attribute 不得为负',
  865. ],[
  866. 'name' =>"名称",
  867. 'pick_up_price' =>"提货费",
  868. 'fuel_price' =>"燃油附加费",
  869. 'service_price' =>"信息服务费",
  870. 'unit_id' =>"单位一",
  871. 'unit_range' =>"区间值",
  872. 'other_unit_id' =>"单位二",
  873. 'other_unit_range' =>"区间值",
  874. 'items.*.unit_id'=>"单位",
  875. 'items.*.range'=>"区间",
  876. 'items.*.province_id'=>"省份",
  877. 'items.*.city_id'=>"城市",
  878. 'items.*.unit_price'=>"单价",
  879. 'items.*.delivery_fee'=>"送货费",
  880. 'items.*.initial_fee'=>"起始计费",
  881. 'items.*.initial_amount'=>"起始计数",
  882. 'items.*.rate'=>"费率",
  883. ]);
  884. }
  885. public function directLogisticIndex(Request $request){
  886. if(!Gate::allows('计费模型-直发-查询')){ return redirect('denied'); }
  887. $models = app("OwnerPriceDirectLogisticService")->paginate($request->input("id"));
  888. return response()->view('maintenance.priceModel.directLogistic.index',compact("models"));
  889. }
  890. public function directLogisticCreate(){
  891. if(!Gate::allows('计费模型-直发-录入')){ return redirect('denied'); }
  892. $owners = app("OwnerService")->getIntersectPermitting();
  893. return response()->view('maintenance.priceModel.directLogistic.create',compact("owners"));
  894. }
  895. public function directLogisticStore(Request $request)
  896. {
  897. if(!Gate::allows('计费模型-直发-录入')){ return redirect('denied'); }
  898. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  899. $this->directLogisticValidator($request->input())->validate();
  900. /** @var OwnerPriceDirectLogistic $model */
  901. $model = app("OwnerPriceDirectLogisticService")->create([
  902. "name" => $request->input("name"),
  903. "base_km" => $request->input("base_km"),
  904. ]);
  905. $result = $model->owners()->sync($request->input("owner_id"));
  906. app("OwnerService")->refreshRelevance($result["attached"],4);
  907. app("OwnerService")->refreshRelevance($result["detached"],4,true);
  908. LogService::log(__METHOD__,"计费模型-录入直发车计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  909. return response()->redirectTo("maintenance/priceModel/directLogistic")->with("successTip","创建“".$request->input("name")."”成功");
  910. }
  911. public function directLogisticDestroy($id)
  912. {
  913. $this->gate("计费模型-直发-删除");
  914. $model = app("OwnerPriceDirectLogisticService")->find($id);
  915. if($model->operation && $model->target_id){
  916. app("OwnerPriceDirectLogisticService")->destroy($id);
  917. $id = $model->target_id;
  918. }
  919. app("OwnerPriceDirectLogisticService")->update(["id"=>$id],["operation"=>"D"]);
  920. $this->success();
  921. }
  922. public function directLogisticEdit($id)
  923. {
  924. if(!Gate::allows('计费模型-直发-编辑')){ return redirect('denied'); }
  925. $owners = app("OwnerService")->getIntersectPermitting();
  926. $model = app("OwnerPriceDirectLogisticService")->find($id)->append("owner_id");
  927. return response()->view('maintenance.priceModel.directLogistic.create',compact("model","owners"));
  928. }
  929. public function directLogisticUpdate($id, Request $request)
  930. {
  931. if(!Gate::allows('计费模型-直发-编辑')){ return redirect('denied'); }
  932. $request->offsetSet("owner_id",explode(",",$request->input("owner_id")));
  933. $this->directLogisticValidator($request->input(),$id)->validate();
  934. $model = app("OwnerPriceDirectLogisticService")->find($id);
  935. $values = [
  936. "name" => $request->input("name"),
  937. "base_km" => $request->input("base_km"),
  938. ];
  939. if ($model->operation){
  940. app("OwnerPriceDirectLogisticService")->update(["id"=>$id],$values);
  941. $model = new OwnerPriceDirectLogistic();
  942. $model->id = $id;
  943. $result = $model->owners()->sync($request->input("owner_id"));
  944. app("OwnerService")->refreshRelevance($result["attached"],4);
  945. app("OwnerService")->refreshRelevance($result["detached"],4,true);
  946. }else app("OwnerPriceDirectLogisticService")->copy($model,$values,request("owner_id"));
  947. LogService::log(__METHOD__,"计费模型-修改直发车计费",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  948. return response()->redirectTo("maintenance/priceModel/directLogistic")->with("successTip","修改“".$request->input("name")."”成功");
  949. }
  950. public function directLogisticGetDetail(Request $request)
  951. {
  952. $this->gate("计费模型-直发-查询");
  953. $model = new OwnerPriceDirectLogistic();
  954. $model->id = $request->input("id");
  955. $model->load(["details"=>function($query){
  956. /** @var Builder $query */
  957. $query->with("carType");
  958. }]);
  959. $this->success($model->details);
  960. }
  961. public function directLogisticImport(Request $request)
  962. {
  963. if(!Gate::allows('计费模型-直发-录入')){ return ["success"=>false,"data"=>"无权操作"]; }
  964. $fileSuffix=$request->file('file')->getClientOriginalExtension();
  965. if ($fileSuffix != 'xlsx' && $fileSuffix != 'xls' && $fileSuffix != 'csv')
  966. return ['success'=>false,'data'=>'不支持该文件类型'];
  967. ini_set('max_execution_time',2500);
  968. ini_set('memory_limit','1526M');
  969. $fileSuffix = ucwords($fileSuffix);
  970. if (!$request->has("id")){
  971. Excel::import(new OwnerPriceDirectLogisticDetailImport(null),$request->file('file')->path(),null,$fileSuffix);
  972. }else{
  973. $model = new OwnerPriceDirectLogistic();
  974. $model->id = $request->input("id");
  975. $model->load("details");
  976. Excel::import(new OwnerPriceDirectLogisticDetailImport($model),$request->file('file')->path(),null,$fileSuffix);
  977. }
  978. if (Cache::has('directLogistic'))return Cache::pull('directLogistic');
  979. return ["success"=>false,"data"=>"导入发生错误,数据无响应"];
  980. }
  981. public function directLogisticUpdateDetail(Request $request)
  982. {
  983. $this->gate("计费模型-直发-编辑");
  984. $detail = $request->input("detail");
  985. if ($detail["id"]){
  986. /** @var \stdClass $item */
  987. $item = OwnerPriceDirectLogisticCar::query()->with("ownerPriceDirectLogistic")->find($detail["id"]);
  988. $obj = [
  989. "base_fee" => $detail["base_fee"],
  990. "additional_fee" => $detail["additional_fee"],
  991. ];
  992. if ($item->ownerPriceDirectLogistic->operation){
  993. app('OwnerPriceDirectLogisticService')->updateDetail(["id"=>$detail["id"]],$obj);
  994. }else{
  995. app('OwnerPriceDirectLogisticService')->copy($item->ownerPriceDirectLogistic,[],null,[$detail["id"]=>$obj]);
  996. }
  997. }else{
  998. $row = app('OwnerPriceDirectLogisticService')->isExistDetail([
  999. "owner_price_direct_logistic_id"=>$request->input("id"),
  1000. "car_type_id"=>$detail["car_type_id"],
  1001. ]);
  1002. if ($row>0)$this->error("已存在该计费模型");
  1003. $model = app('OwnerPriceDirectLogisticService')->find(request("id"));
  1004. $values = [
  1005. "owner_price_direct_logistic_id"=>$request->input("id"),
  1006. "car_type_id"=>$detail["car_type_id"],
  1007. "base_fee" => $detail["base_fee"],
  1008. "additional_fee" => $detail["additional_fee"],
  1009. ];
  1010. if (!$model->operation){
  1011. $model = app('OwnerPriceDirectLogisticService')->copy($model);
  1012. $values["owner_price_direct_logistic_id"] = $model->id;
  1013. }
  1014. /** @var OwnerPriceExpressProvince $detail */
  1015. $detail = app('OwnerPriceDirectLogisticService')->createDetail($values);
  1016. $detail->load("carType");
  1017. }
  1018. LogService::log(__METHOD__,"计费模型-修改直发车计费详情",json_encode($request->input(),JSON_UNESCAPED_UNICODE));
  1019. $this->success($detail);
  1020. }
  1021. public function directLogisticDestroyDetail(Request $request)
  1022. {
  1023. $this->gate("计费模型-直发-删除");
  1024. $id = $request->input("id");
  1025. if (!$id)$this->error("非法参数");
  1026. /** @var \stdClass $item */
  1027. $item = OwnerPriceDirectLogisticCar::query()->with("ownerPriceDirectLogistic")->find($id);
  1028. if ($item->ownerPriceDirectLogistic->operation){
  1029. app("OwnerPriceDirectLogisticService")->destroyDetail($id);
  1030. }else{
  1031. $model = app("OwnerPriceDirectLogisticService")->copy($item->ownerPriceDirectLogistic);
  1032. OwnerPriceDirectLogisticCar::query()->where("owner_price_direct_logistic_id",$model->id)
  1033. ->where("car_type_id",$item->car_type_id)->delete();
  1034. }
  1035. LogService::log(__METHOD__,"计费模型-删除直发车计费详情",$id);
  1036. $this->success();
  1037. }
  1038. private function directLogisticValidator(array $params, $id= null)
  1039. {
  1040. return Validator::make($params,[
  1041. 'name'=>['required',$id?"unique:owner_price_direct_logistics,name,$id":'unique:owner_price_direct_logistics,name'],
  1042. 'base_km'=>['required','numeric','min:0'],
  1043. 'owner_id'=>[function ($attribute, $value, $fail)use($id) {
  1044. $owners = app("OwnerPriceDirectLogisticService")->getExistOwnerName($value,$id);
  1045. if ($owners)$fail("(".implode(',',$owners).') 已经绑定直发计费模型');
  1046. }],
  1047. ],[
  1048. 'required'=>':attribute 为必填项',
  1049. 'unique' => ':attribute 已存在',
  1050. 'numeric' => ':attribute 必须为数字',
  1051. 'min' => ':attribute 不得为负',
  1052. ],[
  1053. 'name' =>"名称",
  1054. 'base_km' =>"起步公里数",
  1055. ]);
  1056. }
  1057. public function apiStoreStorage()
  1058. {
  1059. $this->gate("计费模型-仓储-录入");
  1060. $errors = $this->storageValidator(request()->input())->errors();
  1061. if (count($errors)>0)$this->success(["errors"=>$errors]);
  1062. if (!request("owner_id"))$this->error("参数传递错误");
  1063. $values = [
  1064. "name" => request("name"),
  1065. "counting_type" => request("counting_type"),
  1066. "using_type" => request("using_type"),
  1067. "minimum_area" => request("minimum_area") ?? 0,
  1068. "price" => request("price"),
  1069. "discount_type" => request("discount_type"),
  1070. "discount_value" => request("discount_value") ?? 0,
  1071. "unit_id" => request("unit_id"),
  1072. "time_unit_id" => request("time_unit_id"),
  1073. "amount_interval" => request("amount_interval") ?? null,
  1074. "tax_rate_id" => request("tax_rate_id") ?? null,
  1075. ];
  1076. if (request("id")){
  1077. $model = app('OwnerStoragePriceModelService')->find(request("id"));
  1078. if ($model->operation){
  1079. app('OwnerStoragePriceModelService')->update(["id"=>request("id")],$values);
  1080. $this->success();
  1081. }else $this->success(app('OwnerStoragePriceModelService')->copy($model,$values));
  1082. }else{
  1083. $model = app('OwnerStoragePriceModelService')->create($values);
  1084. DB::insert(DB::raw("INSERT INTO owner_storage_price_model_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
  1085. app("OwnerService")->refreshRelevance(request("owner_id"),0);
  1086. $this->success($model->id);
  1087. }
  1088. }
  1089. public function apiStoreOperation()
  1090. {
  1091. $this->gate("计费模型-作业-录入");
  1092. $params = request()->input();
  1093. $params["owner_id"] = [$params["owner_id"]];
  1094. $errors = $this->operationValidator($params,request("id"))->errors();
  1095. if (count($errors)>0)$this->success(["errors"=>$errors]);
  1096. if (!request("owner_id"))$this->error("参数传递错误");
  1097. $operation = [
  1098. "name" => request("name"),
  1099. "max_fee" => request("max_fee") ?? null,
  1100. "operation_type" => request("operation_type"),
  1101. "strategy" => request("strategy"),
  1102. "feature" => request("feature"),
  1103. "type_mark" => request()->has("type_mark") ? \request("type_mark") : null,
  1104. "surcharge" => request("surcharge") ? request("surcharge") : null,
  1105. "surcharge_unit_id" => request("surcharge_unit_id") ? request("surcharge_unit_id") : null,
  1106. "remark" => request("remark"),
  1107. "discount_count" => implode(",",request("discount_count")),
  1108. "total_price" => request("total_price"),
  1109. "total_discount_price"=> implode(",",request("total_discount_price")),
  1110. "tax_rate_id" => request("tax_rate_id") ?? null,
  1111. ];
  1112. if (request("id")){
  1113. $model = app('OwnerPriceOperationService')->find(request("id"),["items"]);
  1114. if ($model->operation){
  1115. app('OwnerPriceOperationService')->findUpdate($model,$operation);
  1116. $delete = [];//需要删除子项
  1117. $update = [["id","strategy","amount","unit_id","unit_price","feature","discount_price","odd_price"]];//需要更新子项
  1118. $insert = [];//需要新增子项
  1119. foreach ($params["items"] as $item){
  1120. $obj = [
  1121. "strategy" => $item["strategy"],
  1122. "amount" => $item["amount"],
  1123. "unit_id" => $item["unit_id"],
  1124. "unit_price"=> $item["unit_price"],
  1125. "feature" => $item["feature"] ?? null,
  1126. "odd_price" => $item["odd_price"] ?? null,
  1127. "discount_price" => implode(",",$item["discount_price"] ?? []),
  1128. ];
  1129. if (isset($item["id"])){
  1130. $obj["id"] = $item["id"];
  1131. $update[] = $obj;
  1132. $delete[] = $item["id"];
  1133. } else{
  1134. $obj["owner_price_operation_id"] = $model->id;
  1135. $obj["feature"] = $item["feature"] ?? null;
  1136. $insert[] = $obj;
  1137. }
  1138. }
  1139. $ids = array_column($model->items->toArray(),"id");
  1140. $delete = array_diff($ids,$delete);
  1141. if ($delete)app("OwnerPriceOperationItemService")->destroy($delete);
  1142. if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_operation_items",$update);
  1143. if ($insert)app("OwnerPriceOperationItemService")->insert($insert);
  1144. }else $model = app("OwnerPriceOperationService")->copy($model,$operation,null,$params["items"],false);
  1145. }else{
  1146. DB::transaction(function ()use(&$model,$params,$operation){
  1147. $model = app('OwnerPriceOperationService')->create($operation);
  1148. foreach ($params["items"] as &$item){
  1149. $temp = [];
  1150. $temp["discount_price"] = implode(",",$item["discount_price"] ?? []);
  1151. $temp["owner_price_operation_id"] = $model->id;
  1152. $temp["feature"] = $item["feature"] ?? null;
  1153. $temp["odd_price"] = $item["odd_price"] ?? null;
  1154. $temp["strategy"] = $item["strategy"];
  1155. $temp["amount"] = $item["amount"];
  1156. $temp["unit_id"] = $item["unit_id"];
  1157. $temp["unit_price"] = $item["unit_price"];
  1158. $item = $temp;
  1159. }
  1160. app("OwnerPriceOperationItemService")->insert($params["items"]);
  1161. DB::insert(DB::raw("INSERT INTO owner_price_operation_owner(owner_price_operation_id,owner_id) VALUES(?,?)"),[$model->id,request("owner_id")]);
  1162. app("OwnerService")->refreshRelevance(request("owner_id"),1);
  1163. DB::commit();
  1164. });
  1165. }
  1166. /** @var OwnerPriceOperation $model */
  1167. $model->load("items");
  1168. $this->success($model);
  1169. }
  1170. public function apiStoreExpress()
  1171. {
  1172. $this->gate("计费模型-快递-录入");
  1173. $params = request()->input();
  1174. $params["logistic_id"] = $params["logistics"];
  1175. $errors = $this->expressValidator($params,request("id"))->errors()->toArray();
  1176. $exist = [];
  1177. foreach ($params["items"] as $index => $item){
  1178. if (isset($exist[$item["province_id"]]))$errors["items.".$index.".province_id"] = ["已存在"];
  1179. else $exist[$item["province_id"]] = true;
  1180. }
  1181. if (count($errors)>0)$this->success(["errors"=>$errors]);
  1182. $obj = [
  1183. "name" => request("name"),
  1184. "initial_weight" => request("initial_weight"),
  1185. "additional_weight" => request("additional_weight"),
  1186. "amount_interval" => request("amount_interval"),
  1187. "weight_interval" => request("weight_interval"),
  1188. "tax_rate_id" => request("tax_rate_id") ?? null,
  1189. ];
  1190. if (request("id")){
  1191. /** @var \stdClass $model */
  1192. $model = app('OwnerPriceExpressService')->find(request("id"),["details"]);
  1193. if ($model->operation){
  1194. app('OwnerPriceExpressService')->update(["id"=>request("id")],$obj);
  1195. $delete = [];//需要删除子项
  1196. $update = [["id","province_id","initial_weight_price","additional_weight_price"]];//需要更新子项
  1197. $insert = [];//需要新增子项
  1198. foreach ($params["items"] as $item){
  1199. $obj = [
  1200. "province_id" => $item["province_id"],
  1201. "initial_weight_price" => $item["initial_weight_price"],
  1202. "additional_weight_price" => $item["additional_weight_price"],
  1203. ];
  1204. if (isset($item["id"])){
  1205. $obj["id"] = $item["id"];
  1206. $update[] = $obj;
  1207. $delete[] = $item["id"];
  1208. } else{
  1209. $obj["owner_price_express_id"] = $model->id;
  1210. $insert[] = $obj;
  1211. }
  1212. }
  1213. $ids = array_column($model->details->toArray(),"id");
  1214. $delete = array_diff($ids,$delete);
  1215. if ($delete)OwnerPriceExpressProvince::destroy($delete);
  1216. if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_express_provinces",$update);
  1217. if ($insert)OwnerPriceExpressProvince::query()->insert($insert);
  1218. /** @var OwnerPriceExpress $model */
  1219. $model->logistics()->sync(request("logistics"));
  1220. }else{
  1221. foreach ($params["items"] as &$item)unset($item["id"]);
  1222. $model = app('OwnerPriceExpressService')->copy($model,$obj,null,request("logistics"),$params["items"],false);
  1223. }
  1224. }else{
  1225. DB::transaction(function ()use(&$model,$params,$obj){
  1226. $model = app('OwnerPriceExpressService')->create($obj);
  1227. foreach ($params["items"] as &$item){
  1228. $item["owner_price_express_id"] = $model->id;
  1229. $item["initial_weight_price"] = json_encode($item["initial_weight_price"]);
  1230. $item["additional_weight_price"] = json_encode($item["additional_weight_price"]);
  1231. }
  1232. OwnerPriceExpressProvince::query()->insert($params["items"]);
  1233. DB::insert(DB::raw("INSERT INTO owner_price_express_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
  1234. app("OwnerService")->refreshRelevance(request("owner_id"),2);
  1235. /** @var OwnerPriceExpress $model */
  1236. $model->logistics()->syncWithoutDetaching(request("logistics"));
  1237. DB::commit();
  1238. });
  1239. }
  1240. $model->load("details");
  1241. $this->success($model);
  1242. }
  1243. public function apiStoreLogistic()
  1244. {
  1245. $this->gate("计费模型-物流-录入");
  1246. $params = request()->input();
  1247. $params["owner_id"] = [$params["owner_id"]];
  1248. $params["logistic_id"] = $params["logistics"];
  1249. $errors = $this->logisticValidator($params,request("id"))->errors()->toArray();
  1250. $exist = [];
  1251. foreach ($params["items"] as $index => $item){
  1252. $key = $item["unit_id"]."-".$item["range"]."-".$item["province_id"]."-".$item["city_id"];
  1253. if (isset($exist[$key]))$errors["items.".$index.".unit_id"] = ["该条已存在"];
  1254. else $exist[$key] = true;
  1255. }
  1256. if (count($errors)>0)$this->success(["errors"=>$errors]);
  1257. $obj = [
  1258. "name" => request('name'),
  1259. "unit_range" => request('unit_range'),
  1260. "unit_id" => request('unit_id'),
  1261. "other_unit_range" => request('other_unit_range'),
  1262. "other_unit_id" => request('other_unit_id'),
  1263. "pick_up_price" => request('pick_up_price'),
  1264. "fuel_price" => request('fuel_price'),
  1265. "service_price" => request('service_price'),
  1266. "tax_rate_id" => request("tax_rate_id") ?? null,
  1267. ];
  1268. if (request("id")){
  1269. $model = app("OwnerPriceLogisticService")->find(request("id"),["details"]);
  1270. if ($model->operation){
  1271. app("OwnerPriceLogisticService")->update(["id"=>request("id")],$obj);
  1272. $delete = [];//需要删除子项
  1273. $update = [["id","unit_id","range","province_id","city_id","unit_price","delivery_fee","initial_fee","initial_amount","rate"]];//需要更新子项
  1274. $insert = [];//需要新增子项
  1275. foreach ($params["items"] as $item){
  1276. $obj = [
  1277. "unit_id" => $item["unit_id"],
  1278. "range" => $item["range"],
  1279. "province_id" => $item["province_id"],
  1280. "city_id" => $item["city_id"],
  1281. "unit_price" => $item["unit_price"],
  1282. "delivery_fee" => $item["delivery_fee"],
  1283. "initial_fee" => $item["initial_fee"],
  1284. "initial_amount" => $item["initial_amount"],
  1285. "rate" => $item["rate"],
  1286. ];
  1287. if (isset($item["id"])){
  1288. $obj["id"] = $item["id"];
  1289. $update[] = $obj;
  1290. $delete[] = $item["id"];
  1291. } else{
  1292. $obj["owner_price_logistic_id"] = $model->id;
  1293. $insert[] = $obj;
  1294. }
  1295. }
  1296. $ids = array_column($model->details->toArray(),"id");
  1297. $delete = array_diff($ids,$delete);
  1298. if ($delete)OwnerPriceLogisticDetail::destroy($delete);
  1299. if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_logistic_details",$update);
  1300. if ($insert)OwnerPriceLogisticDetail::query()->insert($insert);
  1301. /** @var OwnerPriceLogistic $model */
  1302. $model->logistics()->sync(request("logistics"));
  1303. }else{
  1304. foreach ($params["items"] as &$item)unset($item["id"]);
  1305. $model = app("OwnerPriceLogisticService")->copy($model,$obj,null,request("logistics"),$params["items"],false);
  1306. }
  1307. }else{
  1308. DB::transaction(function ()use(&$model,$params,$obj){
  1309. $model = app("OwnerPriceLogisticService")->create($obj);
  1310. foreach ($params["items"] as &$param)$param["owner_price_logistic_id"] = $model->id;
  1311. OwnerPriceLogisticDetail::query()->insert($params["items"]);
  1312. DB::insert(DB::raw("INSERT INTO owner_price_logistic_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
  1313. app("OwnerService")->refreshRelevance(request("owner_id"),3);
  1314. /** @var OwnerPriceLogistic $model */
  1315. $model->logistics()->syncWithoutDetaching(request("logistics"));
  1316. DB::commit();
  1317. });
  1318. }
  1319. $model->load("details");
  1320. $this->success($model);
  1321. }
  1322. public function apiStoreDirectLogistic()
  1323. {
  1324. $this->gate("计费模型-直发-录入");
  1325. $errors = $this->directLogisticValidator(request()->input(),request("id"))->errors()->toArray();
  1326. $exist = [];
  1327. foreach (request("items") as $index=>$item){
  1328. if (isset($exist[$item['car_type_id']]))$errors["items.".$index.".car_type_id"] = ["已存在"];
  1329. else $exist[$item['car_type_id']] = true;
  1330. }
  1331. if (count($errors)>0)$this->success(["errors"=>$errors]);
  1332. $items = request()->input("items");
  1333. $obj = [
  1334. "name" => request("name"),
  1335. "base_km" => request("base_km"),
  1336. "tax_rate_id"=> request("tax_rate_id") ?? null,
  1337. ];
  1338. if (request("id")){
  1339. $model = app("OwnerPriceDirectLogisticService")->find(request("id"),["details"]);
  1340. if ($model->operation){
  1341. app("OwnerPriceDirectLogisticService")->update(["id"=>request("id")],$obj);
  1342. $delete = [];//需要删除子项
  1343. $update = [["id","car_type_id","base_fee","additional_fee"]];//需要更新子项
  1344. $insert = [];//需要新增子项
  1345. foreach ($items as $item){
  1346. $obj = [
  1347. "car_type_id" => $item["car_type_id"],
  1348. "base_fee" => $item["base_fee"],
  1349. "additional_fee" => $item["additional_fee"],
  1350. ];
  1351. if (isset($item["id"])){
  1352. $obj["id"] = $item["id"];
  1353. $update[] = $obj;
  1354. $delete[] = $item["id"];
  1355. } else{
  1356. $obj["owner_price_direct_logistic_id"] = $model->id;
  1357. $insert[] = $obj;
  1358. }
  1359. }
  1360. $ids = array_column($model->details->toArray(),"id");
  1361. $delete = array_diff($ids,$delete);
  1362. if ($delete)OwnerPriceDirectLogisticCar::destroy($delete);
  1363. if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_direct_logistic_cars",$update);
  1364. if ($insert)OwnerPriceDirectLogisticCar::query()->insert($insert);
  1365. }else{
  1366. foreach ($items as &$item)unset($item["id"]);
  1367. $model = app("OwnerPriceDirectLogisticService")->copy($model,$obj,null,$items,false);
  1368. }
  1369. }else{
  1370. DB::transaction(function ()use(&$model,$items,$obj){
  1371. $model = app("OwnerPriceDirectLogisticService")->create($obj);
  1372. foreach ($items as &$item)$item["owner_price_direct_logistic_id"] = $model->id;
  1373. OwnerPriceDirectLogisticCar::query()->insert($items);
  1374. DB::insert(DB::raw("INSERT INTO owner_price_direct_logistic_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
  1375. app("OwnerService")->refreshRelevance(request("owner_id"),4);
  1376. DB::commit();
  1377. });
  1378. }
  1379. /** @var OwnerPriceDirectLogistic $model */
  1380. $model->load("details");
  1381. $this->success($model);
  1382. }
  1383. public function apiStoreSystem()
  1384. {
  1385. $this->gate("计费模型-系统-录入");
  1386. if (!request("owner_id") || !request("usage_fee") || \request("usage_fee")<0)$this->success(["errors"=>["usage_fee"=>["不得小于0"]]]);
  1387. $obj = [
  1388. "usage_fee"=>\request("usage_fee"),
  1389. "tax_rate_id"=>\request("tax_rate_id"),
  1390. "time_unit_id"=>\request("time_unit_id"),
  1391. ];
  1392. if (request("id")){
  1393. /** @var OwnerPriceSystem|\stdClass $model */
  1394. $model = OwnerPriceSystem::query()->find(request("id"));
  1395. $obj["operation"] = "U";
  1396. if ($model->operation)$model->update($obj);
  1397. else{
  1398. $obj["target_id"] = $model->id;
  1399. $obj["owner_id"] = $model->owner_id;
  1400. $model = OwnerPriceSystem::query()->create($obj);
  1401. }
  1402. }else{
  1403. $obj["operation"] = "C";
  1404. $obj["owner_id"] = \request("owner_id");
  1405. $model = OwnerPriceSystem::query()->create($obj);
  1406. }
  1407. $this->success($model);
  1408. }
  1409. public function getPriceModel()
  1410. {
  1411. /** @var Owner|\stdClass $owner */
  1412. $owner = new Owner();
  1413. $owner->id = request("id");
  1414. $owner->load(["ownerStoragePriceModels","ownerPriceOperations"=>function($query){
  1415. /** @var Builder $query */
  1416. $query->with(["items"=>function($query){
  1417. /** @var Builder $query */
  1418. $query->orderByRaw("CASE strategy WHEN '起步' THEN 1 WHEN '默认' THEN 2 WHEN '特征' THEN 3 END");
  1419. }]);
  1420. },"ownerPriceExpresses"=>function($query){
  1421. /** @var Builder $query */
  1422. $query->with(["details","logistics"]);
  1423. },"ownerPriceLogistics"=>function($query){
  1424. /** @var Builder $query */
  1425. $query->with(["details","logistics"]);
  1426. },"ownerPriceDirectLogistics.details","ownerPriceSystem"]);
  1427. $owner->loadCount(["storageAudit","operationAudit","expressAudit","logisticAudit","directLogisticAudit","systemAudit"]);
  1428. $features = app("FeatureService")->getMapArray();
  1429. OwnerPriceOperation::$features = $features;
  1430. OwnerPriceOperationItem::$features = $features;
  1431. foreach ($owner->ownerPriceOperations as &$operation){
  1432. $operation["featureFormat"] = $operation->featureFormat;
  1433. $operation["isRejected"] = $operation->type_mark === 0 ? true : false;
  1434. foreach ($operation->items as &$item){
  1435. $item["featureFormat"] = $item->featureFormat;
  1436. if ($item["strategy"] == "起步")$item["type"] = $item["amount"] ? 0 : 1;
  1437. }
  1438. }
  1439. $this->success($owner);
  1440. }
  1441. public function apiGetStorage()
  1442. {
  1443. $this->gate("项目管理-项目-录入");
  1444. $models = OwnerStoragePriceModel::query()->where(function (Builder $query){
  1445. $query->whereNull("operation")->orWhere("operation","");
  1446. });
  1447. if (request("customer_id")){
  1448. $customerId = request("customer_id");
  1449. $models->whereHas("owners",function ($query)use($customerId){
  1450. /** @var Builder $query */
  1451. $query->where("customer_id",$customerId);
  1452. });
  1453. }
  1454. if (request("owner_id")){
  1455. $ownerId = request("owner_id");
  1456. $models->whereHas("owners",function ($query)use($ownerId){
  1457. /** @var Builder $query */
  1458. $query->where("id",$ownerId);
  1459. });
  1460. }
  1461. if (request("name")){
  1462. $models->where("name","like","%".request("name")."%");
  1463. }
  1464. $this->success($models->get());
  1465. }
  1466. public function apiGetOperation()
  1467. {
  1468. $this->gate("项目管理-项目-录入");
  1469. $models = OwnerPriceOperation::query()->with("items")->where(function (Builder $query){
  1470. $query->whereNull("operation")->orWhere("operation","");
  1471. });
  1472. if (request("customer_id")){
  1473. $customerId = request("customer_id");
  1474. $models->whereHas("owners",function ($query)use($customerId){
  1475. /** @var Builder $query */
  1476. $query->where("customer_id",$customerId);
  1477. });
  1478. }
  1479. if (request("owner_id")){
  1480. $ownerId = request("owner_id");
  1481. $models->whereHas("owners",function ($query)use($ownerId){
  1482. /** @var Builder $query */
  1483. $query->where("id",$ownerId);
  1484. });
  1485. }
  1486. if (request("name")){
  1487. $models->where("name","like","%".request("name")."%");
  1488. }
  1489. $features = app("FeatureService")->getMapArray();
  1490. OwnerPriceOperation::$features = $features;
  1491. OwnerPriceOperationItem::$features = $features;
  1492. $models = $models->get();
  1493. foreach ($models as &$operation){
  1494. $operation["featureFormat"] = $operation->featureFormat;
  1495. $operation["isRejected"] = $operation->type_mark === 0 ? true : false;
  1496. $items = [];
  1497. foreach ($operation->items as $item){
  1498. $items[] = [
  1499. "strategy" => $item["strategy"],
  1500. "amount" => $item["amount"],
  1501. "unit_id" => $item["unit_id"],
  1502. "unit_price" => $item["unit_price"],
  1503. "discount_price" => $item["discount_price"] ?? null,
  1504. "feature" => $item["feature"],
  1505. "featureFormat" => $item->featureFormat,
  1506. ];
  1507. }
  1508. $operation["items"] = $items;
  1509. }
  1510. $this->success($models);
  1511. }
  1512. public function apiGetExpress()
  1513. {
  1514. $this->gate("项目管理-项目-录入");
  1515. $models = OwnerPriceExpress::query()->with(["details","logistics:id"])->where(function (Builder $query){
  1516. $query->whereNull("operation")->orWhere("operation","");
  1517. });
  1518. if (request("customer_id")){
  1519. $customerId = request("customer_id");
  1520. $models->whereHas("owners",function ($query)use($customerId){
  1521. /** @var Builder $query */
  1522. $query->where("customer_id",$customerId);
  1523. });
  1524. }
  1525. if (request("owner_id")){
  1526. $ownerId = request("owner_id");
  1527. $models->whereHas("owners",function ($query)use($ownerId){
  1528. /** @var Builder $query */
  1529. $query->where("id",$ownerId);
  1530. });
  1531. }
  1532. if (request("logistic_id")){
  1533. $logisticId = request("logistic_id");
  1534. $models->whereHas("logistics",function ($query)use($logisticId){
  1535. /** @var Builder $query */
  1536. $query->where("id",$logisticId);
  1537. });
  1538. }
  1539. if (request("name")){
  1540. $models->where("name","like","%".request("name")."%");
  1541. }
  1542. $this->success($models->get());
  1543. }
  1544. public function apiGetLogistic()
  1545. {
  1546. $this->gate("项目管理-项目-录入");
  1547. $models = OwnerPriceLogistic::query()->with(["details","logistics"])->where(function (Builder $query){
  1548. $query->whereNull("operation")->orWhere("operation","");
  1549. });
  1550. if (request("customer_id")){
  1551. $customerId = request("customer_id");
  1552. $models->whereHas("owners",function ($query)use($customerId){
  1553. /** @var Builder $query */
  1554. $query->where("customer_id",$customerId);
  1555. });
  1556. }
  1557. if (request("owner_id")){
  1558. $ownerId = request("owner_id");
  1559. $models->whereHas("owners",function ($query)use($ownerId){
  1560. /** @var Builder $query */
  1561. $query->where("id",$ownerId);
  1562. });
  1563. }
  1564. if (request("logistic_id")){
  1565. $logisticId = request("logistic_id");
  1566. $models->whereHas("logistics",function ($query)use($logisticId){
  1567. /** @var Builder $query */
  1568. $query->where("id",$logisticId);
  1569. });
  1570. }
  1571. if (request("name")){
  1572. $models->where("name","like","%".request("name")."%");
  1573. }
  1574. $this->success($models->get());
  1575. }
  1576. public function apiGetDirectLogistic()
  1577. {
  1578. $this->gate("项目管理-项目-录入");
  1579. $models = OwnerPriceDirectLogistic::query()->with("details")->where(function (Builder $query){
  1580. $query->whereNull("operation")->orWhere("operation","");
  1581. });
  1582. if (request("customer_id")){
  1583. $customerId = request("customer_id");
  1584. $models->whereHas("owners",function ($query)use($customerId){
  1585. /** @var Builder $query */
  1586. $query->where("customer_id",$customerId);
  1587. });
  1588. }
  1589. if (request("owner_id")){
  1590. $ownerId = request("owner_id");
  1591. $models->whereHas("owners",function ($query)use($ownerId){
  1592. /** @var Builder $query */
  1593. $query->where("id",$ownerId);
  1594. });
  1595. }
  1596. if (request("name")){
  1597. $models->where("name","like","%".request("name")."%");
  1598. }
  1599. $this->success($models->get());
  1600. }
  1601. public function apiDelStorage()
  1602. {
  1603. $this->publicDeleteNode("OwnerStoragePriceModelService");
  1604. }
  1605. public function apiDelOperation()
  1606. {
  1607. $this->publicDeleteNode("OwnerPriceOperationService");
  1608. }
  1609. public function apiDelOperationItem()
  1610. {
  1611. $this->gate("项目管理-项目-录入");
  1612. if (!request("id"))$this->error("非法参数");
  1613. /** @var \stdClass $item */
  1614. $item = OwnerPriceOperationItem::query()->with("ownerPriceOperation")->find(request("id"));
  1615. if ($item->ownerPriceOperation->operation){
  1616. app("OwnerPriceOperationItemService")->destroy(request("id"));
  1617. $this->success();
  1618. }else{
  1619. /** @var OwnerPriceOperation $model */
  1620. $model = app("OwnerPriceOperationService")->copy($item->ownerPriceOperation);
  1621. OwnerPriceOperationItem::query()->where("owner_price_operation_id",$model->id)
  1622. ->where("strategy",$item->strategy)
  1623. ->where("amount",$item->amount)
  1624. ->where("unit_id",$item->unit_id)
  1625. ->where("unit_price",$item->unit_price)
  1626. ->where("feature",$item->feature)
  1627. ->where("priority",$item->priority)
  1628. ->where("discount_price",$item->discount_price)->delete();
  1629. $model->load(["items"=>function($query){
  1630. /** @var Builder $query */
  1631. $query->orderByRaw("CASE strategy WHEN '起步' THEN 1 WHEN '默认' THEN 2 WHEN '特征' THEN 3 END");
  1632. }]);
  1633. $this->success($model);
  1634. }
  1635. }
  1636. public function apiDelExpress()
  1637. {
  1638. $this->publicDeleteNode("OwnerPriceExpressService");
  1639. }
  1640. public function apiDelExpressItem()
  1641. {
  1642. $this->gate("项目管理-项目-录入");
  1643. if (!request("id"))$this->error("非法参数");
  1644. /** @var \stdClass $item */
  1645. $item = OwnerPriceExpressProvince::query()->with("ownerPriceExpress")->find(request("id"));
  1646. if ($item->ownerPriceExpress->operation){
  1647. app("OwnerPriceExpressService")->destroyDetail(request("id"));
  1648. }else{
  1649. $model = app("OwnerPriceExpressService")->copy($item->ownerPriceExpress);
  1650. OwnerPriceExpressProvince::query()->where("owner_price_express_id",$model->id)
  1651. ->where("province_id",$item->province_id)->delete();
  1652. }
  1653. $this->success();
  1654. }
  1655. public function apiDelLogistic()
  1656. {
  1657. $this->publicDeleteNode("OwnerPriceLogisticService");
  1658. }
  1659. public function apiDelLogisticItem()
  1660. {
  1661. $this->gate("项目管理-项目-录入");
  1662. if (!request("id"))$this->error("非法参数");
  1663. /** @var \stdClass $item */
  1664. $item = OwnerPriceLogisticDetail::query()->with("ownerPriceLogistic")->find(request("id"));
  1665. if ($item->ownerPriceLogistic->operation){
  1666. app("OwnerPriceLogisticService")->destroyDetail(request("id"));
  1667. }else{
  1668. $model = app("OwnerPriceLogisticService")->copy($item->ownerPriceLogistic);
  1669. OwnerPriceLogisticDetail::query()->where("owner_price_logistic_id",$model->id)
  1670. ->where("unit_id",$item->unit_id)->where("range",$item->range)
  1671. ->where("province_id",$item->province_id)
  1672. ->where("city_id",$item->city_id)->delete();
  1673. }
  1674. $this->success();
  1675. }
  1676. public function apiDelDirectLogistic()
  1677. {
  1678. $this->publicDeleteNode("OwnerPriceDirectLogisticService");
  1679. }
  1680. public function apiDelDirectLogisticItem()
  1681. {
  1682. $this->gate("项目管理-项目-录入");
  1683. if (!request("id"))$this->error("非法参数");
  1684. /** @var \stdClass $item */
  1685. $item = OwnerPriceDirectLogisticCar::query()->with("ownerPriceDirectLogistic")->find(request("id"));
  1686. if ($item->ownerPriceDirectLogistic->operation){
  1687. app("OwnerPriceDirectLogisticService")->destroyDetail(request("id"));
  1688. }else{
  1689. $model = app("OwnerPriceDirectLogisticService")->copy($item->ownerPriceDirectLogistic);
  1690. OwnerPriceDirectLogisticCar::query()->where("owner_price_direct_logistic_id",$model->id)
  1691. ->where("car_type_id",$item->car_type_id)->delete();
  1692. }
  1693. $this->success();
  1694. }
  1695. public function apiDelSystem()
  1696. {
  1697. $this->gate("项目管理-系统-录入");
  1698. if (!request("id"))$this->error("非法参数");
  1699. /** @var OwnerPriceSystem|\stdClass $model */
  1700. $model = OwnerPriceSystem::query()->find(\request("id"));
  1701. if ($model->operation && $model->target_id){
  1702. OwnerPriceSystem::query()->where($model->target_id,\request("id"))->update(["operation"=>"D"]);
  1703. $model->delete();
  1704. }
  1705. $this->success();
  1706. }
  1707. private function publicDeleteNode(string $serviceName)
  1708. {
  1709. $this->gate("项目管理-项目-录入");
  1710. $id = request("id");
  1711. if (!$id)$this->error("非法参数");
  1712. $model = app($serviceName)->find($id);
  1713. if ($model->operation && $model->target_id){
  1714. app($serviceName)->destroy($id);
  1715. $id = $model->target_id;
  1716. }
  1717. app($serviceName)->update(["id"=>$id],["operation"=>"D"]);
  1718. $this->success();
  1719. }
  1720. //审核或恢复计费模型
  1721. public function auditOrRecoverModel()
  1722. {
  1723. $this->gate("项目管理-项目-计费模型-审核");
  1724. $ownerId = request("owner_id");
  1725. if (!$ownerId)$this->error("非法参数");
  1726. /** @var \stdClass|Owner $owner */
  1727. $owner = new Owner();
  1728. $owner->id = $ownerId;
  1729. switch (request("type")){
  1730. case "storage":
  1731. app("OwnerStoragePriceModelService")->auditOrRecover(request("isAudit"),$ownerId);//priceModelAuditOrRecoverQuery
  1732. $owner->load("ownerStoragePriceModels");
  1733. $this->success($owner->ownerStoragePriceModels);
  1734. break;
  1735. case "operation":
  1736. app("OwnerPriceOperationService")->auditOrRecover(request("isAudit"),$ownerId);
  1737. $owner->load(["ownerPriceOperations"=>function($query){
  1738. /** @var Builder $query */
  1739. $query->with(["items"=>function($query){
  1740. /** @var Builder $query */
  1741. $query->orderByRaw("CASE strategy WHEN '起步' THEN 1 WHEN '默认' THEN 2 WHEN '特征' THEN 3 END");
  1742. }]);
  1743. }]);
  1744. $features = app("FeatureService")->getMapArray();
  1745. OwnerPriceOperation::$features = $features;
  1746. OwnerPriceOperationItem::$features = $features;
  1747. foreach ($owner->ownerPriceOperations as &$operation){
  1748. $operation["featureFormat"] = $operation->featureFormat;
  1749. foreach ($operation->items as &$item){
  1750. $item["featureFormat"] = $item->featureFormat;
  1751. if ($operation["operation_type"]==='出库' && $item["strategy"] == "起步"){
  1752. $item["type"] = $item["amount"] ? 0 : 1;
  1753. }
  1754. }
  1755. }
  1756. $this->success($owner->ownerPriceOperations);
  1757. break;
  1758. case "express":
  1759. app("OwnerPriceExpressService")->auditOrRecover(request("isAudit"),$ownerId);
  1760. $owner->load(["ownerPriceExpresses"=>function($query){
  1761. /** @var Builder $query */
  1762. $query->with(["details","logistics"]);
  1763. }]);
  1764. $this->success($owner->ownerPriceExpresses);
  1765. break;
  1766. case "logistic":
  1767. app("OwnerPriceLogisticService")->auditOrRecover(request("isAudit"),$ownerId);
  1768. $owner->load(["ownerPriceLogistics"=>function($query){
  1769. /** @var Builder $query */
  1770. $query->with(["details","logistics"]);
  1771. }]);
  1772. $this->success($owner->ownerPriceLogistics);
  1773. break;
  1774. case "directLogistic":
  1775. app("OwnerPriceDirectLogisticService")->auditOrRecover(request("isAudit"),$ownerId);
  1776. $owner->load("ownerPriceDirectLogistics.details");
  1777. $this->success($owner->ownerPriceDirectLogistics);
  1778. break;
  1779. case "system":
  1780. $result = app(QueryService::class)->priceModelAuditOrRecoverQuery(request("isAudit"),OwnerPriceSystem::query(),$ownerId,null,true);
  1781. if ($result["delete"]){
  1782. OwnerPriceSystem::destroy($result["delete"]);
  1783. app("OwnerService")->refreshRelevance($ownerId,5,true);
  1784. }
  1785. if ($result["update"]){
  1786. OwnerPriceSystem::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
  1787. app("OwnerService")->refreshRelevance($ownerId,5);
  1788. }
  1789. $owner->load("ownerPriceSystem");
  1790. $this->success($owner->ownerPriceSystem);
  1791. break;
  1792. }
  1793. }
  1794. /**
  1795. * 获取审核对比信息
  1796. */
  1797. public function getPriceModelAudit()
  1798. {
  1799. $result = null;
  1800. switch (\request("type")){
  1801. case "storage":
  1802. $result = $this->auditInfoQuery(OwnerStoragePriceModel::query(),\request("owner_id"))->get();
  1803. break;
  1804. case "operation":
  1805. $result = $this->auditInfoQuery(OwnerPriceOperation::query(),\request("owner_id"))->with("items")->get();
  1806. $features = app("FeatureService")->getMapArray();
  1807. OwnerPriceOperation::$features = $features;
  1808. OwnerPriceOperationItem::$features = $features;
  1809. foreach ($result as &$operation){
  1810. $operation->feature = $operation->featureFormat;
  1811. $operation->type_mark = $operation->type_mark !==null ? OwnerPriceOperation::MARK[$operation->type_mark] : '';
  1812. foreach ($operation->items as &$item){
  1813. $item->feature = $item->featureFormat;
  1814. }
  1815. }
  1816. break;
  1817. case "express":
  1818. $result = $this->auditInfoQuery(OwnerPriceExpress::query(),\request("owner_id"))->with(["details","logistics"])->get();
  1819. break;
  1820. case "logistic":
  1821. $result = $this->auditInfoQuery(OwnerPriceLogistic::query(),\request("owner_id"))->with(["details","logistics"])->get();
  1822. break;
  1823. case "directLogistic":
  1824. $result = $this->auditInfoQuery(OwnerPriceDirectLogistic::query(),\request("owner_id"))->with("details")->get();
  1825. break;
  1826. case "system":
  1827. $result = $this->auditInfoQuery(OwnerPriceSystem::query(),\request("owner_id"),false)->get();
  1828. break;
  1829. }
  1830. $this->success($result);
  1831. }
  1832. /**
  1833. * @param Builder $query
  1834. * @param integer $owner
  1835. * @param bool $isRelevance
  1836. *
  1837. * @return Builder
  1838. */
  1839. private function auditInfoQuery(Builder $query,$owner,$isRelevance = true)
  1840. {
  1841. $childQuery = clone $query;
  1842. $query->where(function ($query)use($childQuery){
  1843. $childQuery = $childQuery->select("target_id")->whereNotNull("target_id");
  1844. /** @var Builder $query */
  1845. $query->whereIn("id",$childQuery)->orWhere(function(Builder $query){
  1846. $query->where("operation","!=","")->orWhereNotNull("operation");
  1847. });
  1848. });
  1849. if ($isRelevance)$query->whereHas("owners",function ($query)use($owner){
  1850. /** @var Builder $query */
  1851. $query->where("id",$owner);
  1852. });else $query->where("owner_id",$owner);
  1853. return $query->orderBy("operation");
  1854. }
  1855. }