GhsClass.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. namespace biz\ghs\classes;
  3. use biz\shop\classes\ShopClass;
  4. use biz\sj\classes\MerchantAssetClass;
  5. use biz\sj\classes\SjClass;
  6. use biz\wx\classes\WxMessageClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use common\components\imgUtil;
  9. use common\components\noticeUtil;
  10. use common\components\stringUtil;
  11. use common\components\util;
  12. use biz\base\classes\BaseClass;
  13. use bizHd\purchase\classes\PurchaseClass;
  14. class GhsClass extends BaseClass
  15. {
  16. const DEBT_NO = 1;
  17. const DEBT_YES = 2;
  18. public static $baseFile = '\biz\ghs\models\Ghs';
  19. //补充供货商 ssh 20211028
  20. public static function bcGhs($hdShopId)
  21. {
  22. return false;
  23. }
  24. //新增客户和供货商关系 ssh 2021.4.13
  25. public static function build($ghsShopId, $hdShopId, $type = 1, $changeName = '')
  26. {
  27. if ($type == 1) {
  28. $ghs = self::getByCondition(['ownShopId' => $hdShopId, 'shopId' => $ghsShopId]);
  29. if (!empty($ghs)) {
  30. return $ghs;
  31. }
  32. } else {
  33. $custom = CustomClass::getByCondition(['ownShopId' => $ghsShopId, 'shopId' => $hdShopId]);
  34. if (!empty($custom)) {
  35. return $custom;
  36. }
  37. }
  38. $ghsShop = ShopClass::getShopInfo($ghsShopId);
  39. $ghsSjId = $ghsShop['sjId'] ?? 0;
  40. $ghsMainId = $ghsShop['mainId'] ?? 0;
  41. $ghsOwnPtStyle = $ghsShop['ptStyle'] ?? 1;
  42. $ghsAvatar = $ghsShop['shortAvatar'] ?? '';
  43. $ghsDefault = $ghsShop['default'] ?? 0;
  44. $allowDebt = $ghsShop['allowDebt'] ?? 0;
  45. $needCheck = $ghsShop['needCheck'] ?? 0;
  46. $home = $ghsShop['home'] ?? 0;
  47. $homeAmount = $ghsShop['homeAmount'] ?? 0;
  48. $homeNum = $ghsShop['homeNum'] ?? 0;
  49. $passStatus = $needCheck == 1 ? 0 : 1;
  50. $live = $ghsShop['live'] ?? 1;
  51. $isHd = $ghsShop['isHd'] ?? 1;
  52. $showStock = $ghsShop['showStock'] ?? 0;
  53. $allowDebtAmount = $ghsShop['allowDebtAmount'] ?? 0;
  54. if ($isHd == 1) {
  55. $giveLevel = 1;
  56. $level = 1;
  57. } else {
  58. $giveLevel = 0;
  59. $level = 0;
  60. }
  61. $name = $ghsDefault == 1 && $ghsShop['shopName'] == '首店' ? $ghsShop['merchantName'] : $ghsShop['merchantName'] . ' ' . $ghsShop['shopName'];
  62. $py = stringUtil::py($name);
  63. $hdShopInfo = ShopClass::getShopInfo($hdShopId);
  64. $hdSjId = $hdShopInfo['sjId'] ?? 0;
  65. $hdMainId = $hdShopInfo['mainId'] ?? 0;
  66. $hdOwnPtStyle = $hdShopInfo['ptStyle'] ?? 1;
  67. $salt = stringUtil::charsShuffleLowerCase(8);
  68. $ghs = self::getByCondition(['ownShopId' => $hdShopId, 'shopId' => $ghsShopId]);
  69. if (empty($ghs)) {
  70. $data = [
  71. 'sjId' => $ghsSjId,
  72. 'shopId' => $ghsShopId,
  73. 'mainId' => $ghsMainId,
  74. 'ownSjId' => $hdSjId,
  75. 'ownShopId' => $hdShopId,
  76. 'ownMainId' => $hdMainId,
  77. 'ownPtStyle' => $hdOwnPtStyle,
  78. 'name' => $name,
  79. 'py' => $py,
  80. 'avatar' => $ghsAvatar,
  81. 'mobile' => $ghsShop['mobile'] ?? '',
  82. 'province' => $ghsShop['province'] ?? '',
  83. 'city' => $ghsShop['city'] ?? '',
  84. 'dist' => $ghsShop['dist'] ?? '',
  85. 'address' => $ghsShop['address'] ?? '',
  86. 'floor' => $ghsShop['floor'] ?? '',
  87. 'fullAddress' => $ghsShop['fullAddress'] ?? '',
  88. 'lat' => $ghsShop['lat'] ?? '',
  89. 'long' => $ghsShop['long'] ?? '',
  90. 'live' => $live,
  91. 'giveLevel' => $giveLevel,
  92. 'showStock' => $showStock,
  93. 'salt' => $salt,
  94. 'passStatus' => $passStatus,
  95. 'home' => $home,
  96. 'homeAmount' => $homeAmount,
  97. 'homeNum' => $homeNum,
  98. ];
  99. $ghs = self::addGhs($data);
  100. }
  101. $ghsId = $ghs['id'] ?? 0;
  102. $shop = ShopClass::getShopInfo($hdShopId);
  103. $hdAvatar = $shop['shortAvatar'] ?? '';
  104. $default = $shop['default'] ?? 0;
  105. $name = $default == 1 && $shop['shopName'] == '首店' ? $shop['merchantName'] : $shop['merchantName'] . ' ' . $shop['shopName'];
  106. //从分店同步客户,客户被修改的名称也要同步过来
  107. $firstName = empty($changeName) ? $name : $changeName;
  108. $py = stringUtil::py($firstName);
  109. $secondName = $name;
  110. $mobile = $shop['mobile'] ?? '';
  111. $uniGhsId = $shop['uniGhsId'] ?? 0;
  112. $hasManyGhs = $shop['hasManyGhs'] ?? 0;
  113. if ($uniGhsId == 0 && $hasManyGhs == 0) {
  114. ShopClass::updateById($hdShopId, ['uniGhsId' => $ghsId]);
  115. } else {
  116. //从只有一个供货商变成有多个供货商
  117. ShopClass::updateById($hdShopId, ['hasManyGhs' => 1]);
  118. }
  119. $custom = CustomClass::getByCondition(['ownShopId' => $ghsShopId, 'shopId' => $hdShopId]);
  120. if (empty($custom)) {
  121. $customData = [
  122. 'sjId' => $hdSjId,
  123. 'ghsId' => $ghsId,
  124. 'shopId' => $hdShopId,
  125. 'mainId' => $hdMainId,
  126. 'ownSjId' => $ghsSjId,
  127. 'ownShopId' => $ghsShopId,
  128. 'ownMainId' => $ghsMainId,
  129. 'ownPtStyle' => $ghsOwnPtStyle,
  130. 'name' => $firstName,
  131. 'originName' => $secondName,
  132. 'py' => $py,
  133. 'avatar' => $hdAvatar,
  134. 'mobile' => $mobile,
  135. 'province' => $shop['province'] ?? '',
  136. 'city' => $shop['city'] ?? '',
  137. 'dist' => $shop['dist'] ?? '',
  138. 'address' => $shop['address'] ?? '',
  139. 'fullAddress' => $shop['fullAddress'] ?? '',
  140. 'showAddress' => $shop['showAddress'] ?? '',
  141. 'long' => $shop['long'] ?? '',
  142. 'lat' => $shop['lat'] ?? '',
  143. 'debt' => $allowDebt,
  144. 'debtLimit' => $allowDebtAmount,
  145. 'live' => $live,
  146. 'level' => $level,
  147. 'showStock' => $showStock,
  148. 'salt' => $salt,
  149. 'passStatus' => $passStatus,
  150. 'home' => $home,
  151. 'homeAmount' => $homeAmount,
  152. 'homeNum' => $homeNum,
  153. ];
  154. $custom = CustomClass::addCustom($customData);
  155. }
  156. $customId = $custom['id'] ?? 0;
  157. self::updateById($ghsId, ['customId' => $customId]);
  158. $customSjId = $custom['sjId'] ?? 0;
  159. $customSj = SjClass::getById($customSjId, true);
  160. $parentShopId = $customSj->parentShopId ?? 0;
  161. $introduce = [];
  162. if (!empty($parentShopId)) {
  163. $introduce = CustomClass::getByCondition(['ownShopId' => $ghsShopId, 'shopId' => $parentShopId], true);
  164. }
  165. //有新客户提醒供货商
  166. $ghsShopObj = ShopClass::getById($ghsShopId, true);
  167. $customObj = CustomClass::getById($customId, true);
  168. WxMessageClass::newGhsCustomInform($ghsShopObj, $customObj, $introduce);
  169. //新客户加入app通知
  170. //NoticeClass::newCustomNotice($ghsShop, $custom);
  171. if ($type == 1) {
  172. return $ghs;
  173. }
  174. return $custom;
  175. }
  176. //增加供货商 ssh 2021.3.29
  177. public static function addGhs($data)
  178. {
  179. return self::add($data);
  180. }
  181. //供货商列表 ssh 2021.1.24
  182. public static function getGhsList($where)
  183. {
  184. $data = self::getList('*', $where, 'inTurn DESC,addTime ASC');
  185. $data['list'] = self::groupBaseInfo($data['list']);
  186. return $data;
  187. }
  188. //根据xhSupplier的id数组 取供货商信息 ssh 2021.1.18
  189. public static function getGhsByIds($ids)
  190. {
  191. $list = self::getByIds($ids, null, 'id');
  192. if (empty($list)) {
  193. return [];
  194. }
  195. $list = self::groupBaseInfo($list);
  196. return $list;
  197. }
  198. public static function groupBaseInfo($list)
  199. {
  200. if (empty($list)) {
  201. return $list;
  202. }
  203. $ids = array_column($list, 'shopId');
  204. $shopInfo = ShopClass::getByIds($ids, null, 'id');
  205. foreach ($list as $key => $val) {
  206. $avatar = $val['avatar'] ?? '';
  207. $list[$key]['shortAvatar'] = $avatar;
  208. $list[$key]['avatar'] = imgUtil::groupImg($avatar);
  209. $list[$key]['smallAvatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_80,w_80";
  210. $currentShopId = $val['shopId'] ?? 0;
  211. $currentShop = $shopInfo[$currentShopId] ?? [];
  212. //新人福利和推荐福利
  213. $xrFl = $currentShop['xrFl'] ?? 0;
  214. $xrFlAmount = $currentShop['xrFlAmount'] ?? 0;
  215. $tjFl = $currentShop['tjFl'] ?? 0;
  216. $tjFlAmount = $currentShop['tjFlAmount'] ?? 0;
  217. $list[$key]['xrFl'] = $xrFl;
  218. $list[$key]['xrFlAmount'] = $xrFlAmount;
  219. $list[$key]['tjFl'] = $tjFl;
  220. $list[$key]['tjFlAmount'] = $tjFlAmount;
  221. $list[$key]['miniKilo'] = $currentShop['miniKilo'] ?? 0;
  222. $list[$key]['kiloFee'] = $currentShop['kiloFee'] ?? 0;
  223. $list[$key]['presell'] = $currentShop['presell'] ?? 0;
  224. $isOpen = 0;
  225. if (!empty($currentShop)) {
  226. $isOpen = ShopClass::isOpen($currentShop);
  227. }
  228. //营业状态
  229. $list[$key]['isOpen'] = $isOpen;
  230. $list[$key]['openStartTime'] = $currentShop['openStartTime'] ?? '';
  231. $list[$key]['openEndTime'] = $currentShop['openEndTime'] ?? '';
  232. $list[$key]['meetNum'] = isset($currentShop['meetNum']) ? floatval($currentShop['meetNum']) : 0;
  233. $list[$key]['meetAmount'] = isset($currentShop['meetAmount']) ? floatval($currentShop['meetAmount']) : 0;
  234. $list[$key]['cutAmount'] = isset($currentShop['cutAmount']) ? floatval($currentShop['cutAmount']) : 0;
  235. $list[$key]['cutStyle'] = isset($currentShop['cutStyle']) ? floatval($currentShop['cutStyle']) : 0;
  236. $count = PurchaseClass::getCount(['ghsId' => $val['id'], 'debt' => PurchaseClass::DEBT_YES]);
  237. $list[$key]['debtNum'] = $count;
  238. $telephone = $currentShop['telephone'] ?? '';
  239. $list[$key]['telephone'] = $telephone;
  240. $telephone2 = $currentShop['telephone2'] ?? '';
  241. $list[$key]['telephone2'] = $telephone2;
  242. $balance = $val['balance'] ?? 0;
  243. $debtAmount = $val['debtAmount'] ?? 0;
  244. $remainDebtAmount = bcsub($debtAmount, $balance, 2);
  245. $list[$key]['remainDebtAmount'] = floatval($remainDebtAmount);
  246. //与花神的合作
  247. $openKmCg = $currentShop['openKmCg'] ?? 0;
  248. $kmCgAppId = $currentShop['kmCgAppId'] ?? '';
  249. $kmCgUrl = $currentShop['kmCgUrl'] ?? '';
  250. $list[$key]['openKmCg'] = $openKmCg;
  251. $list[$key]['kmCgAppId'] = $kmCgAppId;
  252. $list[$key]['kmCgUrl'] = $kmCgUrl;
  253. $pfLevel = $currentShop['pfLevel'] ?? 0;
  254. $list[$key]['pfLevel'] = $pfLevel;
  255. if (isset($val['mobile']) == false || empty($val['mobile'])) {
  256. $mobile = $currentShop['mobile'] ?? '';
  257. $list[$key]['mobile'] = $mobile;
  258. }
  259. }
  260. return $list;
  261. }
  262. //根据供货商信息 ssh 2021.1.18
  263. public static function getGhsInfo($id)
  264. {
  265. $info = self::getById($id);
  266. if (empty($info)) {
  267. return [];
  268. }
  269. $list = self::groupBaseInfo([$info]);
  270. return current($list);
  271. }
  272. //待结款列表 ssh 2021.126
  273. public static function getDebtList($where)
  274. {
  275. $sjId = $where['sjId'] ?? 0;
  276. if (empty($sjId)) {
  277. util::fail('没有找到商家');
  278. }
  279. $data = self::getList('*', $where, 'debtAmount DESC');
  280. $data['list'] = self::groupBaseInfo($data['list']);
  281. $asset = MerchantAssetClass::getBySjId($sjId);
  282. $data['debtAmount'] = $asset['cgDebtAmount'] ?? 0.00;
  283. $data['debtNum'] = $asset['cgDebtNum'] ?? 0;
  284. return $data;
  285. }
  286. //验证权限 ssh 2021.4.11
  287. public static function valid($info, $shopId)
  288. {
  289. if (empty($info)) {
  290. util::fail('没有供货商');
  291. }
  292. if ($info['ownShopId'] == $shopId) {
  293. return true;
  294. }
  295. util::fail('找不到供货商');
  296. }
  297. // 建立附近批发店关系
  298. public static function buildNearbyGhsRelations($shopId, $pfShopId, $toLat, $toLnt)
  299. {
  300. if (empty($toLat) || empty($toLnt)) {
  301. noticeUtil::push("批发关系建立失败:目标门店坐标为空,shopId={$shopId}");
  302. return false;
  303. }
  304. if (!self::isValidAmapCoordinate($toLat, $toLnt)) {
  305. noticeUtil::push("批发关系建立失败:目标门店坐标非法,shopId={$shopId},lat={$toLat},lng={$toLnt}");
  306. return false;
  307. }
  308. // 获取除虚拟外的所有批发店
  309. $nearbyShops = ShopClass::getAllByCondition(['ptStyle' => 2, 'live' => 1, 'actTime>=' => strtotime('yesterday')], null, 'id, merchantName, lat, long');
  310. if (empty($nearbyShops)) {
  311. return false;
  312. }
  313. //要排除的批发店
  314. $exclude = [64112, 795, 1405, 28051, 62304, 42385, 56611, 38143, 15373, 42946, 15375, 15734, 15736, 15738, 15740, 24132, 29044, 29161, 30902, 44724, 48608, 48642, 82971, 38231, 1105, 3, 154, 208, 23580, 24713, 8249, 16070, 83690, 86726, 1596, 2167, 15007, 88085, 91064, 413, 62421];
  315. foreach ($nearbyShops as $key => $nearbyShop) {
  316. if (in_array($nearbyShop['id'], $exclude)) {
  317. unset($nearbyShops[$key]);
  318. }
  319. }
  320. $nearbyShopOriginPairs = [];
  321. foreach ($nearbyShops as $nearbyShop) {
  322. if ($nearbyShop['id'] == $pfShopId) {
  323. continue;
  324. }
  325. if (!empty($nearbyShop['lat']) && !empty($nearbyShop['long']) && self::isValidAmapCoordinate($nearbyShop['lat'], $nearbyShop['long'])) {
  326. $nearbyShopOriginPairs[] = [
  327. 'shop' => $nearbyShop,
  328. 'origin' => [
  329. 'lat' => $nearbyShop['lat'],
  330. 'lng' => $nearbyShop['long'],
  331. ],
  332. ];
  333. } else {
  334. // 发送钉钉通知
  335. noticeUtil::push('批发商:' . $nearbyShop['merchantName'] . '(' . $nearbyShop['id'] . ')经纬度无效,lat=' . ($nearbyShop['lat'] ?? '') . ',lng=' . ($nearbyShop['long'] ?? ''));
  336. }
  337. }
  338. if (empty($nearbyShopOriginPairs)) {
  339. return false;
  340. }
  341. // 高德批量测距接口最多支持 100 个起点,需分批处理
  342. $chunkPairs = array_chunk($nearbyShopOriginPairs, 100);
  343. $distanceShopPairs = [];
  344. foreach ($chunkPairs as $chunk) {
  345. $chunkOrigins = array_column($chunk, 'origin');
  346. $distances = \common\components\mapUtil::calculateBatchDistance($chunkOrigins, $toLnt, $toLat);
  347. if (empty($distances)) {
  348. noticeUtil::push("批发关系建立失败:高德批量测距返回空结果,shopId={$shopId}");
  349. continue;
  350. }
  351. foreach ($chunk as $index => $pair) {
  352. if (!isset($distances[$index]) || !is_numeric($distances[$index])) {
  353. continue;
  354. }
  355. $distanceShopPairs[] = [
  356. 'distance' => (float)$distances[$index],
  357. 'shop' => $pair['shop'],
  358. ];
  359. }
  360. }
  361. if (empty($distanceShopPairs)) {
  362. noticeUtil::push("批发关系建立失败:没有可用距离数据,shopId={$shopId}");
  363. return false;
  364. }
  365. // 建立关系前按距离从小到大排序
  366. usort($distanceShopPairs, function ($a, $b) {
  367. return $a['distance'] <=> $b['distance'];
  368. });
  369. // 取前6个
  370. $distanceShopPairs = array_slice($distanceShopPairs, 0, 6);
  371. // 建立关系并保存距离
  372. foreach ($distanceShopPairs as $pair) {
  373. $nearbyShop = $pair['shop'];
  374. $ghsShopId = $nearbyShop['id'];
  375. $distance = $pair['distance'] ?? 0;
  376. // 距离大于39公里不建立关系
  377. if ($distance > 39 * 1000) {
  378. continue;
  379. }
  380. // 调用 GhsClass::build 建立关系 (返回的 $ghs 包含 id 和 customId)
  381. $ghs = self::build($ghsShopId, $shopId);
  382. if (!empty($ghs) && $distance > 0) {
  383. $ghsId = $ghs['id'];
  384. $ghsName = $ghs['name'] ?? '';
  385. noticeUtil::push("建立了一个关系:ghsId:" . $ghsId . ' name:' . $ghsName);
  386. self::updateById($ghsId, ['distance' => $distance]);
  387. }
  388. }
  389. return true;
  390. }
  391. /**
  392. * 高德坐标有效性校验(仅保留中国大陆范围坐标)
  393. * @param mixed $lat
  394. * @param mixed $lng
  395. * @return bool
  396. */
  397. private static function isValidAmapCoordinate($lat, $lng)
  398. {
  399. if (!is_numeric($lat) || !is_numeric($lng)) {
  400. return false;
  401. }
  402. $lat = (float)$lat;
  403. $lng = (float)$lng;
  404. if ($lng < 72.004 || $lng > 137.8347) {
  405. return false;
  406. }
  407. if ($lat < 0.8293 || $lat > 55.8271) {
  408. return false;
  409. }
  410. return true;
  411. }
  412. }