OwnerOutStorageRule.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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($value)
  38. {
  39. if ($this->strategy == '默认')return "/";
  40. if (!self::$features)return $value;
  41. $result = [];
  42. preg_match_all('/\d+|[\&\|\(\)]/',$value,$result);
  43. foreach ($result[0] as &$str){
  44. if (is_numeric($str) && isset(self::$features[$str])){
  45. $column = self::$features[$str]["type"];
  46. $logic = self::$features[$str]["logic"];
  47. $describe = self::$features[$str]["describe"];
  48. if (self::$columnMapping){
  49. $column = self::$columnMapping[$column] ?? $column;
  50. switch ($logic){
  51. case "包含":
  52. $logic = " like ";
  53. $describe = "%".$describe."%";
  54. break;
  55. case "不包含":
  56. $logic = " not like ";
  57. $describe = "%".$describe."%";
  58. break;
  59. case "等于":
  60. $logic = " = ";
  61. break;
  62. }
  63. $str = $column.$logic.$describe;
  64. }else $str = self::$features[$str]["type"].' '.self::$features[$str]["logic"].' '.self::$features[$str]["describe"];
  65. }
  66. if ($str == "&"){
  67. if (self::$columnMapping) $str = " and ";
  68. else $str = " 并且 ";
  69. }
  70. if ($str == "|"){
  71. if (self::$columnMapping) $str = " or ";
  72. else $str = " 或 ";
  73. }
  74. }
  75. return implode("",$result[0]);
  76. }
  77. }