xhShopService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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\configDict;
  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 = configDict::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 = configDict::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 = configDict::getCacheKey('xhShop').$id;
  113. $poidIdKey = configDict::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. * 微信门店拉取并保存
  121. * @param $merchant
  122. * @return bool $array:有拉取到门店(总共数、成功数、审批中的数、失败数) false:无门店
  123. */
  124. public function saveShopList($merchant)
  125. {
  126. $faileApply = 0;
  127. $successApply = 0;
  128. $applying = 0;
  129. $merchantId = $merchant['id'];
  130. $wx = new wxUtil($merchant);
  131. $storeList = $wx->getStoreList(0, 50);//获取门店列表
  132. $storeList = json_decode($storeList, true);
  133. if($storeList['errcode'] != 0){
  134. util::fail('门店拉取失败');
  135. }
  136. if($storeList['total_count'] == 0){
  137. //util::fail('商户还未新建门店');
  138. return false;
  139. }
  140. $shopList = $storeList['business_list'];
  141. $now = time();
  142. $date = date("Y-m-d H:i:s", $now);
  143. $wxUtil = new wxUtil($merchant);
  144. $wifiList = $wxUtil->getShopWifiListAll();
  145. foreach($shopList as $key => $val){
  146. $shop = $val['base_info'];
  147. $poiId = $shop['poi_id'];
  148. $data = [];
  149. $data['businessName'] = $shop['business_name'];
  150. $data['branchName'] = $shop['branch_name'];
  151. $data['merchantId'] = $merchantId;
  152. $data['poiId'] = $poiId;//poiId适用于门店
  153. $data['shopLat'] = (string)$shop['latitude'];
  154. $data['shopLong'] = (string)$shop['longitude'];
  155. $data['shopProvince'] = $shop['province'];
  156. $data['shopCity'] = $shop['city'];
  157. $data['shopDist'] = $shop['district'];
  158. $data['shopAddress'] = $shop['address'];
  159. $data['telephone'] = $shop['telephone'];
  160. $data['introduction'] = $shop['introduction'];
  161. $data['addTime'] = $now;
  162. $data['status'] = $this->getAvailableState($shop['available_state']);
  163. $data['createTime'] = $date;
  164. //取shopId
  165. $data['shopId'] = $wxUtil->getShopId($poiId, $wifiList);
  166. if($data['status'] == 1){
  167. ++$applying;
  168. $info = self::getByPoiId($poiId);
  169. if(!empty($info)){
  170. continue;
  171. }
  172. self::add($data);
  173. $shopId = $wxUtil->getShopId($poiId);
  174. $wifiData = ['shopId' => $shopId, 'createTime' => date('Y-m-d H:i:s', time())];
  175. xhWifi::add($wifiData);//初始化商家wifi
  176. }elseif($data['status'] == 2){
  177. ++$successApply;
  178. $info = self::getByPoiId($poiId);
  179. if(!empty($info)){
  180. continue;
  181. }
  182. self::add($data);
  183. $shopId = $wxUtil->getShopId($poiId);
  184. $wifiData = ['shopId' => $shopId, 'createTime' => date('Y-m-d H:i:s', time())];
  185. xhWifi::add($wifiData);//初始化商家wifi
  186. }else{
  187. ++$faileApply;
  188. }
  189. }
  190. if(($num = count($wifiList)) > 0){
  191. return ['total' => $num, 'success' => $successApply, 'applying' => $applying, 'fail' => $faileApply];
  192. }
  193. return false;
  194. }
  195. /**
  196. * 运营平台后台中调用的,用于自动生成门店数据
  197. * @param $mcInfo
  198. * @param string $branchName
  199. */
  200. public function platformInitStore($mcInfo, $merchant, $branchName = ''){
  201. $wxUtil = new wxUtil($merchant);
  202. $xhShopService = new xhShopService();
  203. $formatStoreData = $xhShopService->getStoreInfo($mcInfo['id'], '', $mcInfo);
  204. $response = $wxUtil->createStore($formatStoreData);
  205. $response = json_decode($response, true);
  206. if($response['errcode'] != 0){
  207. return $response['errmsg'];
  208. }
  209. $now = time();
  210. $data = [];
  211. $data['businessName'] = $mcInfo['merchantName'];
  212. $data['branchName'] = $branchName;
  213. $data['merchantId'] = $mcInfo['id'];
  214. $data['poiId'] = '';
  215. $data['shopId'] = '';
  216. $data['shopLat'] = (string)$mcInfo['shopLat'];
  217. $data['shopLong'] = (string)$mcInfo['shopLong'];
  218. $data['shopProvince'] = $mcInfo['shopProvince'];
  219. $data['shopCity'] = $mcInfo['shopCity'];
  220. $data['shopDist'] = $mcInfo['shopDist'];
  221. $data['shopAddress'] = $mcInfo['shopAddress'];
  222. $data['telephone'] = '';
  223. $data['introduction'] = '';
  224. $data['addTime'] = $now;
  225. $data['status'] = 0;
  226. $data['createTime'] = date('Y-m-d h:i:s', $now);
  227. $re = xhShopService::add($data);
  228. if($re){
  229. return true;
  230. }
  231. return 'shopData save error';
  232. }
  233. /**
  234. * 从 xhShop 取数据
  235. * 给微信接口传门店信息 (门店信息格式化)
  236. * @param $shopId 门店表id
  237. */
  238. public function getShopData($shopId){
  239. $shop = xhShopService::getById($shopId);
  240. $info = ["business" =>[
  241. "base_info" => [
  242. //"sid" => '123456',//非必须参数
  243. "business_name" => $shop['businessName'],//门店名称
  244. "branch_name" => $shop['branchName'],
  245. "province" => $shop['shopProvince'], //"不超过10个字",
  246. "city" => $shop['shopCity'],//"不超过30个字",
  247. "district" => $shop['shopDist'],//"不超过10个字",
  248. "address" => $shop['shopAddress'],//"门店所在的详细街道地址(不要填写省市信息):不超过80个字",
  249. "telephone" => $shop['telephone'],//"不超53个字符(不可以出现文字)",
  250. "categories" => '休闲娱乐,其它娱乐休闲',
  251. "offset_type" => 1,//坐标类型
  252. "longitude" => $shop['shopLong'],
  253. "latitude" => $shop['shopLat'],
  254. //"photo_list" => ["photo_url"=>"https:// 不超过20张.com", "photo_url"=>"https://XXX.com"],
  255. //"recommend" => "不超过200字。麦辣鸡腿堡套餐,麦乐鸡,全家桶",
  256. //"special" => "不超过200字。免费wifi,外卖服务",
  257. "introduction" => $shop['introduction'],
  258. //"open_time" => "8:00-20:00",
  259. //"avg_price" => 100
  260. ]
  261. ]
  262. ];
  263. return $info;
  264. }
  265. /**
  266. * 从 xhMerchant取数据
  267. * 给微信接口传门店信息 (门店信息格式化)
  268. * @param $merchantId 商户号
  269. * @param $branchName 分店名称
  270. * @param $mcInfo
  271. * @return array
  272. * @throws \Exception
  273. */
  274. public function getStoreInfo($merchantId, $branchName='', $mcInfo=''){
  275. /*if(empty($branchName)){
  276. throw new \Exception("分店名称未填写");
  277. }*/
  278. if(empty($mcInfo)){
  279. $mcInfo = $merchantInfo = xhMerchantService::getById($merchantId);
  280. }
  281. if(empty($mcInfo['shopDist'])){
  282. throw new \Exception("区(县)未设定");
  283. }
  284. $info = ["business" =>[
  285. "base_info" => [
  286. //"sid" => '123456',//非必须参数
  287. "business_name" => $mcInfo['merchantName'],//门店名称
  288. "branch_name" => $branchName,
  289. "province" => $mcInfo['shopProvince'], //"不超过10个字",
  290. "city" => $mcInfo['shopCity'],//"不超过30个字",
  291. "district" => empty($mcInfo['shopDist']) ? '' : $mcInfo['shopDist'],//"不超过10个字",
  292. "address" => $mcInfo['shopAddress'],//"门店所在的详细街道地址(不要填写省市信息):不超过80个字",
  293. "telephone" => '18259179840',//"不超53个字符(不可以出现文字)",
  294. "categories" => '休闲娱乐,其它娱乐休闲',
  295. "offset_type" => 1,
  296. "longitude" => $mcInfo['shopLong'],
  297. "latitude" => $mcInfo['shopLat'],
  298. //"photo_list" => ["photo_url"=>"https:// 不超过20张.com", "photo_url"=>"https://XXX.com"],
  299. //"recommend" => "不超过200字。麦辣鸡腿堡套餐,麦乐鸡,全家桶",
  300. //"special" => "不超过200字。免费wifi,外卖服务",
  301. //"introduction" => "不超过300字。麦当劳是全球大型跨国连锁餐厅,1940 年创立于美国,在世界上大约拥有3 万间分店。主要售卖汉堡包,以及薯条、炸鸡、汽水、冰品、沙拉、 水果等快餐食品",
  302. //"open_time" => "8:00-20:00",
  303. //"avg_price" => 100
  304. ]
  305. ]
  306. ];
  307. return $info;
  308. }
  309. /**
  310. * 微信门店的审核状态转换
  311. * @param $n
  312. * @return int
  313. */
  314. private function getAvailableState($n){
  315. switch ($n){
  316. case 1:
  317. return 4;
  318. break;
  319. case 2:
  320. return 1;
  321. break;
  322. case 3:
  323. return 2;
  324. break;
  325. case 4:
  326. return 3;
  327. break;
  328. default:
  329. throw new \Exception('weixin available_state not exist!');
  330. break;
  331. }
  332. }
  333. }