Datadict.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\admin\model\stock;
  3. use think\Model;
  4. class Datadict extends Model {
  5. // 表名
  6. protected $name = 'stock_datadict';
  7. // 自动写入时间戳字段
  8. protected $autoWriteTimestamp = 'int';
  9. // 定义时间戳字段名
  10. protected $createTime = 'createtime';
  11. protected $updateTime = 'updatetime';
  12. // 追加属性
  13. protected $append = [
  14. 'istree_text',
  15. 'enabled_text'
  16. ];
  17. protected static function init() {
  18. self::afterInsert(function ($row) {
  19. $pk = $row->getPk();
  20. $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
  21. });
  22. }
  23. public function getIstreeList() {
  24. return ['0' => __('Istree 0'), '1' => __('Istree 1')];
  25. }
  26. public function getEnabledList() {
  27. return ['1' => __('Enabled 1'), '0' => __('Enabled 0')];
  28. }
  29. public function getIstreeTextAttr($value, $data) {
  30. $value = $value ? $value : $data['istree'];
  31. $list = $this->getIstreeList();
  32. return isset($list[$value]) ? $list[$value] : '';
  33. }
  34. public function getEnabledTextAttr($value, $data) {
  35. $value = $value ? $value : $data['enabled'];
  36. $list = $this->getEnabledList();
  37. return isset($list[$value]) ? $list[$value] : '';
  38. }
  39. public function getDatadic($code = '') {
  40. $sqlresult = \think\Db::view('stock_datadict a','code')
  41. ->view('stock_datadictitem b','name,value','a.id=b.datadict_id')
  42. ->where('a.code',$code)
  43. ->order('b.weigh ASC')
  44. ->select();
  45. $result = [];
  46. foreach ($sqlresult as $k => $v) {
  47. $result[$v['value']] = $v['name'];
  48. }
  49. return $result;
  50. }
  51. }