OrdersProductProcess.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. /**
  7. *
  8. * Class Unit
  9. * @package App\Models
  10. */
  11. class OrdersProductProcess extends Model
  12. {
  13. protected $table = "orders_product_bom_process";
  14. const CREATED_AT = 'crt_time';
  15. const UPDATED_AT = 'upd_time';
  16. protected $dateFormat = 'U';
  17. //$fillable属性里面的字段是可以赋值的,其他的所有属性不能被赋值
  18. //$guarded属性里面的字段是不可以赋值,其他的所有属性都能被赋值
  19. protected $guarded = [];
  20. public $month = [
  21. 1 => '01_03',
  22. 2 => '01_03',
  23. 3 => '01_03',
  24. 4 => '04_06',
  25. 5 => '04_06',
  26. 6 => '04_06',
  27. 7 => '07_09',
  28. 8 => '07_09',
  29. 9 => '07_09',
  30. 10 => '10_12',
  31. 11 => '10_12',
  32. 12 => '10_12',
  33. ];
  34. public function __construct(array $attributes = [])
  35. {
  36. parent::__construct($attributes);
  37. if (isset($attributes['channel']) && $attributes['channel'] > 0) {
  38. $channel = $attributes['channel'];
  39. $month = (int)substr($channel,4,2);
  40. $channel = substr($channel,0,4).$this->month[$month];
  41. $this->setTableById($channel);
  42. }elseif (isset($attributes['process']) && $attributes['process'] > 0){
  43. $process = $attributes['process'];
  44. $month = (int)substr($process,4,2);
  45. $process = substr($process,0,4).$this->month[$month];
  46. $this->setTableByProcess($process);
  47. }
  48. }
  49. public function setTableById($channel)
  50. {
  51. if ($channel > 0) {
  52. $tb = $this->table.'_' . (string)$channel;
  53. $this->createTable($tb);
  54. $this->setTable($tb);
  55. }
  56. }
  57. //创建表
  58. private function createTable($table){
  59. if(! empty($table) && ! Schema::hasTable($table)){
  60. //执行建表语句
  61. DB::statement('create table '. $table .' like orders_product_bom_process');
  62. }
  63. }
  64. public function setTableByProcess($process)
  65. {
  66. if ($process > 0) {
  67. $tb = $this->table.'_' . (string)$process;
  68. $this->setTable($tb);
  69. }
  70. }
  71. public function is_table_isset(){
  72. if(Schema::hasTable($this->table)) return true;
  73. return false;
  74. }
  75. }