xhShopService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use linslin\yii2\curl;
  5. use common\models\xhShop;
  6. use common\models\xhWifi;
  7. use common\components\dict;
  8. use common\components\util;
  9. use common\components\wxUtil;
  10. class xhShopService {
  11. public static function add($data)
  12. {
  13. $shop = xhShop::add($data);
  14. return $shop;
  15. }
  16. public static function updateById($id,$data)
  17. {
  18. xhShop::updateById($id, $data);
  19. self::refresh($id);
  20. }
  21. public static function updateByPoiId($poiId,$data)
  22. {
  23. xhShop::updateByPoiId($poiId, $data);
  24. self::refresh($poiId, true);
  25. }
  26. public static function getById($id){
  27. $cacheKey = dict::getCacheKey('xhShop').$id;
  28. $cacheData = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  29. if(!empty($cacheData)){
  30. $data = [];
  31. $list = [];
  32. $x = 0;
  33. foreach($cacheData as $cKey => $cVal){
  34. if($cKey%2 == 0){
  35. $list[$x]['key'] = $cVal;
  36. }else{
  37. $list[$x]['value'] = $cVal;
  38. $x++;
  39. }
  40. }
  41. foreach($list as $lKey => $lVal){
  42. $data[$lVal['key']] = $lVal['value'];
  43. }
  44. unset($data['info_already_get_by_sql']);
  45. return $data;
  46. }
  47. $merchant = xhShop::getByCondition(['id' => $id]);
  48. $arr = [$cacheKey];
  49. $arr[] = 'info_already_get_by_sql';
  50. $arr[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  51. if(!empty($merchant)){
  52. foreach($merchant as $key => $val){
  53. $arr[] = $key;
  54. $arr[] = $val;
  55. }
  56. }
  57. Yii::$app->redis->executeCommand('HMSET', $arr);
  58. return $merchant;
  59. }
  60. public static function getByPoiId($poiId)
  61. {
  62. $cacheKey = dict::getCacheKey('xhShop').$poiId;
  63. $shop = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  64. if(!empty($shop)){
  65. $arr = [];
  66. $i = 0;
  67. $x = 0;
  68. foreach($shop as $key => $val){
  69. $i++;
  70. if($i%2 == 0){
  71. $arr[$x]['value'] = $val;
  72. $x++;
  73. }else{
  74. $arr[$x]['key'] = $val;
  75. }
  76. }
  77. $shopArr = [];
  78. foreach($arr as $key => $val){
  79. $shopArr[$val['key']] = $val['value'];
  80. }
  81. unset($shopArr['info_already_get_by_sql']);
  82. return $shopArr;
  83. }
  84. $shop = xhShop::find()->where(['poiId' => $poiId])->asArray()->one();
  85. $params = [$cacheKey];
  86. $params[] = 'info_already_get_by_sql';
  87. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  88. if(!empty($shop)){
  89. foreach($shop as $key => $val){
  90. $params[] = $key;
  91. $params[] = $val;
  92. }
  93. }
  94. Yii::$app->redis->executeCommand('HMSET',$params);
  95. return $shop;
  96. }
  97. /**
  98. * 更新redis缓存
  99. * @param $id shopId 或者 poiId
  100. * @param bool $poiId 默认(false)是shopId, true是poiId
  101. */
  102. public static function refresh($id, $poiId = false)
  103. {
  104. if(!$poiId){
  105. $shop = xhShop::getById($id);
  106. $poiId = $shop['poiId'];
  107. }else{
  108. $shop = xhShop::getByPoiId($id);
  109. $poiId = $id;
  110. $id = $shop['id'];
  111. }
  112. $idKey = dict::getCacheKey('xhShop').$id;
  113. $poidIdKey = dict::getCacheKey('xhShop').$poiId;
  114. Yii::$app->redis->executeCommand('DEL', [$idKey]);
  115. Yii::$app->redis->executeCommand('DEL', [$poidIdKey]);
  116. self::getById($id);
  117. self::getByPoiId($poiId);
  118. }
  119. //微信门店拉取并保存
  120. public function saveShopList($merchant)
  121. {
  122. $faileApply = 0;
  123. $successApply = 0;
  124. $applying = 0;
  125. $sjId = $merchant['id'];
  126. $wx = new wxUtil($merchant);
  127. $storeList = $wx->getStoreList(0, 50);//获取门店列表
  128. $storeList = json_decode($storeList, true);
  129. if($storeList['errcode'] != 0){
  130. util::fail('门店拉取失败');
  131. }
  132. if($storeList['total_count'] == 0){
  133. //util::fail('商户还未新建门店');
  134. return false;
  135. }
  136. $shopList = $storeList['business_list'];
  137. $now = time();
  138. $date = date("Y-m-d H:i:s", $now);
  139. $wxUtil = new wxUtil($merchant);
  140. $wifiList = $wxUtil->getShopWifiListAll();
  141. foreach($shopList as $key => $val){
  142. $shop = $val['base_info'];
  143. $poiId = $shop['poi_id'];
  144. $data = [];
  145. $data['businessName'] = $shop['business_name'];
  146. $data['branchName'] = $shop['branch_name'];
  147. $data['sjId'] = $sjId;
  148. $data['poiId'] = $poiId;//poiId适用于门店
  149. $data['lat'] = (string)$shop['latitude'];
  150. $data['long'] = (string)$shop['longitude'];
  151. $data['province'] = $shop['province'];
  152. $data['city'] = $shop['city'];
  153. $data['dist'] = $shop['district'];
  154. $data['address'] = $shop['address'];
  155. $data['telephone'] = $shop['telephone'];
  156. $data['introduction'] = $shop['introduction'];
  157. $data['addTime'] = $now;
  158. $data['status'] = $this->getAvailableState($shop['available_state']);
  159. $data['createTime'] = $date;
  160. //取shopId
  161. $data['shopId'] = $wxUtil->getShopId($poiId, $wifiList);
  162. if($data['status'] == 1){
  163. ++$applying;
  164. $info = self::getByPoiId($poiId);
  165. if(!empty($info)){
  166. continue;
  167. }
  168. self::add($data);
  169. $shopId = $wxUtil->getShopId($poiId);
  170. $wifiData = ['shopId' => $shopId, 'createTime' => date('Y-m-d H:i:s', time())];
  171. xhWifi::add($wifiData);//初始化商家wifi
  172. }elseif($data['status'] == 2){
  173. ++$successApply;
  174. $info = self::getByPoiId($poiId);
  175. if(!empty($info)){
  176. continue;
  177. }
  178. self::add($data);
  179. $shopId = $wxUtil->getShopId($poiId);
  180. $wifiData = ['shopId' => $shopId, 'createTime' => date('Y-m-d H:i:s', time())];
  181. xhWifi::add($wifiData);//初始化商家wifi
  182. }else{
  183. ++$faileApply;
  184. }
  185. }
  186. if(($num = count($wifiList)) > 0){
  187. return ['total' => $num, 'success' => $successApply, 'applying' => $applying, 'fail' => $faileApply];
  188. }
  189. return false;
  190. }
  191. //运营平台后台中调用的,用于自动生成门店数据
  192. public function platformInitStore($mcInfo, $merchant, $branchName = ''){
  193. $wxUtil = new wxUtil($merchant);
  194. $xhShopService = new xhShopService();
  195. $formatStoreData = $xhShopService->getStoreInfo($mcInfo['id'], '', $mcInfo);
  196. $response = $wxUtil->createStore($formatStoreData);
  197. $response = json_decode($response, true);
  198. if($response['errcode'] != 0){
  199. return $response['errmsg'];
  200. }
  201. $now = time();
  202. $data = [];
  203. $data['businessName'] = $mcInfo['name'];
  204. $data['branchName'] = $branchName;
  205. $data['sjId'] = $mcInfo['id'];
  206. $data['poiId'] = '';
  207. $data['shopId'] = '';
  208. $data['lat'] = (string)$mcInfo['lat'];
  209. $data['long'] = (string)$mcInfo['long'];
  210. $data['province'] = $mcInfo['province'];
  211. $data['city'] = $mcInfo['city'];
  212. $data['dist'] = $mcInfo['dist'];
  213. $data['address'] = $mcInfo['address'];
  214. $data['telephone'] = '';
  215. $data['introduction'] = '';
  216. $data['addTime'] = $now;
  217. $data['status'] = 0;
  218. $data['createTime'] = date('Y-m-d h:i:s', $now);
  219. $re = xhShopService::add($data);
  220. if($re){
  221. return true;
  222. }
  223. return 'shopData save error';
  224. }
  225. //从 xhShop 取数据 给微信接口传门店信息 (门店信息格式化)
  226. public function getShopData($shopId){
  227. $shop = xhShopService::getById($shopId);
  228. $info = ["business" =>[
  229. "base_info" => [
  230. //"sid" => '123456',//非必须参数
  231. "business_name" => $shop['businessName'],//门店名称
  232. "branch_name" => $shop['branchName'],
  233. "province" => $shop['province'], //"不超过10个字",
  234. "city" => $shop['city'],//"不超过30个字",
  235. "district" => $shop['dist'],//"不超过10个字",
  236. "address" => $shop['address'],//"门店所在的详细街道地址(不要填写省市信息):不超过80个字",
  237. "telephone" => $shop['telephone'],//"不超53个字符(不可以出现文字)",
  238. "categories" => '休闲娱乐,其它娱乐休闲',
  239. "offset_type" => 1,//坐标类型
  240. "longitude" => $shop['long'],
  241. "latitude" => $shop['lat'],
  242. //"photo_list" => ["photo_url"=>"https:// 不超过20张.com", "photo_url"=>"https://XXX.com"],
  243. //"recommend" => "不超过200字。麦辣鸡腿堡套餐,麦乐鸡,全家桶",
  244. //"special" => "不超过200字。免费wifi,外卖服务",
  245. "introduction" => $shop['introduction'],
  246. //"open_time" => "8:00-20:00",
  247. //"avg_price" => 100
  248. ]
  249. ]
  250. ];
  251. return $info;
  252. }
  253. /**
  254. * 从 xhMerchant取数据
  255. * 给微信接口传门店信息 (门店信息格式化)
  256. * @param $sjId 商户号
  257. * @param $branchName 分店名称
  258. * @param $mcInfo
  259. * @return array
  260. * @throws \Exception
  261. */
  262. public function getStoreInfo($sjId, $branchName='', $mcInfo=''){
  263. /*if(empty($branchName)){
  264. throw new \Exception("分店名称未填写");
  265. }*/
  266. if(empty($mcInfo)){
  267. $mcInfo = $merchantInfo = xhMerchantService::getById($sjId);
  268. }
  269. if(empty($mcInfo['dist'])){
  270. throw new \Exception("区(县)未设定");
  271. }
  272. $info = ["business" =>[
  273. "base_info" => [
  274. //"sid" => '123456',//非必须参数
  275. "business_name" => $mcInfo['name'],//门店名称
  276. "branch_name" => $branchName,
  277. "province" => $mcInfo['province'], //"不超过10个字",
  278. "city" => $mcInfo['city'],//"不超过30个字",
  279. "district" => empty($mcInfo['dist']) ? '' : $mcInfo['dist'],//"不超过10个字",
  280. "address" => $mcInfo['address'],//"门店所在的详细街道地址(不要填写省市信息):不超过80个字",
  281. "telephone" => '18259179840',//"不超53个字符(不可以出现文字)",
  282. "categories" => '休闲娱乐,其它娱乐休闲',
  283. "offset_type" => 1,
  284. "longitude" => $mcInfo['long'],
  285. "latitude" => $mcInfo['lat'],
  286. //"photo_list" => ["photo_url"=>"https:// 不超过20张.com", "photo_url"=>"https://XXX.com"],
  287. //"recommend" => "不超过200字。麦辣鸡腿堡套餐,麦乐鸡,全家桶",
  288. //"special" => "不超过200字。免费wifi,外卖服务",
  289. //"introduction" => "不超过300字。麦当劳是全球大型跨国连锁餐厅,1940 年创立于美国,在世界上大约拥有3 万间分店。主要售卖汉堡包,以及薯条、炸鸡、汽水、冰品、沙拉、 水果等快餐食品",
  290. //"open_time" => "8:00-20:00",
  291. //"avg_price" => 100
  292. ]
  293. ]
  294. ];
  295. return $info;
  296. }
  297. /**
  298. * 微信门店的审核状态转换
  299. * @param $n
  300. * @return int
  301. */
  302. private function getAvailableState($n){
  303. switch ($n){
  304. case 1:
  305. return 4;
  306. break;
  307. case 2:
  308. return 1;
  309. break;
  310. case 3:
  311. return 2;
  312. break;
  313. case 4:
  314. return 3;
  315. break;
  316. default:
  317. throw new \Exception('weixin available_state not exist!');
  318. break;
  319. }
  320. }
  321. }