GhsClass.php 18 KB

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