OwnerOutStorageRule.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class OwnerOutStorageRule extends Model
  5. {
  6. protected $fillable = [
  7. "owner_price_operation_id", //作业计费ID
  8. "strategy", //出库策略
  9. "amount", //起步数
  10. "unit_id", //单位ID
  11. "unit_price", //单价
  12. "feature", //特征
  13. "priority", //优先级 值越大越高
  14. ];
  15. public $timestamps=false;
  16. public static $features = null;
  17. public static $columnMapping = null;
  18. public function unit()
  19. { //单位
  20. return $this->hasOne(Unit::class,"id","unit_id");
  21. }
  22. public function translateFeature(array $features)
  23. {
  24. $result = [];
  25. preg_match_all('/\d+|[\&\|\(\)]/',$this->feature,$result);
  26. foreach ($result[0] as &$str){
  27. if (is_numeric($str) && isset($features[$str]))$str = $features[$str]["type"].' '.$features[$str]["logic"].' '.$features[$str]["describe"];
  28. }
  29. $this->feature = implode("",$result[0]);
  30. }
  31. public function getFeatureAttribute($value)
  32. {
  33. if ($this->strategy == '默认')return "/";
  34. return $value;
  35. }
  36. /* 格式化依据静态参数,在调用前设置 */
  37. public function getFeatureFormatAttribute()
  38. {
  39. $value = $this->feature;
  40. if ($this->strategy == '默认')return "/";
  41. if (!self::$features)return $value;
  42. $result = [];
  43. preg_match_all('/\d+|[\&\|\(\)]/',$value,$result);
  44. foreach ($result[0] as &$str){
  45. if (is_numeric($str) && isset(self::$features[$str])){
  46. $column = self::$features[$str]["type"];
  47. $logic = self::$features[$str]["logic"];
  48. $describe = self::$features[$str]["describe"];
  49. if (self::$columnMapping){
  50. $column = self::$columnMapping[$column] ?? $column;
  51. switch ($logic){
  52. case "包含":
  53. $logic = " like ";
  54. $describe = "%".$describe."%";
  55. break;
  56. case "不包含":
  57. $logic = " not like ";
  58. $describe = "%".$describe."%";
  59. break;
  60. case "等于":
  61. $logic = " = ";
  62. break;
  63. }
  64. $str = $column.$logic.$describe;
  65. }else $str = self::$features[$str]["type"].' '.self::$features[$str]["logic"].' '.self::$features[$str]["describe"];
  66. }
  67. if ($str == "&"){
  68. if (self::$columnMapping) $str = " and ";
  69. else $str = " 并且 ";
  70. }
  71. if ($str == "|"){
  72. if (self::$columnMapping) $str = " or ";
  73. else $str = " 或 ";
  74. }
  75. }
  76. return implode("",$result[0]);
  77. }
  78. }