PriceModelController.php 90 KB

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