OwnerPriceOperationService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerInStorageRule;
  4. use App\OwnerOutStorageRule;
  5. use App\OwnerPriceOperation;
  6. use App\Services\common\QueryService;
  7. use App\Unit;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Support\Facades\DB;
  11. Class OwnerPriceOperationService
  12. {
  13. /**
  14. * @param Builder $builder
  15. * @param array $params
  16. * @return Builder
  17. */
  18. private function query(Builder $builder, array $params)
  19. {
  20. if ($params["owner_id"] ?? false){
  21. $ids = $this->ownerGetIds($params["owner_id"]);
  22. if ($ids)$builder->whereIn("id",$ids);
  23. unset($params["owner_id"]);
  24. }
  25. $columnQueryRules = [
  26. "name" => ["like"=>""]
  27. ];
  28. return app(QueryService::class)->query($params, $builder, $columnQueryRules);
  29. }
  30. public function paginate(array $params, array $withs = [])
  31. {
  32. return $this->query(OwnerPriceOperation::query()->orderByDesc('id')->with($withs),$params)
  33. ->paginate($params["paginate"] ?? 50);
  34. }
  35. private function ownerGetIds(string $owner_id)
  36. {
  37. if (!$owner_id)return [];
  38. $arr = DB::select(DB::raw("SELECT owner_price_operation_id AS id FROM owner_price_operation_owner WHERE owner_id in (".$owner_id.")"));
  39. return array_column($arr,"id");
  40. }
  41. public function destroy($id)
  42. {
  43. OwnerOutStorageRule::query()->where("owner_price_operation_id",$id)->delete();
  44. OwnerInStorageRule::query()->where("owner_price_operation_id",$id)->delete();
  45. DB::table("owner_price_operation_owner")->where("owner_price_operation_id",$id)->delete();
  46. return OwnerPriceOperation::destroy($id);
  47. }
  48. /**
  49. * @param array $params
  50. * @return Model
  51. */
  52. public function create(array $params)
  53. {
  54. return OwnerPriceOperation::query()->create($params);
  55. }
  56. public function insertRule(array $params, $type = '出库')
  57. {
  58. if ($type == '出库')OwnerOutStorageRule::query()->insert($params);
  59. else OwnerInStorageRule::query()->insert($params);
  60. }
  61. public function find($id, $isGetRule = false, $withs = [])
  62. {
  63. $query = OwnerPriceOperation::query()->with($withs)->find($id);
  64. if ($isGetRule){
  65. if ($query->operation_type == '入库'){
  66. $query->load("ownerInStorageRule");
  67. $query->rules = $query->ownerInStorageRule ? json_encode([$query->ownerInStorageRule]) : null;
  68. }else{
  69. $query->load("ownerOutStorageRules");
  70. $query->rules = json_encode($query->ownerOutStorageRules);
  71. }
  72. }
  73. return $query;
  74. }
  75. public function destroyRule($id, $type)
  76. {
  77. if ($type=="入库")OwnerInStorageRule::query()->where("owner_price_operation_id",$id)->delete();
  78. else OwnerOutStorageRule::query()->where("owner_price_operation_id",$id)->delete();
  79. }
  80. public function findUpdate(OwnerPriceOperation $model, array $params)
  81. {
  82. return $model->update($params);
  83. }
  84. /** 参数顺序: 数量 匹配对象 列映射 货主ID 单位ID 类型 SKU .
  85. * 匹配顺序: 类型 货主 策略 单位 特征 ..多对多匹配规则废弃,1对1,设单位必定为件,对应规则必然只有一项存在
  86. * 单位匹配: 件,箱,单 由小到大,依次换算匹配 .
  87. * @param int $amount
  88. * @param array|object $matchObject key-val
  89. * @param array $columnMapping key-val
  90. * @param string $owner_id
  91. * @param string $type
  92. * @param string $sku
  93. * @return double
  94. * 错误代码: -1:非法数量 -2:无计费模型 -3:未知单位 -4:sku为空 -5:货主未找到 -6:无箱规 -7:未匹配到计费模型
  95. */
  96. public function matchRule($amount, $matchObject, $columnMapping, $owner_id, $sku = null, $type = '出库')
  97. {
  98. if ($amount <= 0 )return -1;
  99. $unitModels = Unit::query()->whereIn("name",["件","箱","单"])->get();
  100. $units = [];
  101. foreach ($unitModels as $unitModel)$units[$unitModel->id] = $unitModel->name;
  102. $withs = $type=='出库' ? ['ownerOutStorageRules'=>function($query){
  103. /** @var Builder $query */
  104. $query->orderByRaw("CASE strategy WHEN '默认' THEN 1 WHEN '特征' THEN 2 WHEN '起步' THEN 3 END DESC,priority DESC");
  105. }] : ['ownerInStorageRule'] ;
  106. $rules = OwnerPriceOperation::query()->with($withs)
  107. ->where("operation_type","出库")
  108. ->whereHas("ownerPriceOperationOwners",function ($query)use($owner_id){
  109. /** @var Builder $query */
  110. $query->where("id",$owner_id);
  111. })
  112. ->orderByRaw("strategy desc,priority desc")->get(); //货主下的全部规则
  113. if (!$rules)return -2;
  114. if ($type == '入库'){
  115. foreach ($rules as $rule){
  116. if (!$rule->ownerInStorageRule)continue;
  117. if ($rule->strategy == '特征'){
  118. $bool = app("FeatureService")->matchFeature($rule->feature,$columnMapping,$matchObject);
  119. if ($bool === true){
  120. if (!isset($units[$rule->ownerInStorageRule->unit_id])) return -3;
  121. if ($units[$rule->ownerInStorageRule->unit_id] == '箱'){ //为箱时同步商品寻找箱规
  122. $amount = $this->changeUnit($amount,$owner_id,$sku);
  123. if ($amount<0) return $amount;
  124. }
  125. if ($units[$rule->ownerInStorageRule->unit_id] == '单')$amount = 1; //为单时数量设为1;
  126. return ceil($amount/$rule->ownerInStorageRule->amount)*$rule->ownerInStorageRule->unit_price;
  127. };
  128. }else{
  129. if (!isset($units[$rule->ownerInStorageRule->unit_id])) return -3;
  130. if ($units[$rule->ownerInStorageRule->unit_id] == '箱'){ //为箱时同步商品寻找箱规
  131. $amount = $this->changeUnit($amount,$owner_id,$sku);
  132. if ($amount<0) return $amount;
  133. }
  134. if ($units[$rule->ownerInStorageRule->unit_id] == '单')$amount = 1; //为单时数量设为1;
  135. return ceil($amount/$rule->ownerInStorageRule->amount)*$rule->ownerInStorageRule->unit_price;
  136. };
  137. }
  138. return -7;
  139. }
  140. //出库
  141. foreach ($rules as $rule){
  142. if (!$rule->ownerOutStorageRules)continue;
  143. if ($rule->strategy == '特征'){
  144. $bool = app("FeatureService")->matchFeature($rule->feature,$columnMapping,$matchObject);//匹配特征
  145. if ($bool === true){
  146. $money = $this->matchOutStorage($amount,$rule->ownerOutStorageRules,$columnMapping,$matchObject,$units,$owner_id,$sku);
  147. if ($money>0)return $money;
  148. };
  149. }else{
  150. $money = $this->matchOutStorage($amount,$rule->ownerOutStorageRules,$columnMapping,$matchObject,$units,$owner_id,$sku);
  151. if ($money>0)return $money;
  152. };
  153. }
  154. return -7;
  155. }
  156. private function changeUnit($amount,$owner_id,$sku)
  157. {
  158. if (!$sku)return -4;
  159. $pack = app("CommodityService")->getPack($owner_id,$sku);
  160. if (!$pack)return -6;
  161. return ceil($amount/$pack);
  162. }
  163. private function matchOutStorage($amount, $rules, $columnMapping, $matchObject, $units, $owner_id, $sku)
  164. {
  165. $money = 0;
  166. foreach ($rules as $rule){
  167. switch ($rule->strategy){
  168. case "起步":
  169. $money = $rule->amount * $rule->unit_price;
  170. if ($units[$rule->unit_id] == '箱') { //为箱时同步商品寻找箱规
  171. if (!$sku)return -4;
  172. $pack = app("CommodityService")->getPack($owner_id,$sku);
  173. if (!$pack)return -6;
  174. $rule->amount *= $pack;
  175. }
  176. if ($amount < $rule->amount)$amount = $rule->amount;
  177. else $amount -= $rule->amount;
  178. break;
  179. case "特征":
  180. if (app("FeatureService")->matchFeature($rule->feature,$columnMapping,$matchObject)){
  181. if (!isset($units[$rule->unit_id]) || $units[$rule->unit_id] == '单') return -3;
  182. if ($units[$rule->unit_id] == '箱'){ //为箱时同步商品寻找箱规
  183. $amount = $this->changeUnit($amount,$owner_id,$sku);
  184. }
  185. return (ceil($amount/$rule->amount)*$rule->unit_price)+$money;
  186. };
  187. break;
  188. case "默认":
  189. if (!isset($units[$rule->unit_id]) || $units[$rule->unit_id] == '单') return -3;
  190. if ($units[$rule->unit_id] == '箱'){ //为箱时同步商品寻找箱规
  191. $amount = $this->changeUnit($amount,$owner_id,$sku);
  192. }
  193. return (ceil($amount/$rule->amount)*$rule->unit_price)+$money;
  194. break;
  195. }
  196. }
  197. return -7;
  198. }
  199. private function matchInStorage($amount, $matchObject, $columnMapping, $owner_id, $unit_id, $rules, $sku, $units = null, $isMatch = false)
  200. {
  201. /*foreach ($rules as $rule){
  202. if ($unit_id != $rule->ownerInStorageRule->unit_id)continue;
  203. else{
  204. if ($rule->strategy == '特征'){
  205. $bool = app("FeatureService")->matchFeature($rule->feature,$columnMapping,$matchObject);
  206. if ($bool === true){
  207. return ceil($amount/$rule->ownerInStorageRule->amount)*$rule->ownerInStorageRule->unit_price;
  208. };
  209. }else{
  210. return ceil($amount/$rule->ownerInStorageRule->amount)*$rule->ownerInStorageRule->unit_price;
  211. };
  212. }
  213. }
  214. //单位换算
  215. if (!$units){
  216. $unitModels = Unit::query()->whereIn("name",["件","箱","单"])->get();
  217. if (!$unitModels) return null;
  218. foreach ($unitModels as $unitModel)$units[$unitModel->name] = $unitModel->id;
  219. }
  220. $name = array_search($unit_id,$units);
  221. //递归匹配
  222. switch ($name){
  223. case "件"://布尔值校验是否匹配过件来确保箱只匹配一次件
  224. return $this->matchInStorage($amount, $matchObject, $columnMapping, $owner_id, $units["箱"], $rules, $sku, $units, true);
  225. case "箱"://箱存在向下向上转换
  226. if ($isMatch){
  227. return $this->matchInStorage($amount, $matchObject, $columnMapping, $owner_id, $units["单"], $rules, $sku, $units);
  228. }else{
  229. return $this->matchInStorage($amount, $matchObject, $columnMapping, $owner_id, $units["件"], $rules, $sku, $units);
  230. }
  231. case "单"://三次匹配皆无,返回null
  232. return null;
  233. }*/
  234. }
  235. }