| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App\Model;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- /**
- *
- * Class Unit
- * @package App\Models
- */
- class OrdersProductProcess extends Model
- {
- protected $table = "orders_product_bom_process";
- const CREATED_AT = 'crt_time';
- const UPDATED_AT = 'upd_time';
- protected $dateFormat = 'U';
- //$fillable属性里面的字段是可以赋值的,其他的所有属性不能被赋值
- //$guarded属性里面的字段是不可以赋值,其他的所有属性都能被赋值
- protected $guarded = [];
- public $month = [
- 1 => '01_03',
- 2 => '01_03',
- 3 => '01_03',
- 4 => '04_06',
- 5 => '04_06',
- 6 => '04_06',
- 7 => '07_09',
- 8 => '07_09',
- 9 => '07_09',
- 10 => '10_12',
- 11 => '10_12',
- 12 => '10_12',
- ];
- public function __construct(array $attributes = [])
- {
- parent::__construct($attributes);
- if (isset($attributes['channel']) && $attributes['channel'] > 0) {
- $channel = $attributes['channel'];
- $month = (int)substr($channel,4,2);
- $channel = substr($channel,0,4).$this->month[$month];
- $this->setTableById($channel);
- }elseif (isset($attributes['process']) && $attributes['process'] > 0){
- $process = $attributes['process'];
- $month = (int)substr($process,4,2);
- $process = substr($process,0,4).$this->month[$month];
- $this->setTableByProcess($process);
- }
- }
- public function setConnection($name)
- {
- $mysql = "mysql";
- if($name === '001') $mysql = "mysql_001";
- if($name === '002') $mysql = "mysql_002";
- return parent::setConnection($mysql); // TODO: Change the autogenerated stub
- }
- public function setTableById($channel)
- {
- if ($channel > 0) {
- $tb = $this->table.'_' . (string)$channel;
- $this->createTable($tb);
- $this->setTable($tb);
- }
- }
- //创建表
- private function createTable($table){
- if(! empty($table) && ! Schema::hasTable($table)){
- //执行建表语句
- DB::statement('create table '. $table .' like orders_product_bom_process');
- }
- }
- public function setTableByProcess($process)
- {
- if ($process > 0) {
- $tb = $this->table.'_' . (string)$process;
- $this->setTable($tb);
- }
- }
- public function is_table_isset(){
- if(Schema::hasTable($this->table)) return true;
- return false;
- }
- }
|