PriceModelController.php 87 KB

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