PriceModelController.php 86 KB

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