PriceModelController.php 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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. "surcharge" => request("surcharge") ? request("surcharge") : null,
  1100. "surcharge_unit_id" => request("surcharge_unit_id") ? request("surcharge_unit_id") : null,
  1101. "remark" => request("remark"),
  1102. "discount_count" => implode(",",request("discount_count")),
  1103. "total_price" => request("total_price"),
  1104. "total_discount_price"=> implode(",",request("total_discount_price")),
  1105. ];
  1106. if (request("id")){
  1107. $model = app('OwnerPriceOperationService')->find(request("id"),["items"]);
  1108. if ($model->operation){
  1109. app('OwnerPriceOperationService')->findUpdate($model,$operation);
  1110. $delete = [];//需要删除子项
  1111. $update = [["id","strategy","amount","unit_id","unit_price","feature","discount_price","odd_price"]];//需要更新子项
  1112. $insert = [];//需要新增子项
  1113. foreach ($params["items"] as $item){
  1114. $obj = [
  1115. "strategy" => $item["strategy"],
  1116. "amount" => $item["amount"],
  1117. "unit_id" => $item["unit_id"],
  1118. "unit_price"=> $item["unit_price"],
  1119. "feature" => $item["feature"] ?? null,
  1120. "odd_price" => $item["odd_price"] ?? null,
  1121. "discount_price" => implode(",",$item["discount_price"] ?? []),
  1122. ];
  1123. if (isset($item["id"])){
  1124. $obj["id"] = $item["id"];
  1125. $update[] = $obj;
  1126. $delete[] = $item["id"];
  1127. } else{
  1128. $obj["owner_price_operation_id"] = $model->id;
  1129. $obj["feature"] = $item["feature"] ?? null;
  1130. $insert[] = $obj;
  1131. }
  1132. }
  1133. $ids = array_column($model->items->toArray(),"id");
  1134. $delete = array_diff($ids,$delete);
  1135. if ($delete)app("OwnerPriceOperationItemService")->destroy($delete);
  1136. if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_operation_items",$update);
  1137. if ($insert)app("OwnerPriceOperationItemService")->insert($insert);
  1138. }else $model = app("OwnerPriceOperationService")->copy($model,$operation,null,$params["items"],false);
  1139. }else{
  1140. DB::transaction(function ()use(&$model,$params,$operation){
  1141. $model = app('OwnerPriceOperationService')->create($operation);
  1142. foreach ($params["items"] as &$item){
  1143. $temp = [];
  1144. $temp["discount_price"] = implode(",",$item["discount_price"] ?? []);
  1145. $temp["owner_price_operation_id"] = $model->id;
  1146. $temp["feature"] = $item["feature"] ?? null;
  1147. $temp["odd_price"] = $item["odd_price"] ?? null;
  1148. $temp["strategy"] = $item["strategy"];
  1149. $temp["amount"] = $item["amount"];
  1150. $temp["unit_id"] = $item["unit_id"];
  1151. $temp["unit_price"] = $item["unit_price"];
  1152. $item = $temp;
  1153. }
  1154. app("OwnerPriceOperationItemService")->insert($params["items"]);
  1155. DB::insert(DB::raw("INSERT INTO owner_price_operation_owner(owner_price_operation_id,owner_id) VALUES(?,?)"),[$model->id,request("owner_id")]);
  1156. app("OwnerService")->refreshRelevance(request("owner_id"),1);
  1157. DB::commit();
  1158. });
  1159. }
  1160. /** @var OwnerPriceOperation $model */
  1161. $model->load("items");
  1162. $this->success($model);
  1163. }
  1164. public function apiStoreExpress()
  1165. {
  1166. $this->gate("项目管理-项目-录入");
  1167. $params = request()->input();
  1168. $params["logistic_id"] = $params["logistics"];
  1169. $errors = $this->expressValidator($params,request("id"))->errors()->toArray();
  1170. $exist = [];
  1171. foreach ($params["items"] as $index => $item){
  1172. if (isset($exist[$item["province_id"]]))$errors["items.".$index.".province_id"] = ["已存在"];
  1173. else $exist[$item["province_id"]] = true;
  1174. }
  1175. if (count($errors)>0)$this->success(["errors"=>$errors]);
  1176. $obj = [
  1177. "name" => request("name"),
  1178. "initial_weight" => request("initial_weight"),
  1179. "additional_weight" => request("additional_weight"),
  1180. "amount_interval" => request("amount_interval"),
  1181. "weight_interval" => request("weight_interval"),
  1182. ];
  1183. if (request("id")){
  1184. /** @var \stdClass $model */
  1185. $model = app('OwnerPriceExpressService')->find(request("id"),["details"]);
  1186. if ($model->operation){
  1187. app('OwnerPriceExpressService')->update(["id"=>request("id")],$obj);
  1188. $delete = [];//需要删除子项
  1189. $update = [["id","province_id","initial_weight_price","additional_weight_price"]];//需要更新子项
  1190. $insert = [];//需要新增子项
  1191. foreach ($params["items"] as $item){
  1192. $obj = [
  1193. "province_id" => $item["province_id"],
  1194. "initial_weight_price" => $item["initial_weight_price"],
  1195. "additional_weight_price" => $item["additional_weight_price"],
  1196. ];
  1197. if (isset($item["id"])){
  1198. $obj["id"] = $item["id"];
  1199. $update[] = $obj;
  1200. $delete[] = $item["id"];
  1201. } else{
  1202. $obj["owner_price_express_id"] = $model->id;
  1203. $insert[] = $obj;
  1204. }
  1205. }
  1206. $ids = array_column($model->details->toArray(),"id");
  1207. $delete = array_diff($ids,$delete);
  1208. if ($delete)OwnerPriceExpressProvince::destroy($delete);
  1209. if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_express_provinces",$update);
  1210. if ($insert)OwnerPriceExpressProvince::query()->insert($insert);
  1211. /** @var OwnerPriceExpress $model */
  1212. $model->logistics()->sync(request("logistics"));
  1213. }else{
  1214. foreach ($params["items"] as &$item)unset($item["id"]);
  1215. $model = app('OwnerPriceExpressService')->copy($model,$obj,null,request("logistics"),$params["items"],false);
  1216. }
  1217. }else{
  1218. DB::transaction(function ()use(&$model,$params,$obj){
  1219. $model = app('OwnerPriceExpressService')->create($obj);
  1220. foreach ($params["items"] as &$item){
  1221. $item["owner_price_express_id"] = $model->id;
  1222. $item["initial_weight_price"] = json_encode($item["initial_weight_price"]);
  1223. $item["additional_weight_price"] = json_encode($item["additional_weight_price"]);
  1224. }
  1225. OwnerPriceExpressProvince::query()->insert($params["items"]);
  1226. DB::insert(DB::raw("INSERT INTO owner_price_express_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
  1227. app("OwnerService")->refreshRelevance(request("owner_id"),2);
  1228. /** @var OwnerPriceExpress $model */
  1229. $model->logistics()->syncWithoutDetaching(request("logistics"));
  1230. DB::commit();
  1231. });
  1232. }
  1233. $model->load("details");
  1234. $this->success($model);
  1235. }
  1236. public function apiStoreLogistic()
  1237. {
  1238. $this->gate("项目管理-项目-录入");
  1239. $params = request()->input();
  1240. $params["owner_id"] = [$params["owner_id"]];
  1241. $params["logistic_id"] = $params["logistics"];
  1242. $errors = $this->logisticValidator($params,request("id"))->errors()->toArray();
  1243. $exist = [];
  1244. foreach ($params["items"] as $index => $item){
  1245. $key = $item["unit_id"]."-".$item["range"]."-".$item["province_id"]."-".$item["city_id"];
  1246. if (isset($exist[$key]))$errors["items.".$index.".unit_id"] = ["该条已存在"];
  1247. else $exist[$key] = true;
  1248. }
  1249. if (count($errors)>0)$this->success(["errors"=>$errors]);
  1250. $obj = [
  1251. "name" => request('name'),
  1252. "unit_range" => request('unit_range'),
  1253. "unit_id" => request('unit_id'),
  1254. "other_unit_range" => request('other_unit_range'),
  1255. "other_unit_id" => request('other_unit_id'),
  1256. "pick_up_price" => request('pick_up_price'),
  1257. "fuel_price" => request('fuel_price'),
  1258. "service_price" => request('service_price'),
  1259. ];
  1260. if (request("id")){
  1261. $model = app("OwnerPriceLogisticService")->find(request("id"),["details"]);
  1262. if ($model->operation){
  1263. app("OwnerPriceLogisticService")->update(["id"=>request("id")],$obj);
  1264. $delete = [];//需要删除子项
  1265. $update = [["id","unit_id","range","province_id","city_id","unit_price","delivery_fee","initial_fee","initial_amount","rate"]];//需要更新子项
  1266. $insert = [];//需要新增子项
  1267. foreach ($params["items"] as $item){
  1268. $obj = [
  1269. "unit_id" => $item["unit_id"],
  1270. "range" => $item["range"],
  1271. "province_id" => $item["province_id"],
  1272. "city_id" => $item["city_id"],
  1273. "unit_price" => $item["unit_price"],
  1274. "delivery_fee" => $item["delivery_fee"],
  1275. "initial_fee" => $item["initial_fee"],
  1276. "initial_amount" => $item["initial_amount"],
  1277. "rate" => $item["rate"],
  1278. ];
  1279. if (isset($item["id"])){
  1280. $obj["id"] = $item["id"];
  1281. $update[] = $obj;
  1282. $delete[] = $item["id"];
  1283. } else{
  1284. $obj["owner_price_logistic_id"] = $model->id;
  1285. $insert[] = $obj;
  1286. }
  1287. }
  1288. $ids = array_column($model->details->toArray(),"id");
  1289. $delete = array_diff($ids,$delete);
  1290. if ($delete)OwnerPriceLogisticDetail::destroy($delete);
  1291. if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_logistic_details",$update);
  1292. if ($insert)OwnerPriceLogisticDetail::query()->insert($insert);
  1293. /** @var OwnerPriceLogistic $model */
  1294. $model->logistics()->sync(request("logistics"));
  1295. }else{
  1296. foreach ($params["items"] as &$item)unset($item["id"]);
  1297. $model = app("OwnerPriceLogisticService")->copy($model,$obj,null,request("logistics"),$params["items"],false);
  1298. }
  1299. }else{
  1300. DB::transaction(function ()use(&$model,$params,$obj){
  1301. $model = app("OwnerPriceLogisticService")->create($obj);
  1302. foreach ($params["items"] as &$param)$param["owner_price_logistic_id"] = $model->id;
  1303. OwnerPriceLogisticDetail::query()->insert($params["items"]);
  1304. DB::insert(DB::raw("INSERT INTO owner_price_logistic_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
  1305. app("OwnerService")->refreshRelevance(request("owner_id"),3);
  1306. /** @var OwnerPriceLogistic $model */
  1307. $model->logistics()->syncWithoutDetaching(request("logistics"));
  1308. DB::commit();
  1309. });
  1310. }
  1311. $model->load("details");
  1312. $this->success($model);
  1313. }
  1314. public function apiStoreDirectLogistic()
  1315. {
  1316. $this->gate("项目管理-项目-录入");
  1317. $errors = $this->directLogisticValidator(request()->input(),request("id"))->errors()->toArray();
  1318. $exist = [];
  1319. foreach (request("items") as $index=>$item){
  1320. if (isset($exist[$item['car_type_id']]))$errors["items.".$index.".car_type_id"] = ["已存在"];
  1321. else $exist[$item['car_type_id']] = true;
  1322. }
  1323. if (count($errors)>0)$this->success(["errors"=>$errors]);
  1324. $items = request()->input("items");
  1325. $obj = [
  1326. "name" => request("name"),
  1327. "base_km" => request("base_km"),
  1328. ];
  1329. if (request("id")){
  1330. $model = app("OwnerPriceDirectLogisticService")->find(request("id"),["details"]);
  1331. if ($model->operation){
  1332. app("OwnerPriceDirectLogisticService")->update(["id"=>request("id")],$obj);
  1333. $delete = [];//需要删除子项
  1334. $update = [["id","car_type_id","base_fee","additional_fee"]];//需要更新子项
  1335. $insert = [];//需要新增子项
  1336. foreach ($items as $item){
  1337. $obj = [
  1338. "car_type_id" => $item["car_type_id"],
  1339. "base_fee" => $item["base_fee"],
  1340. "additional_fee" => $item["additional_fee"],
  1341. ];
  1342. if (isset($item["id"])){
  1343. $obj["id"] = $item["id"];
  1344. $update[] = $obj;
  1345. $delete[] = $item["id"];
  1346. } else{
  1347. $obj["owner_price_direct_logistic_id"] = $model->id;
  1348. $insert[] = $obj;
  1349. }
  1350. }
  1351. $ids = array_column($model->details->toArray(),"id");
  1352. $delete = array_diff($ids,$delete);
  1353. if ($delete)OwnerPriceDirectLogisticCar::destroy($delete);
  1354. if (count($update) > 1)app(BatchUpdateService::class)->batchUpdate("owner_price_direct_logistic_cars",$update);
  1355. if ($insert)OwnerPriceDirectLogisticCar::query()->insert($insert);
  1356. }else{
  1357. foreach ($items as &$item)unset($item["id"]);
  1358. $model = app("OwnerPriceDirectLogisticService")->copy($model,$obj,null,$items,false);
  1359. }
  1360. }else{
  1361. DB::transaction(function ()use(&$model,$items,$obj){
  1362. $model = app("OwnerPriceDirectLogisticService")->create($obj);
  1363. foreach ($items as &$item)$item["owner_price_direct_logistic_id"] = $model->id;
  1364. OwnerPriceDirectLogisticCar::query()->insert($items);
  1365. DB::insert(DB::raw("INSERT INTO owner_price_direct_logistic_owner VALUES(?,?)"),[$model->id,request("owner_id")]);
  1366. app("OwnerService")->refreshRelevance(request("owner_id"),4);
  1367. DB::commit();
  1368. });
  1369. }
  1370. /** @var OwnerPriceDirectLogistic $model */
  1371. $model->load("details");
  1372. $this->success($model);
  1373. }
  1374. public function getPriceModel()
  1375. {
  1376. /** @var Owner|\stdClass $owner */
  1377. $owner = new Owner();
  1378. $owner->id = request("id");
  1379. $owner->load(["ownerStoragePriceModels","ownerPriceOperations"=>function($query){
  1380. /** @var Builder $query */
  1381. $query->with(["items"=>function($query){
  1382. /** @var Builder $query */
  1383. $query->orderByRaw("CASE strategy WHEN '起步' THEN 1 WHEN '默认' THEN 2 WHEN '特征' THEN 3 END");
  1384. }]);
  1385. },"ownerPriceExpresses"=>function($query){
  1386. /** @var Builder $query */
  1387. $query->with(["details","logistics"]);
  1388. },"ownerPriceLogistics"=>function($query){
  1389. /** @var Builder $query */
  1390. $query->with(["details","logistics"]);
  1391. },"ownerPriceDirectLogistics.details"]);
  1392. $owner->loadCount(["storageAudit","operationAudit","expressAudit","logisticAudit","directLogisticAudit"]);
  1393. $features = app("FeatureService")->getMapArray();
  1394. OwnerPriceOperation::$features = $features;
  1395. OwnerPriceOperationItem::$features = $features;
  1396. foreach ($owner->ownerPriceOperations as &$operation){
  1397. $operation["featureFormat"] = $operation->featureFormat;
  1398. $operation["isRejected"] = $operation->type_mark === 0 ? true : false;
  1399. foreach ($operation->items as &$item){
  1400. $item["featureFormat"] = $item->featureFormat;
  1401. if ($item["strategy"] == "起步")$item["type"] = $item["amount"] ? 0 : 1;
  1402. }
  1403. }
  1404. $this->success($owner);
  1405. }
  1406. public function apiGetStorage()
  1407. {
  1408. $this->gate("项目管理-项目-录入");
  1409. $models = OwnerStoragePriceModel::query()->where(function (Builder $query){
  1410. $query->whereNull("operation")->orWhere("operation","");
  1411. });
  1412. if (request("customer_id")){
  1413. $customerId = request("customer_id");
  1414. $models->whereHas("owners",function ($query)use($customerId){
  1415. /** @var Builder $query */
  1416. $query->where("customer_id",$customerId);
  1417. });
  1418. }
  1419. if (request("owner_id")){
  1420. $ownerId = request("owner_id");
  1421. $models->whereHas("owners",function ($query)use($ownerId){
  1422. /** @var Builder $query */
  1423. $query->where("id",$ownerId);
  1424. });
  1425. }
  1426. if (request("name")){
  1427. $models->where("name","like","%".request("name")."%");
  1428. }
  1429. $this->success($models->get());
  1430. }
  1431. public function apiGetOperation()
  1432. {
  1433. $this->gate("项目管理-项目-录入");
  1434. $models = OwnerPriceOperation::query()->with("items")->where(function (Builder $query){
  1435. $query->whereNull("operation")->orWhere("operation","");
  1436. });
  1437. if (request("customer_id")){
  1438. $customerId = request("customer_id");
  1439. $models->whereHas("owners",function ($query)use($customerId){
  1440. /** @var Builder $query */
  1441. $query->where("customer_id",$customerId);
  1442. });
  1443. }
  1444. if (request("owner_id")){
  1445. $ownerId = request("owner_id");
  1446. $models->whereHas("owners",function ($query)use($ownerId){
  1447. /** @var Builder $query */
  1448. $query->where("id",$ownerId);
  1449. });
  1450. }
  1451. if (request("name")){
  1452. $models->where("name","like","%".request("name")."%");
  1453. }
  1454. $features = app("FeatureService")->getMapArray();
  1455. OwnerPriceOperation::$features = $features;
  1456. OwnerPriceOperationItem::$features = $features;
  1457. $models = $models->get();
  1458. foreach ($models as &$operation){
  1459. $operation["featureFormat"] = $operation->featureFormat;
  1460. $operation["isRejected"] = $operation->type_mark === 0 ? true : false;
  1461. $items = [];
  1462. foreach ($operation->items as $item){
  1463. $items[] = [
  1464. "strategy" => $item["strategy"],
  1465. "amount" => $item["amount"],
  1466. "unit_id" => $item["unit_id"],
  1467. "unit_price" => $item["unit_price"],
  1468. "discount_price" => $item["discount_price"] ?? null,
  1469. "feature" => $item["feature"],
  1470. "featureFormat" => $item->featureFormat,
  1471. ];
  1472. }
  1473. $operation["items"] = $items;
  1474. }
  1475. $this->success($models);
  1476. }
  1477. public function apiGetExpress()
  1478. {
  1479. $this->gate("项目管理-项目-录入");
  1480. $models = OwnerPriceExpress::query()->with(["details","logistics:id"])->where(function (Builder $query){
  1481. $query->whereNull("operation")->orWhere("operation","");
  1482. });
  1483. if (request("customer_id")){
  1484. $customerId = request("customer_id");
  1485. $models->whereHas("owners",function ($query)use($customerId){
  1486. /** @var Builder $query */
  1487. $query->where("customer_id",$customerId);
  1488. });
  1489. }
  1490. if (request("owner_id")){
  1491. $ownerId = request("owner_id");
  1492. $models->whereHas("owners",function ($query)use($ownerId){
  1493. /** @var Builder $query */
  1494. $query->where("id",$ownerId);
  1495. });
  1496. }
  1497. if (request("logistic_id")){
  1498. $logisticId = request("logistic_id");
  1499. $models->whereHas("logistics",function ($query)use($logisticId){
  1500. /** @var Builder $query */
  1501. $query->where("id",$logisticId);
  1502. });
  1503. }
  1504. if (request("name")){
  1505. $models->where("name","like","%".request("name")."%");
  1506. }
  1507. $this->success($models->get());
  1508. }
  1509. public function apiGetLogistic()
  1510. {
  1511. $this->gate("项目管理-项目-录入");
  1512. $models = OwnerPriceLogistic::query()->with(["details","logistics"])->where(function (Builder $query){
  1513. $query->whereNull("operation")->orWhere("operation","");
  1514. });
  1515. if (request("customer_id")){
  1516. $customerId = request("customer_id");
  1517. $models->whereHas("owners",function ($query)use($customerId){
  1518. /** @var Builder $query */
  1519. $query->where("customer_id",$customerId);
  1520. });
  1521. }
  1522. if (request("owner_id")){
  1523. $ownerId = request("owner_id");
  1524. $models->whereHas("owners",function ($query)use($ownerId){
  1525. /** @var Builder $query */
  1526. $query->where("id",$ownerId);
  1527. });
  1528. }
  1529. if (request("logistic_id")){
  1530. $logisticId = request("logistic_id");
  1531. $models->whereHas("logistics",function ($query)use($logisticId){
  1532. /** @var Builder $query */
  1533. $query->where("id",$logisticId);
  1534. });
  1535. }
  1536. if (request("name")){
  1537. $models->where("name","like","%".request("name")."%");
  1538. }
  1539. $this->success($models->get());
  1540. }
  1541. public function apiGetDirectLogistic()
  1542. {
  1543. $this->gate("项目管理-项目-录入");
  1544. $models = OwnerPriceDirectLogistic::query()->with("details")->where(function (Builder $query){
  1545. $query->whereNull("operation")->orWhere("operation","");
  1546. });
  1547. if (request("customer_id")){
  1548. $customerId = request("customer_id");
  1549. $models->whereHas("owners",function ($query)use($customerId){
  1550. /** @var Builder $query */
  1551. $query->where("customer_id",$customerId);
  1552. });
  1553. }
  1554. if (request("owner_id")){
  1555. $ownerId = request("owner_id");
  1556. $models->whereHas("owners",function ($query)use($ownerId){
  1557. /** @var Builder $query */
  1558. $query->where("id",$ownerId);
  1559. });
  1560. }
  1561. if (request("name")){
  1562. $models->where("name","like","%".request("name")."%");
  1563. }
  1564. $this->success($models->get());
  1565. }
  1566. public function apiDelStorage()
  1567. {
  1568. $this->publicDeleteNode("OwnerStoragePriceModelService");
  1569. }
  1570. public function apiDelOperation()
  1571. {
  1572. $this->publicDeleteNode("OwnerPriceOperationService");
  1573. }
  1574. public function apiDelOperationItem()
  1575. {
  1576. $this->gate("项目管理-项目-录入");
  1577. if (!request("id"))$this->error("非法参数");
  1578. /** @var \stdClass $item */
  1579. $item = OwnerPriceOperationItem::query()->with("ownerPriceOperation")->find(request("id"));
  1580. if ($item->ownerPriceOperation->operation){
  1581. app("OwnerPriceOperationItemService")->destroy(request("id"));
  1582. $this->success();
  1583. }else{
  1584. /** @var OwnerPriceOperation $model */
  1585. $model = app("OwnerPriceOperationService")->copy($item->ownerPriceOperation);
  1586. OwnerPriceOperationItem::query()->where("owner_price_operation_id",$model->id)
  1587. ->where("strategy",$item->strategy)
  1588. ->where("amount",$item->amount)
  1589. ->where("unit_id",$item->unit_id)
  1590. ->where("unit_price",$item->unit_price)
  1591. ->where("feature",$item->feature)
  1592. ->where("priority",$item->priority)
  1593. ->where("discount_price",$item->discount_price)->delete();
  1594. $model->load(["items"=>function($query){
  1595. /** @var Builder $query */
  1596. $query->orderByRaw("CASE strategy WHEN '起步' THEN 1 WHEN '默认' THEN 2 WHEN '特征' THEN 3 END");
  1597. }]);
  1598. $this->success($model);
  1599. }
  1600. }
  1601. public function apiDelExpress()
  1602. {
  1603. $this->publicDeleteNode("OwnerPriceExpressService");
  1604. }
  1605. public function apiDelExpressItem()
  1606. {
  1607. $this->gate("项目管理-项目-录入");
  1608. if (!request("id"))$this->error("非法参数");
  1609. /** @var \stdClass $item */
  1610. $item = OwnerPriceExpressProvince::query()->with("ownerPriceExpress")->find(request("id"));
  1611. if ($item->ownerPriceExpress->operation){
  1612. app("OwnerPriceExpressService")->destroyDetail(request("id"));
  1613. }else{
  1614. $model = app("OwnerPriceExpressService")->copy($item->ownerPriceExpress);
  1615. OwnerPriceExpressProvince::query()->where("owner_price_express_id",$model->id)
  1616. ->where("province_id",$item->province_id)->delete();
  1617. }
  1618. $this->success();
  1619. }
  1620. public function apiDelLogistic()
  1621. {
  1622. $this->publicDeleteNode("OwnerPriceLogisticService");
  1623. }
  1624. public function apiDelLogisticItem()
  1625. {
  1626. $this->gate("项目管理-项目-录入");
  1627. if (!request("id"))$this->error("非法参数");
  1628. /** @var \stdClass $item */
  1629. $item = OwnerPriceLogisticDetail::query()->with("ownerPriceLogistic")->find(request("id"));
  1630. if ($item->ownerPriceLogistic->operation){
  1631. app("OwnerPriceLogisticService")->destroyDetail(request("id"));
  1632. }else{
  1633. $model = app("OwnerPriceLogisticService")->copy($item->ownerPriceLogistic);
  1634. OwnerPriceLogisticDetail::query()->where("owner_price_logistic_id",$model->id)
  1635. ->where("unit_id",$item->unit_id)->where("range",$item->range)
  1636. ->where("province_id",$item->province_id)
  1637. ->where("city_id",$item->city_id)->delete();
  1638. }
  1639. $this->success();
  1640. }
  1641. public function apiDelDirectLogistic()
  1642. {
  1643. $this->publicDeleteNode("OwnerPriceDirectLogisticService");
  1644. }
  1645. public function apiDelDirectLogisticItem()
  1646. {
  1647. $this->gate("项目管理-项目-录入");
  1648. if (!request("id"))$this->error("非法参数");
  1649. /** @var \stdClass $item */
  1650. $item = OwnerPriceDirectLogisticCar::query()->with("ownerPriceDirectLogistic")->find(request("id"));
  1651. if ($item->ownerPriceDirectLogistic->operation){
  1652. app("OwnerPriceDirectLogisticService")->destroyDetail(request("id"));
  1653. }else{
  1654. $model = app("OwnerPriceDirectLogisticService")->copy($item->ownerPriceDirectLogistic);
  1655. OwnerPriceDirectLogisticCar::query()->where("owner_price_direct_logistic_id",$model->id)
  1656. ->where("car_type_id",$item->car_type_id)->delete();
  1657. }
  1658. $this->success();
  1659. }
  1660. private function publicDeleteNode(string $serviceName)
  1661. {
  1662. $this->gate("项目管理-项目-录入");
  1663. $id = request("id");
  1664. if (!$id)$this->error("非法参数");
  1665. $model = app($serviceName)->find($id);
  1666. if ($model->operation && $model->target_id){
  1667. app($serviceName)->destroy($id);
  1668. $id = $model->target_id;
  1669. }
  1670. app($serviceName)->update(["id"=>$id],["operation"=>"D"]);
  1671. $this->success();
  1672. }
  1673. //审核或恢复计费模型
  1674. public function auditOrRecoverModel()
  1675. {
  1676. $this->gate("项目管理-项目-计费模型-审核");
  1677. $ownerId = request("owner_id");
  1678. if (!$ownerId)$this->error("非法参数");
  1679. /** @var \stdClass|Owner $owner */
  1680. $owner = new Owner();
  1681. $owner->id = $ownerId;
  1682. switch (request("type")){
  1683. case "storage":
  1684. app("OwnerStoragePriceModelService")->auditOrRecover(request("isAudit"),$ownerId);//priceModelAuditOrRecoverQuery
  1685. $owner->load("ownerStoragePriceModels");
  1686. $this->success($owner->ownerStoragePriceModels);
  1687. break;
  1688. case "operation":
  1689. app("OwnerPriceOperationService")->auditOrRecover(request("isAudit"),$ownerId);
  1690. $owner->load(["ownerPriceOperations"=>function($query){
  1691. /** @var Builder $query */
  1692. $query->with(["items"=>function($query){
  1693. /** @var Builder $query */
  1694. $query->orderByRaw("CASE strategy WHEN '起步' THEN 1 WHEN '默认' THEN 2 WHEN '特征' THEN 3 END");
  1695. }]);
  1696. }]);
  1697. $features = app("FeatureService")->getMapArray();
  1698. OwnerPriceOperation::$features = $features;
  1699. OwnerPriceOperationItem::$features = $features;
  1700. foreach ($owner->ownerPriceOperations as &$operation){
  1701. $operation["featureFormat"] = $operation->featureFormat;
  1702. foreach ($operation->items as &$item){
  1703. $item["featureFormat"] = $item->featureFormat;
  1704. if ($operation["operation_type"]==='出库' && $item["strategy"] == "起步"){
  1705. $item["type"] = $item["amount"] ? 0 : 1;
  1706. }
  1707. }
  1708. }
  1709. $this->success($owner->ownerPriceOperations);
  1710. break;
  1711. case "express":
  1712. app("OwnerPriceExpressService")->auditOrRecover(request("isAudit"),$ownerId);
  1713. $owner->load(["ownerPriceExpresses"=>function($query){
  1714. /** @var Builder $query */
  1715. $query->with(["details","logistics"]);
  1716. }]);
  1717. $this->success($owner->ownerPriceExpresses);
  1718. break;
  1719. case "logistic":
  1720. app("OwnerPriceLogisticService")->auditOrRecover(request("isAudit"),$ownerId);
  1721. $owner->load(["ownerPriceLogistics"=>function($query){
  1722. /** @var Builder $query */
  1723. $query->with(["details","logistics"]);
  1724. }]);
  1725. $this->success($owner->ownerPriceLogistics);
  1726. break;
  1727. case "directLogistic":
  1728. app("OwnerPriceDirectLogisticService")->auditOrRecover(request("isAudit"),$ownerId);
  1729. $owner->load("ownerPriceDirectLogistics.details");
  1730. $this->success($owner->ownerPriceDirectLogistics);
  1731. break;
  1732. }
  1733. }
  1734. }