PriceModelController.php 89 KB

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