GhsClass.php 18 KB

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