MSysAreaChina.Class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * 地区数据模型(中国)
  4. * Created by PhpStorm.
  5. * User: QianNiao-C
  6. * Date: 2019/10/26
  7. * Time: 10:07
  8. */
  9. namespace JinDouYun\Model\SysAreaChina;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use JinDouYun\Cache\SysAreaChinaCache;
  13. use JinDouYun\Dao\SysAreaChina\DSysAreaChina;
  14. class MSysAreaChina
  15. {
  16. private $objDSysAreaChina;
  17. /**@var array 省市区 */
  18. private static $depth = [
  19. 'province' => 1, //省份
  20. 'city' => 2, //市
  21. 'area' => 3, //区
  22. ];
  23. public function __construct()
  24. {
  25. $this->objDSysAreaChina = new DSysAreaChina('default');
  26. }
  27. /**
  28. * 获取所有省
  29. * @return ResultWrapper
  30. */
  31. public function getAllProvince()
  32. {
  33. $dbResult = $this->objDSysAreaChina->select(['depth' => self::$depth['province']], '*');
  34. if ($dbResult === false) {
  35. return ResultWrapper::fail($this->objDSysAreaChina->error(), ErrorCode::$dberror);
  36. }
  37. return ResultWrapper::success($dbResult);
  38. }
  39. /**
  40. * 获取指定省下的所有市
  41. * @param $selectParams
  42. * @return ResultWrapper
  43. */
  44. public function getAllCityByProvinceCode($selectParams)
  45. {
  46. $selectParams['depth'] = self::$depth['city'];
  47. $dbResult = $this->objDSysAreaChina->select($selectParams, '*');
  48. if ($dbResult === false) {
  49. return ResultWrapper::fail($this->objDSysAreaChina->error(), ErrorCode::$dberror);
  50. }
  51. return ResultWrapper::success($dbResult);
  52. }
  53. /**
  54. * 获取指定市下的所有区
  55. * @param $selectParams
  56. * @return ResultWrapper
  57. */
  58. public function getAllAreaByCityCode($selectParams)
  59. {
  60. $selectParams['depth'] = self::$depth['area'];
  61. $dbResult = $this->objDSysAreaChina->select($selectParams, '*');
  62. if ($dbResult === false) {
  63. return ResultWrapper::fail($this->objDSysAreaChina->error(), ErrorCode::$dberror);
  64. }
  65. return ResultWrapper::success($dbResult);
  66. }
  67. /**
  68. * 通过code=>名称
  69. * @param $params
  70. * @return array
  71. */
  72. public function getNameByCode($params)
  73. {
  74. $objSysAreaChinaCache = new SysAreaChinaCache();
  75. $allNameData = [];
  76. $notCacheCodes = [];
  77. foreach ($params as $k => $v) {
  78. $result = $this->objDSysAreaChina->get(['code'=>$v], 'name,code');
  79. if(empty($result)){
  80. $allNameData[$v] = '';
  81. $notCacheCodes[] = $v;
  82. }else{
  83. $allNameData[$v] = $result['name'];
  84. }
  85. }
  86. $notCacheCodes = array_filter($notCacheCodes);
  87. // 如果缓存取不出来,走数据库
  88. if(!empty($notCacheCodes)){
  89. $dbResult = $this->objDSysAreaChina->select(['code'=>$notCacheCodes], 'name,code');
  90. if ($dbResult === false) {
  91. return $allNameData;
  92. }
  93. if(!empty($dbResult)){
  94. foreach ($dbResult as $key => $value){
  95. $allNameData[$value['code']] = $value['name'];
  96. $objSysAreaChinaCache->cacheCodeRelationName($value['name'], $value['code']);
  97. }
  98. }
  99. }
  100. return $allNameData;
  101. }
  102. /********************************************通过地区名称获取code******************************************/
  103. /**
  104. * @param $params
  105. * @return ResultWrapper
  106. */
  107. public function getCodeByName($params)
  108. {
  109. //参数为省市区名称
  110. $data['provinceName'] = isset($params['provinceName']) ? $params['provinceName'] : false; //省
  111. $data['cityName'] = isset($params['cityName']) ? $params['cityName'] : false; //市
  112. $data['districtName'] = isset($params['districtName']) ? $params['districtName'] : false; //区
  113. $selectKey = '';
  114. $selectKeyArray = [];
  115. foreach($data as $key => $value){
  116. if($value === false){
  117. return ResultWrapper::fail('获取地区'.$key.'参数为空', ErrorCode::$paramError);
  118. }
  119. $selectKey = $selectKey.$value;
  120. $selectKeyArray[$key] = md5($selectKey);
  121. }
  122. if(empty($data)){
  123. return ResultWrapper::fail('获取地区参数为空', ErrorCode::$paramError);
  124. }
  125. $objSysAreaChinaCache = new SysAreaChinaCache();
  126. //获取缓存
  127. $returnData = [];
  128. $emptyData = [];
  129. foreach($data as $key => $value){
  130. $cacheResult = $objSysAreaChinaCache->getCache($selectKeyArray[$key]);
  131. if($cacheResult){
  132. $returnData[strstr($key, 'Name', true).'Code'] = $cacheResult;
  133. }else{
  134. $emptyData[$key] = $value;
  135. }
  136. }
  137. if(count($returnData) == count($data)){
  138. return ResultWrapper::success($returnData);
  139. }
  140. //如果缓存没有
  141. //查询数据库
  142. $sql = 0;
  143. $returnData = [];
  144. foreach($data as $key => $value){
  145. $sonSql = !empty($sql) ? "(".$sql.")" : $sql;
  146. $sql = "select code from ".$this->objDSysAreaChina->get_Table()." where `name` = '".$value."' and pcode = ".$sonSql;
  147. //file_put_contents('/www/wwwroot/logs/apiqnys.liuniukj.com/MSysAreaChina_error.log', date('Y-m-d H:i:s') . '错误信息|sql错误 :' . $sql . PHP_EOL, FILE_APPEND);
  148. $dbResult = $this->objDSysAreaChina->query($sql);
  149. if($dbResult === false){
  150. return ResultWrapper::fail($this->objDSysAreaChina->error(), ErrorCode::$paramError);
  151. }
  152. if(!empty($dbResult)){
  153. $areaData = array_shift($dbResult);
  154. $returnData[strstr($key, 'Name', true).'Code'] = $areaData['code'];
  155. if(!empty($emptyData) && isset($emptyData[$key])){
  156. //缓存地区
  157. $result = $objSysAreaChinaCache->setCache($selectKeyArray[$key], $areaData['code']);
  158. if(!$result){
  159. return ResultWrapper::fail('缓存失败', ErrorCode::$paramError);
  160. }
  161. }
  162. }else{
  163. //这里都是匹配不到的省市区(todo)
  164. $objSysAreaChinaCache->setNotArea($value);
  165. $returnData[strstr($key, 'Name', true).'Code'] = 0;
  166. }
  167. }
  168. if(count($returnData) != count($data)){
  169. return ResultWrapper::fail('地区不存在', ErrorCode::$paramError);
  170. }
  171. return ResultWrapper::success($returnData);
  172. }
  173. }