Dic.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\common\model\base\dic;
  3. /**
  4. * @title : 字典模型模型
  5. * @desc :
  6. * @Author : Rock
  7. * @Date : 2021-12-04 09:58:38
  8. * @LastEditTime : 2022-06-28 10:21:38
  9. */
  10. use app\common\model\Common;
  11. class Dic extends Common{
  12. protected $name = "system_dic";
  13. protected $append = [
  14. 'group_name',
  15. 'status_txt'
  16. ];
  17. static public function statusList()
  18. {
  19. return [1=>'启用',2=>'禁用'];
  20. }
  21. public function getStatusTxtAttr($value,$data)
  22. {
  23. $value = $value ? $value : (isset($data['status']) ? $data['status'] : 0);
  24. $statusList = $this->statusList();
  25. return isset($statusList[$value])?$statusList[$value]:'';
  26. }
  27. public function getGroupNameAttr($value,$data)
  28. {
  29. $value = isset($data['group_code'])?$data['group_code']:'';
  30. $DicGroupList = cache('DIC_GROUP_'.$value);
  31. if(empty($DicGroupList)){
  32. $DicGroupList = DicGroup::where('group_code',$value)->column('group_name','group_code');
  33. cache('DIC_GROUP_'.$value,$DicGroupList,7200);
  34. }
  35. return isset($DicGroupList[$value])?$DicGroupList[$value]:'';
  36. }
  37. /**
  38. * @title: 重置排序号
  39. * @desc:
  40. * @param {int} {group_id} {} {分组ID}
  41. * @return {*}
  42. * @Author: Rock
  43. * @Date: 2021-12-04 10:06:46
  44. * @LastEditTime: Do not edit
  45. */
  46. static public function resetSort($group_id)
  47. {
  48. $list = self::where('group_id',$group_id)->order('sort ASC')->select();
  49. foreach($list as $key=>$val){
  50. $val->sort = $key+1;
  51. $val->save();
  52. }
  53. }
  54. /**
  55. * @title:取得最大的排序号
  56. * @desc:
  57. * @param {int} {group_id} {} {分组ID}
  58. * @return {*}
  59. * @Author: Rock
  60. * @Date: 2021-12-04 10:07:43
  61. * @LastEditTime: Do not edit
  62. */
  63. static public function getMaxSort($group_id)
  64. {
  65. $max = self::where('group_id',$group_id)->max('sort');
  66. return $max + 1;
  67. }
  68. /**
  69. * @title: 创建字典
  70. * @desc:
  71. * @param {array} {data} {} {字典数据}
  72. * @return {*}
  73. * @Author: Rock
  74. * @Date: 2021-12-13 18:41:39
  75. * @LastEditTime: Do not edit
  76. */
  77. static public function createDic($data)
  78. {
  79. $code = !empty($data['code'])?$data['code']:getStrFirstChar($data['title']);
  80. $sort = self::where('group_code',$data['group_code'])->max('sort');
  81. if(empty($data['sort'])){
  82. $sort +=1;
  83. }else{
  84. $sort = $data['sort'];
  85. }
  86. $info = self::where('code',$code)->where('group_code',$data['group_code'])->find();
  87. if(empty($info)){
  88. $data['code'] = $code;
  89. }else{
  90. $data['code'] = $code.$sort;
  91. }
  92. return self::create($data);
  93. }
  94. }