OwnerPriceExpressService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Services;
  3. use App\Logistic;
  4. use App\Owner;
  5. use App\OwnerPriceExpress;
  6. use App\OwnerPriceExpressProvince;
  7. use App\Services\common\QueryService;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Support\Facades\DB;
  10. use App\Traits\ServiceAppAop;
  11. class OwnerPriceExpressService
  12. {
  13. use ServiceAppAop;
  14. protected $modelClass=OwnerPriceExpress::class;
  15. public function paginate($id = null)
  16. {
  17. $query = OwnerPriceExpress::query()->with(["owners","logistics"])
  18. ->orderByDesc("id");
  19. if ($id)$query->where("id",$id);
  20. return $query->paginate(50);
  21. }
  22. public function find($id, $withs = [])
  23. {
  24. return OwnerPriceExpress::query()->with($withs)->find($id);
  25. }
  26. /**
  27. * 拷贝目标数据
  28. *
  29. * @param object|int $model
  30. * @param array $values
  31. * @param array|null $owners
  32. * @param array|null $logistics
  33. * @param array $items
  34. * @param bool $isLoadItem
  35. *
  36. * @return object|null
  37. */
  38. public function copy($model, $values = [], $owners = null, $logistics = null, $items = [], $isLoadItem = true)
  39. {
  40. if (is_integer($model))$model = OwnerPriceExpress::query()->find($model);
  41. if (!$model)return null;
  42. $values["operation"] = "U";
  43. $values["target_id"] = $model->id;
  44. foreach ($model->getFillable() as $column){
  45. if (!array_key_exists($column,$values))$values[$column] = $model[$column];
  46. }
  47. /** @var OwnerPriceExpress $copyModel */
  48. $copyModel = OwnerPriceExpress::query()->create($values);
  49. if ($owners===null){
  50. $query = DB::raw("SELECT * FROM owner_price_express_owner WHERE owner_price_express_id = {$model->id}");
  51. $owners = array_column(DB::select($query),"owner_id");
  52. }
  53. $copyModel->owners()->sync($owners);
  54. if ($logistics===null){
  55. $query = DB::raw("SELECT * FROM owner_price_express_logistic WHERE owner_price_express_id = {$model->id}");
  56. $logistics = array_column(DB::select($query),"logistic_id");
  57. }
  58. $copyModel->logistics()->sync($logistics);
  59. $insert = [];
  60. if ($isLoadItem){
  61. $model->loadMissing("details");
  62. /** @var \stdClass $model */
  63. foreach ($model->details as $item){
  64. $columns = ["province_id","initial_weight_price","additional_weight_price"];
  65. if ($items[$item->id] ?? false){
  66. foreach ($columns as $column){
  67. if (!array_key_exists($column,$items[$item->id]))$items[$item->id][$column] = $item[$column];
  68. }
  69. $obj = $items[$item->id];
  70. unset($items[$item->id]);
  71. }else{
  72. /** @var OwnerPriceExpressProvince $item */
  73. $obj = $item->toArray();
  74. unset($obj["id"]);
  75. }
  76. $obj["owner_price_express_id"] = $copyModel->id;
  77. $insert[] = $obj;
  78. }
  79. }else{
  80. foreach ($items as $item){
  81. $item["owner_price_express_id"] = $copyModel->id;
  82. $insert[] = $item;
  83. }
  84. }
  85. if ($insert)OwnerPriceExpressProvince::query()->insert($insert);
  86. return $copyModel;
  87. }
  88. /**
  89. * 审核或恢复目标集
  90. *
  91. * @param bool $isAudit
  92. * @param integer|null|array $ownerId
  93. * @param integer|null|array $ids
  94. */
  95. public function auditOrRecover($isAudit = true, $ownerId = null, $ids = null)
  96. {
  97. if (!$ownerId && !$ids)return;
  98. $result = app(QueryService::class)->priceModelAuditOrRecoverQuery($isAudit,OwnerPriceExpress::query(),$ownerId,$ids);
  99. if ($result["delete"])$this->destroy($result["delete"]);
  100. if ($result["update"])OwnerPriceExpress::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
  101. }
  102. public function updateDetail(array $params, array $values)
  103. {
  104. $query = OwnerPriceExpressProvince::query();
  105. foreach ($params as $column => $param){
  106. $query->where($column,$param);
  107. }
  108. return $query->update($values);
  109. }
  110. public function create(array $params)
  111. {
  112. $params["operation"] = "U";
  113. return OwnerPriceExpress::query()->create($params);
  114. }
  115. public function createDetail(array $params)
  116. {
  117. return OwnerPriceExpressProvince::query()->create($params);
  118. }
  119. public function isExistDetail(array $params)
  120. {
  121. $query = OwnerPriceExpressProvince::query();
  122. foreach ($params as $column => $param){
  123. $query->where($column,$param);
  124. }
  125. return $query->count();
  126. }
  127. public function destroyDetail($id)
  128. {
  129. return OwnerPriceExpressProvince::destroy($id);
  130. }
  131. public function getExistOwnerName($owner_id, $logistic_id, $id=null, $type = 'ownerPriceExpresses') :array
  132. {
  133. if (!is_array($owner_id))$owner_id = [$owner_id];
  134. if (!is_array($logistic_id))$logistic_id = [$logistic_id];
  135. $owners = Owner::query()->with([$type=>function($query)use($id){
  136. /** @var Builder $query */
  137. if ($id)$query->where("id","!=",$id);
  138. $query->with(["logistics"]);
  139. }])->whereIn("id",$owner_id)->get();
  140. $result = [];
  141. foreach ($owners as $owner){
  142. $arr = [];
  143. if ($owner->ownerPriceExpresses){
  144. foreach ($owner->ownerPriceExpresses as $express){
  145. if ($express->logistics){
  146. foreach ($express->logistics as $logistic){
  147. if (array_search($logistic->id,$logistic_id) !== false)$arr[] = $logistic->name;
  148. }
  149. }
  150. }
  151. }
  152. if (count($arr)>0)$result[] = "“".$owner->name."”已绑定:".implode(",",$arr);
  153. }
  154. return $result;
  155. }
  156. public function getExistLogisticName($logistic_id, $id=null):array
  157. {
  158. $logistics = Logistic::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
  159. if ($id)$query->where("id","!=",$id);
  160. }])->whereIn("id",$logistic_id)->get();
  161. $arr = [];
  162. foreach ($logistics as $logistic){
  163. if ($logistic->owner_price_expresses_count > 0)$arr[] = $logistic->name;
  164. }
  165. return $arr;
  166. }
  167. public function destroy($id)
  168. {
  169. if (!is_array($id))$id = [$id];
  170. OwnerPriceExpressProvince::query()->whereIn("owner_price_express_id",$id)->delete();
  171. $query = "IN (";
  172. for ($i=0;$i<count($id)-1;$i++)$query .= "{$id[$i]},";
  173. $query .= "{$id[count($id)-1]})";
  174. $sql = "SELECT * FROM owner_price_express_owner WHERE owner_price_express_id {$query}";
  175. $owners = array_column(DB::select(DB::raw($sql)),"owner_id");
  176. DB::table("owner_price_express_owner")->whereIn("owner_price_express_id",$id)->delete();
  177. app("OwnerService")->refreshRelevance($owners,2,true);
  178. DB::table("owner_price_express_logistic")->whereIn("owner_price_express_id",$id)->delete();
  179. return OwnerPriceExpress::destroy($id);
  180. }
  181. public function update(array $params, array $values)
  182. {
  183. $query = OwnerPriceExpress::query();
  184. foreach ($params as $column=>$param){
  185. $query->where($column,$params);
  186. }
  187. return $query->update($values);
  188. }
  189. /**
  190. * 获取绑定承运商
  191. *
  192. * @param $owner
  193. * @return array
  194. */
  195. public function getBuildLogistic($owner)
  196. {
  197. return app(CacheService::class)->getOrExecute("logistics_owner_".$owner,function ()use($owner){
  198. $query = DB::raw(<<<sql
  199. SELECT logistic_id FROM `owner_price_express_owner` e
  200. LEFT JOIN owner_price_express_logistic l
  201. ON e.owner_price_express_id = l.owner_price_express_id
  202. WHERE e.owner_id = ?
  203. sql
  204. );
  205. $logistics = DB::select($query,[$owner]);
  206. return array_column($logistics,"logistic_id");
  207. },null);
  208. }
  209. /**
  210. * CODE: -1:未找到计费模型 -2:重量无效
  211. *
  212. * @param double $weight
  213. * @param integer $owner_id
  214. * @param integer $logistic_id
  215. * @param integer $province_id
  216. * @return double
  217. */
  218. public function matching($weight, $owner_id, $logistic_id, $province_id)
  219. {
  220. if (!$weight)return -2;
  221. $model = OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){
  222. /** @var Builder $query */
  223. $query->where("province_id",$province_id);
  224. }])->whereHas("owners",function ($query)use($owner_id){
  225. /** @var Builder $query */
  226. $query->where("id",$owner_id);
  227. })->whereHas("logistics",function ($query)use($logistic_id){
  228. /** @var Builder $query */
  229. $query->where("id",$logistic_id);
  230. })->where(function(Builder $query){
  231. $query->whereNull("operation")->orWhere("operation","");
  232. })->first();
  233. if (!$model || count($model->details)<1)return -1;
  234. if ($weight <= $model->initial_weight)return $model->details[0]->initial_weight_price;
  235. $weight -= $model->initial_weight;
  236. return (ceil($weight/$model->additional_weight)*$model->details[0]->additional_weight_price)+$model->details[0]->initial_weight_price;
  237. }
  238. }