Dictionary.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Services\Base;
  3. use Illuminate\Support\Facades\Cache;
  4. use App\Models\BaseDictionaryModel;
  5. use App\Models\BaseAreaModel;
  6. /**
  7. * 全局数据字典的调用
  8. *
  9. * 提供配置缓存的类型
  10. *
  11. * @author wangzhoudong <admin@zhen.pl>
  12. * @version 1.0
  13. *
  14. */
  15. class Dictionary {
  16. public $cacheType;
  17. private $ckey_length = 4;
  18. private $mmCache = 'table_dictionary_option_all';
  19. private $dict;
  20. public function __construct() {
  21. }
  22. /**
  23. * 取得数据字典对应数据
  24. *
  25. * @author wangzhoudong
  26. * @param string $table_code 数据字典的类型
  27. * @param string $code 数据字典代码
  28. * @param string $val 数据字典数据对应下标
  29. * @return array 返回数据字典数组
  30. * @throws CException $table_code,$code 不能为空
  31. *
  32. */
  33. public function get($table_code,$code,$val=null) {
  34. $mKey = Cache::get($this->mmCache);
  35. if(isset($mKey[$table_code][$code])) {
  36. $arr = $mKey[$table_code][$code];
  37. }else{
  38. $this->update($table_code,$code);
  39. if(isset($this->dict[$table_code][$code])) {
  40. $arr = $this->dict[$table_code][$code];
  41. }else{
  42. $arr = [];
  43. }
  44. }
  45. if(!$arr) {
  46. return null;
  47. }
  48. if($val !== null) {
  49. if(array_key_exists($val, $arr)) {
  50. return $arr[$val];
  51. }else{
  52. return '';
  53. }
  54. }else{
  55. return $arr;
  56. }
  57. }
  58. /**
  59. * 获取地区数据名称
  60. * @param int $areaId
  61. * $start 排除前面的层级
  62. */
  63. public function areaName($areaId,$spec=" ",$start=1) {
  64. if(!$areaId) {
  65. return '';
  66. }
  67. $objArea = new BaseAreaModel();
  68. $sMerger = "";
  69. $arr = $objArea->select("merger_name")->find($areaId);
  70. if(isset($arr->merger_name)) {
  71. $aMerger = explode(',',$arr->merger_name);
  72. for($i=1,$j=count($aMerger);$i<$j;$i++) {
  73. $sMerger .= $spec . $aMerger[$i];
  74. }
  75. if($sMerger) {
  76. $sMerger = substr($sMerger,strlen($spec));
  77. }
  78. }
  79. return $sMerger;
  80. }
  81. public function area($areaId) {
  82. if(!$areaId) {
  83. return '';
  84. }
  85. $objArea = new BaseAreaModel();
  86. $arr = $objArea->find($areaId);
  87. if($arr) {
  88. $arr = $arr->toArray();
  89. }
  90. return $arr;
  91. }
  92. /**
  93. *
  94. * @param string $table_code 数据字典的类型
  95. * @param string $code 数据字典代码
  96. * @param string $selection 被选中的值。
  97. * @param array $htmlOptions 额外的HTML属性。如name='cate' ,id='cate'下面两个特定的属性是被认可:
  98. * empty: 字符串,指定空选项的文本,它的值为空。
  99. * 'empty'选项也可以是一个值-标签对形式的数组。
  100. * 在一开始每个对都会用于渲染一个列表的选项。注意,
  101. * 文本标签不会被HTML编码。
  102. *
  103. *
  104. */
  105. public function select($table_code, $code, $selection=null, $htmlOptions=array()) {
  106. $selectOption = $htmlOptions;
  107. unset($selectOption['empty']);
  108. $html = "<select ";
  109. foreach($htmlOptions as $key=>$val) {
  110. $html .= " $key='$val'";
  111. }
  112. $html .= ' >';
  113. if (isset ( $htmlOptions ['empty'] )) {
  114. $value = "";
  115. $label = $htmlOptions ['empty'];
  116. if (is_array ( $label )) {
  117. $value = array_keys ( $label );
  118. $value = $value [0];
  119. $label = array_values ( $label );
  120. $label = $label [0];
  121. }
  122. $html .= '<option value="' . $value . '">' . ($label) . "</option>\n";
  123. }
  124. $optionHtml = $this->option($table_code, $code,$selection);
  125. $html .= $optionHtml;
  126. $html .= "</select>";
  127. return $html;
  128. }
  129. public function option($table_code,$code,$selection=null) {
  130. $data = $this->get($table_code,$code);
  131. $html = "";
  132. foreach ($data as $k=>$v) {
  133. $html .= "<option value='$k'";
  134. if($k==$selection) {
  135. $html .= " selected";
  136. }
  137. $html .= ">$v</option>";
  138. }
  139. return $html;
  140. }
  141. /**
  142. * 更新对应数据字典,如参数都为空全部更新
  143. *
  144. * @author wangzhoudong
  145. * @param string $table_code 需要更新的类型
  146. * @param string $code 需要更新的代码
  147. * @return bool 返回成功失败
  148. * @throws CException
  149. *
  150. */
  151. public function update($table_code=null,$code=null) {
  152. $oBaseDictionary = new BaseDictionaryModel();
  153. $oData = $oBaseDictionary->all();
  154. $dict = array();
  155. foreach($oData as $key=>$value) {
  156. $dict[$value->dictionary_table_code][$value->dictionary_code][$value->value] = $value->name;
  157. }
  158. $this->dict = $dict;
  159. Cache::forever($this->mmCache,$this->dict);
  160. return true;
  161. }
  162. /**
  163. * 获取联动数据
  164. * @param $table_code
  165. * @param $code
  166. */
  167. public function linkageData($table_code, $code)
  168. {
  169. $array = [];
  170. $oBaseDictionary = new BaseDictionaryModel();
  171. $data = $oBaseDictionary
  172. ->where("dictionary_table_code", $table_code)
  173. ->where("dictionary_code", $code)
  174. ->where("key", "")
  175. ->orderBy("sort", "DESc")
  176. ->get()->toArray();
  177. foreach($data AS $key => $item){
  178. $array[$key] = $item;
  179. $array[$key]['childs'] = $oBaseDictionary
  180. ->where("dictionary_table_code", $table_code)
  181. ->where("dictionary_code", $code)
  182. ->where("key", $item['value'])
  183. ->orderBy("sort", "DESc")
  184. ->get()->toArray();
  185. }
  186. return $array;
  187. }
  188. }
  189. ?>