OrdersProductProcess.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 setConnection($name)
  50. {
  51. $mysql = "mysql";
  52. if($name === '001') $mysql = "mysql_001";
  53. if($name === '002') $mysql = "mysql_002";
  54. return parent::setConnection($mysql); // TODO: Change the autogenerated stub
  55. }
  56. public function setTableById($channel)
  57. {
  58. if ($channel > 0) {
  59. $tb = $this->table.'_' . (string)$channel;
  60. $this->createTable($tb);
  61. $this->setTable($tb);
  62. }
  63. }
  64. //创建表
  65. private function createTable($table){
  66. if(! empty($table) && ! Schema::hasTable($table)){
  67. //执行建表语句
  68. DB::statement('create table '. $table .' like orders_product_bom_process');
  69. }
  70. }
  71. public function setTableByProcess($process)
  72. {
  73. if ($process > 0) {
  74. $tb = $this->table.'_' . (string)$process;
  75. $this->setTable($tb);
  76. }
  77. }
  78. public function is_table_isset(){
  79. if(Schema::hasTable($this->table)) return true;
  80. return false;
  81. }
  82. }