| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace app\admin\model\stock;
- use think\Model;
- class Datadict extends Model {
- // 表名
- protected $name = 'stock_datadict';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'istree_text',
- 'enabled_text'
- ];
- protected static function init() {
- self::afterInsert(function ($row) {
- $pk = $row->getPk();
- $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
- });
- }
- public function getIstreeList() {
- return ['0' => __('Istree 0'), '1' => __('Istree 1')];
- }
- public function getEnabledList() {
- return ['1' => __('Enabled 1'), '0' => __('Enabled 0')];
- }
- public function getIstreeTextAttr($value, $data) {
- $value = $value ? $value : $data['istree'];
- $list = $this->getIstreeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getEnabledTextAttr($value, $data) {
- $value = $value ? $value : $data['enabled'];
- $list = $this->getEnabledList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getDatadic($code = '') {
- $sqlresult = \think\Db::view('stock_datadict a','code')
- ->view('stock_datadictitem b','name,value','a.id=b.datadict_id')
- ->where('a.code',$code)
- ->order('b.weigh ASC')
- ->select();
- $result = [];
- foreach ($sqlresult as $k => $v) {
- $result[$v['value']] = $v['name'];
- }
- return $result;
- }
- }
|