PriceModelController.php 87 KB

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