SystemStoreServices.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\store;
  12. use app\dao\store\SystemStoreDao;
  13. use app\services\BaseServices;
  14. use app\services\order\store\BranchOrderServices;
  15. use app\services\order\StoreDeliveryOrderServices;
  16. use app\services\order\StoreOrderCartInfoServices;
  17. use app\services\order\StoreOrderServices;
  18. use app\services\product\branch\StoreBranchProductServices;
  19. use app\services\product\sku\StoreProductAttrValueServices;
  20. use app\services\store\finance\StoreFinanceFlowServices;
  21. use app\services\system\SystemRoleServices;
  22. use crmeb\exceptions\AdminException;
  23. use crmeb\services\erp\Erp;
  24. use crmeb\services\FormBuilder;
  25. use think\exception\ValidateException;
  26. use think\facade\Cache;
  27. use think\facade\Log;
  28. /**
  29. * 门店
  30. * Class SystemStoreServices
  31. * @package app\services\system\store
  32. * @mixin SystemStoreDao
  33. */
  34. class SystemStoreServices extends BaseServices
  35. {
  36. /**
  37. * 创建form表单
  38. * @var Form
  39. */
  40. protected $builder;
  41. /**
  42. * 构造方法
  43. * SystemStoreServices constructor.
  44. * @param SystemStoreDao $dao
  45. * @param FormBuilder $builder
  46. */
  47. public function __construct(SystemStoreDao $dao, FormBuilder $builder)
  48. {
  49. $this->dao = $dao;
  50. $this->builder = $builder;
  51. }
  52. /**
  53. * 获取单个门店信息
  54. * @param int $id
  55. * @return array|\think\Model|null
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function getStoreInfo(int $id)
  61. {
  62. $storeInfo = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  63. if (!$storeInfo) {
  64. throw new ValidateException('获取门店信息失败');
  65. }
  66. $storeInfo['day_time'] = $storeInfo['day_time'] ? explode('-', $storeInfo['day_time']) : [];
  67. return $storeInfo->toArray();
  68. }
  69. /**
  70. * 附近门店
  71. * @param array $where
  72. * @param string $latitude
  73. * @param string $longitude
  74. * @param string $ip
  75. * @param int $limit
  76. * @return array|mixed
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function getNearbyStore(array $where, string $latitude, string $longitude, string $ip = '', int $limit = 0)
  82. {
  83. $where = array_merge($where, ['type' => 0]);
  84. if ($limit) {
  85. $page = 1;
  86. $field = ['*'];
  87. } else {
  88. [$page, $limit] = $this->getPageValue();
  89. $field = ['id', 'name', 'phone', 'image', 'latitude', 'longitude', 'address', 'detailed_address', 'is_show', 'day_time', 'day_start', 'day_end', 'valid_range'];
  90. }
  91. //默认附近门店
  92. $store_type = $where['store_type'] ?? 1;
  93. $uid = $where['uid'] ?? 0;
  94. unset($where['store_type'], $where['uid']);
  95. if ($store_type != 1) {//常用门店
  96. if ($uid) {
  97. /** @var StoreUserServices $storeUserServices */
  98. $storeUserServices = app()->make(StoreUserServices::class);
  99. $ids = $storeUserServices->getColumn(['uid' => $uid], 'store_id');
  100. if (!$ids) {
  101. return [];
  102. }
  103. $where['ids'] = $ids;
  104. } else {//没登录,无常用门店
  105. return [];
  106. }
  107. }
  108. $storeList = [];
  109. if (isset($where['id']) && $where['id']) {
  110. $storeList = $this->dao->getStoreList($where, $field, $page, $limit, [], $latitude, $longitude, $latitude && $longitude ? 1 : 0);
  111. } elseif ($latitude && $longitude) {
  112. $storeList = $this->dao->getStoreList($where, $field, $page, $limit, [], $latitude, $longitude, 1);
  113. } elseif ((isset($where['province']) && $where['province']) || (isset($where['city']) && $where['city']) || (isset($where['area']) && $where['area'])) {
  114. $storeList = $this->dao->getStoreList($where, $field, $page, $limit, [], $latitude, $longitude, $latitude && $longitude ? 1 : 0);
  115. } elseif ((isset($where['province']) && $where['province']) && (isset($where['city']) && $where['city']) && (isset($where['area']) && $where['area']) && !$latitude && !$longitude) {
  116. $storeList = $this->dao->getStoreList($where, $field, $page, $limit);
  117. } elseif ($ip) {
  118. $strField = $limit == 1 ? '*' : implode(',', $field);
  119. $addressArr = $this->addressHandle($this->convertIp($ip));
  120. $city = $addressArr['city'] ?? '';
  121. if ($city) {
  122. $storeList = $this->dao->getStoreByAddressInfo($city, $where, $strField, $page, $limit);
  123. }
  124. $province = $addressArr['province'] ?? '';
  125. if (!$storeList && $province) {
  126. $storeList = $this->dao->getStoreByAddressInfo($province, $where, $strField, $page, $limit);
  127. }
  128. }
  129. //上面条件都没获取到门店
  130. if (!$storeList) {
  131. $storeList = $this->dao->getStoreList($where, $field, $page, $limit);
  132. }
  133. if ($storeList) {
  134. foreach ($storeList as &$item) {
  135. $item['range'] = 0;
  136. if (isset($item['distance'])) {
  137. $item['range'] = bcdiv($item['distance'], '1000', 1);
  138. } else {
  139. $item['range'] = 0;
  140. }
  141. if (isset($item['is_show']) && $item['is_show'] == 1) {
  142. $item['status_name'] = '营业中';
  143. } else {
  144. $item['status_name'] = '已停业';
  145. }
  146. if ($item['day_start'] && $item['day_end']) {
  147. $start = date('H:i', strtotime($item['day_start']));
  148. $end = date('H:i', strtotime($item['day_end']));
  149. $item['day_time'] = implode(' - ', [$start, $end]);
  150. }
  151. }
  152. }
  153. return $limit == 1 ? ($storeList[0] ?? []) : $storeList;
  154. }
  155. /**
  156. * 获取门店
  157. * @param array $where
  158. * @param array $field
  159. * @param string $latitude
  160. * @param string $longitude
  161. * @param int $product_id
  162. * @param array $with
  163. * @param int $type 0:普通1:秒杀
  164. * @return array
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public function getStoreList(array $where, array $field = ['*'], string $latitude = '', string $longitude = '', int $product_id = 0, array $with = [], int $type = 0, $store_id = 0)
  170. {
  171. [$page, $limit] = $this->getPageValue();
  172. $order = 0;
  173. if (isset($where['order_id']) && $where['order_id']) {
  174. /** @var StoreOrderServices $storeOrderServices */
  175. $storeOrderServices = app()->make(StoreOrderServices::class);
  176. $user_location = $storeOrderServices->value(['id' => $where['order_id']], 'user_location');
  177. [$longitude, $latitude] = explode(' ', $user_location);
  178. }
  179. if ($longitude && $latitude) {
  180. $order = 1;
  181. }
  182. $list = [];
  183. $count = 0;
  184. //该商品上架的门店
  185. if ($product_id) {
  186. /** @var StoreBranchProductServices $productServices */
  187. $productServices = app()->make(StoreBranchProductServices::class);
  188. [$ids, $applicableType] = $productServices->getApplicableStoreIds($product_id, $type);
  189. if ($ids) {
  190. if ($store_id && !in_array($store_id, $ids)) {
  191. $store_id = 0;
  192. }
  193. $where['ids'] = $ids;
  194. } else {
  195. if ($applicableType != 1) {//不是所有门店
  196. return compact('list', 'count');
  197. }
  198. }
  199. }
  200. $oid = (int)($where['order_id'] ?? 0);
  201. unset($where['order_id']);
  202. $storeList = $this->dao->getStoreList($where, $field, $page, $limit, $with, $latitude, $longitude, $order);
  203. $storeIds = [];
  204. if ($oid) {
  205. [$storeIds, $cartInfo] = $this->checkOrderProductShare($oid, $storeList, 2);
  206. }
  207. $prefix = config('admin.store_prefix');
  208. foreach ($storeList as &$item) {
  209. if (isset($item['distance'])) {
  210. $item['range'] = bcdiv($item['distance'], '1000', 1);
  211. } else {
  212. $item['range'] = 0;
  213. }
  214. if ($item['is_show'] == 1) {
  215. $item['status_name'] = '营业中';
  216. } else {
  217. $item['status_name'] = '已停业';
  218. }
  219. $item['prefix'] = $prefix;
  220. if ($oid) {
  221. if (in_array($item['id'], $storeIds)) {
  222. $list[] = $item;
  223. }
  224. } else {
  225. $list[] = $item;
  226. }
  227. }
  228. if (count($list) && $store_id) {
  229. $arr = [];
  230. foreach ($list as $key => $value) {
  231. if ($value['id'] == $store_id) {
  232. $arr = $value;
  233. unset($list[$key]);
  234. }
  235. }
  236. array_unshift($list, $arr);
  237. }
  238. if ($oid) {
  239. $count = count($list);
  240. } else {
  241. $count = $this->dao->count($where);
  242. }
  243. return compact('list', 'count');
  244. }
  245. /**
  246. * 获取提货点头部统计信息
  247. * @return mixed
  248. */
  249. public function getStoreData()
  250. {
  251. $data['show'] = [
  252. 'name' => '显示中的提货点',
  253. 'num' => $this->dao->count(['type' => 0]),
  254. ];
  255. $data['hide'] = [
  256. 'name' => '隐藏中的提货点',
  257. 'num' => $this->dao->count(['type' => 1]),
  258. ];
  259. $data['recycle'] = [
  260. 'name' => '回收站的提货点',
  261. 'num' => $this->dao->count(['type' => 2])
  262. ];
  263. return $data;
  264. }
  265. /**
  266. * 门店重置账号密码表单
  267. * @param int $id
  268. * @return array
  269. * @throws \FormBuilder\Exception\FormBuilderException
  270. * @throws \think\db\exception\DataNotFoundException
  271. * @throws \think\db\exception\DbException
  272. * @throws \think\db\exception\ModelNotFoundException
  273. */
  274. public function storeAdminAccountForm(int $id)
  275. {
  276. $storeInfo = $this->getStoreInfo($id);
  277. /** @var SystemStoreStaffServices $staffServices */
  278. $staffServices = app()->make(SystemStoreStaffServices::class);
  279. $staffInfo = $staffServices->getOne(['store_id' => $storeInfo['id'], 'level' => 0, 'is_admin' => 1, 'is_manager' => 1, 'is_del' => 0]);
  280. $field[] = $this->builder->hidden('staff_id', $staffInfo['id'] ?? 0);
  281. $field[] = $this->builder->input('phone', '管理员手机号', $staffInfo['phone'] ?? '')->col(24)->required('请输入手机号')->info('重置手机号:会同步清除之前绑定的商城用户信息,需要在门店后台重新编辑绑定');
  282. $field[] = $this->builder->input('account', '管理员账号', $staffInfo['account'] ?? '')->col(24)->required('请输入账号');
  283. $field[] = $this->builder->input('password', '登录密码')->type('password')->col(24)->required('请输入密码');
  284. $field[] = $this->builder->input('true_password', '确认密码')->type('password')->col(24)->required('请再次确认密码');
  285. return create_form('门店重置账号密码', $field, $this->url('/store/store/reset_admin/' . $id));
  286. }
  287. /**
  288. * 门店重置账号、密码、管理员手机号
  289. * @param int $id
  290. * @param array $data
  291. * @return bool
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\DbException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. */
  296. public function resetAdmin(int $id, array $data)
  297. {
  298. $storeInfo = $this->getStoreInfo((int)$id);
  299. if (!$storeInfo) {
  300. throw new ValidateException('门店数据不存在');
  301. }
  302. /** @var SystemStoreStaffServices $staffServices */
  303. $staffServices = app()->make(SystemStoreStaffServices::class);
  304. $staffInfo = $staffServices->get((int)$data['staff_id']);
  305. if (!$staffInfo) {
  306. throw new ValidateException('门店管理员数据不存在');
  307. }
  308. $staff_data = [];
  309. if ($data['phone'] != $storeInfo['phone']) {//重置手机号 清空之前绑定
  310. $staff_data['uid'] = 0;
  311. }
  312. $staff_data['account'] = $data['account'];
  313. $staff_data['store_id'] = $storeInfo['id'];
  314. $staff_data['level'] = 0;
  315. $staff_data['is_admin'] = 1;
  316. $staff_data['verify_status'] = 1;
  317. $staff_data['is_manager'] = 1;
  318. $staff_data['is_cashier'] = 1;
  319. $staff_data['add_time'] = time();
  320. $staff_data['pwd'] = $this->passwordHash($data['password']);
  321. $staffInfo = $staffServices->getOne(['account' => $data['account'], 'is_del' => 0]);
  322. if ($data['staff_id'] && $staffServices->getCount(['id' => $data['staff_id'], 'is_del' => 0])) {
  323. if ($staffInfo && $staffInfo['id'] != $data['staff_id']) {
  324. throw new ValidateException('该账号已存在');
  325. }
  326. if (!$staffServices->update($data['staff_id'], $staff_data)) {
  327. throw new ValidateException('创建门店管理员失败!');
  328. }
  329. } else {
  330. if ($staffInfo) {
  331. throw new ValidateException('该账号已存在');
  332. }
  333. if (!$staffServices->save($staff_data)) {
  334. throw new ValidateException('创建门店管理员失败!');
  335. }
  336. }
  337. return true;
  338. }
  339. /**
  340. * 获取erp门店列表
  341. * @return array|mixed
  342. * @throws \Exception
  343. */
  344. public function erpShopList()
  345. {
  346. [$page, $limit] = $this->getPageValue();
  347. if (!sys_config('erp_open')) {
  348. return [];
  349. }
  350. try {
  351. /** @var Erp $erpService */
  352. $erpService = app()->make(Erp::class);
  353. $res = Cache::tag('erp_shop')->remember('list_' . $page . '_' . $limit, function () use ($page, $limit, $erpService) {
  354. return $erpService->serviceDriver('Comment')->getShopList($page, $limit);
  355. }, 60);
  356. } catch (\Throwable $e) {
  357. Log::error([
  358. 'message' => '读取ERP门店信息失败',
  359. 'file' => $e->getFile(),
  360. 'line' => $e->getLine()
  361. ]);
  362. }
  363. return $res['data']['datas'] ?? [];
  364. }
  365. /**
  366. * 保存或修改门店
  367. * @param int $id
  368. * @param array $data
  369. * @param array $staff_data
  370. * @return mixed
  371. */
  372. public function saveStore(int $id, array $data, array $staff_data = [])
  373. {
  374. return $this->transaction(function () use ($id, $data, $staff_data) {
  375. if ($id) {
  376. $is_new = 0;
  377. if ($this->dao->update($id, $data)) {
  378. return [$id, $is_new];
  379. } else {
  380. throw new AdminException('修改失败或者您没有修改什么!');
  381. }
  382. } else {
  383. $is_new = 1;
  384. $data['add_time'] = time();
  385. if ($res = $this->dao->save($data)) {
  386. if ($staff_data) {
  387. $staffServices = app()->make(SystemStoreStaffServices::class);
  388. if ($staffServices->count(['phone' => $staff_data['phone'], 'is_del' => 0])) {
  389. throw new AdminException('该手机号已经存在');
  390. }
  391. if ($staffServices->count(['account' => $staff_data['account'], 'is_del' => 0])) {
  392. throw new AdminException('管理员账号已存在');
  393. }
  394. $staff_data['level'] = 0;
  395. $staff_data['store_id'] = $res->id;
  396. $staff_data['is_admin'] = 1;
  397. $staff_data['is_store'] = 1;
  398. $staff_data['verify_status'] = 1;
  399. $staff_data['is_manager'] = 1;
  400. $staff_data['is_cashier'] = 1;
  401. $staff_data['add_time'] = time();
  402. $staff_data['pwd'] = $this->passwordHash($staff_data['pwd']);
  403. if (!$staffServices->save($staff_data)) {
  404. throw new AdminException('创建门店管理员失败!');
  405. }
  406. $data = [
  407. ['role_name' => '店员', 'type' => 1, 'relation_id' => $res->id, 'status' => 1, 'level' => 1, 'rules' => '1048,1049,1097,1098,1099,1100,1050,1051,1101,1102,1103,1273,1274,1275,1276,1081,1104,1105,1106,1052,1054,1086,1129,1132,1133,1134,1135,1136,1137,1138,1139,1140,1141,1142,1143,1144,1145,1146,1147,1148,1149,1150,1151,1152,1153,1154,1155,1156,1157,1158,1159,1160,1161,1162,1163,1130,1166,1167,1168,1169,1131,1170,1171,1172,1173,1174,1175,1176,1242,1088,1122,1123,1124,1125,1126,1127,1164,1165,1053,1107,1108,1109,1110,1111,1112,1113,1114,1115,1116,1117,1118,1119,1120,1280'],
  408. ['role_name' => '管理员', 'type' => 1, 'relation_id' => $res->id, 'status' => 1, 'level' => 1, 'rules' => '1048,1049,1097,1098,1099,1100,1050,1051,1101,1102,1103,1273,1274,1275,1276,1081,1104,1105,1106,1052,1054,1086,1129,1132,1133,1134,1135,1136,1137,1138,1139,1140,1141,1142,1143,1144,1145,1146,1147,1148,1149,1150,1151,1152,1153,1154,1155,1156,1157,1158,1159,1160,1161,1162,1163,1130,1166,1167,1168,1169,1131,1170,1171,1172,1173,1174,1175,1176,1242,1088,1122,1123,1124,1125,1126,1127,1164,1165,1053,1107,1108,1109,1110,1111,1112,1113,1114,1115,1116,1117,1118,1119,1120,1280,1055,1056,1177,1178,1179,1180,1181,1182,1183,1184,1185,1186,1187,1188,1189,1190,1191,1192,1277,1057,1193,1194,1195,1196,1197,1249,1250,1251,1252,1253,1254,1058,1059,1060,1198,1199,1200,1243,1255,1256,1257,1258,1259,1260,1061,1201,1241,1062,1063,1215,1218,1219,1220,1244,1261,1262,1263,1264,1265,1064,1216,1217,1202,1065,1066,1203,1214,1067,1204,1212,1213,1235,1068,1205,1206,1069,1207,1208,1070,1089,1071,1209,1210,1211,1072,1073,1082,1083,1084,1085,1228,1229,1230,1231,1232,1233,1234,1236,1245,1246,1247,1248,1221,1222,1223,1224,1225,1226,1227,1266,1267,1268,1269,1270,1271,1272,1237,1238,1239,1240']
  409. ];
  410. /** @var SystemRoleServices $systemRoleServices */
  411. $systemRoleServices = app()->make(SystemRoleServices::class);
  412. $systemRoleServices->saveAll($data);
  413. }
  414. return [(int)$res->id, $is_new];
  415. } else {
  416. throw new AdminException('保存失败!');
  417. }
  418. }
  419. });
  420. }
  421. /**
  422. * 获取门店缓存
  423. * @param int $id
  424. * @return array
  425. * @throws \think\db\exception\DataNotFoundException
  426. * @throws \think\db\exception\DbException
  427. * @throws \think\db\exception\ModelNotFoundException
  428. * @author 等风来
  429. * @email 136327134@qq.com
  430. * @date 2022/11/21
  431. */
  432. public function getStoreDisposeCache(int $id, string $felid = '')
  433. {
  434. $storeInfo = $this->dao->cacheRemember($id, function () use ($id) {
  435. $storeInfo = $this->dao->get($id);
  436. return $storeInfo ? $storeInfo->toArray() : null;
  437. });
  438. if ($felid) {
  439. return $storeInfo[$felid] ?? null;
  440. }
  441. if ($storeInfo) {
  442. $storeInfo['latlng'] = $storeInfo['latitude'] . ',' . $storeInfo['longitude'];
  443. $storeInfo['dataVal'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  444. $storeInfo['timeVal'] = $storeInfo['day_time'] ? explode(' - ', $storeInfo['day_time']) : [];
  445. $storeInfo['address2'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  446. return $storeInfo;
  447. }
  448. return [];
  449. }
  450. /**
  451. * 后台获取提货点详情
  452. * @param int $id
  453. * @param string $felid
  454. * @return array|false|mixed|string|string[]|\think\Model|null
  455. * @throws \think\db\exception\DataNotFoundException
  456. * @throws \think\db\exception\DbException
  457. * @throws \think\db\exception\ModelNotFoundException
  458. */
  459. public function getStoreDispose(int $id, string $felid = '')
  460. {
  461. if ($felid) {
  462. return $this->dao->value(['id' => $id], $felid);
  463. } else {
  464. $storeInfo = $this->dao->get($id);
  465. if ($storeInfo) {
  466. $storeInfo['latlng'] = $storeInfo['latitude'] . ',' . $storeInfo['longitude'];
  467. $storeInfo['dataVal'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  468. $storeInfo['timeVal'] = $storeInfo['day_time'] ? explode(' - ', $storeInfo['day_time']) : [];
  469. $storeInfo['address2'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  470. return $storeInfo;
  471. }
  472. return false;
  473. }
  474. }
  475. /**
  476. * 获取门店不分页
  477. * @return array
  478. * @throws \think\db\exception\DataNotFoundException
  479. * @throws \think\db\exception\DbException
  480. * @throws \think\db\exception\ModelNotFoundException
  481. */
  482. public function getStore()
  483. {
  484. return $this->dao->getStore(['type' => 0]);
  485. }
  486. /**
  487. * 获得导出店员列表
  488. * @param array $where
  489. * @return array
  490. * @throws \think\db\exception\DataNotFoundException
  491. * @throws \think\db\exception\DbException
  492. * @throws \think\db\exception\ModelNotFoundException
  493. */
  494. public function getExportData(array $where)
  495. {
  496. return $this->dao->getStoreList($where, ['*']);
  497. }
  498. /**
  499. * 平台门店运营统计
  500. * @param array $time
  501. * @return array
  502. * @throws \think\db\exception\DataNotFoundException
  503. * @throws \think\db\exception\DbException
  504. * @throws \think\db\exception\ModelNotFoundException
  505. */
  506. public function storeChart(array $time)
  507. {
  508. $list = $this->dao->getStoreList(['is_del' => 0, 'is_show' => 1], ['id', 'name', 'image']);
  509. /** @var StoreUserServices $storeUserServices */
  510. $storeUserServices = app()->make(StoreUserServices::class);
  511. /** @var BranchOrderServices $orderServices */
  512. $orderServices = app()->make(BranchOrderServices::class);
  513. /** @var StoreFinanceFlowServices $storeFinancFlowServices */
  514. $storeFinancFlowServices = app()->make(StoreFinanceFlowServices::class);
  515. $where = ['time' => $time];
  516. $order_where = ['paid' => 1, 'pid' => 0, 'is_del' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
  517. foreach ($list as &$item) {
  518. $store_where = ['store_id' => $item['id']];
  519. $item['store_price'] = (float)bcsub((string)$storeFinancFlowServices->sum($where + $store_where + ['trade_type' => 1, 'no_type' => 1, 'pm' => 1, 'is_del' => 0], 'number', true), (string)$storeFinancFlowServices->sum($where + $store_where + ['trade_type' => 1, 'no_type' => 1, 'pm' => 0, 'is_del' => 0], 'number', true), 2);
  520. $item['store_product_count'] = $orderServices->sum($where + $store_where + $order_where, 'total_num', true);
  521. $item['store_order_price'] = $orderServices->sum($where + $store_where + $order_where, 'pay_price', true);
  522. $item['store_user_count'] = $storeUserServices->count($where + $store_where);
  523. }
  524. return $list;
  525. }
  526. /**
  527. * 检测订单商品门店是否支持配送
  528. * @param int $oid
  529. * @param array $storeList
  530. * @param int $getType
  531. * @return array
  532. */
  533. public function checkOrderProductShare(int $oid, array $storeList, int $getType = 1)
  534. {
  535. if (!$oid || !$storeList) {
  536. return [[], []];
  537. }
  538. /** @var StoreOrderServices $storeOrderServices */
  539. $storeOrderServices = app()->make(StoreOrderServices::class);
  540. $orderInfo = $storeOrderServices->get($oid, ['id', 'order_id', 'uid', 'store_id', 'supplier_id']);
  541. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  542. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  543. $cart_info = $storeOrderCartInfoServices->getSplitCartList($oid, 'cart_info');
  544. if (!$cart_info) {
  545. return [[], []];
  546. }
  547. /** @var StoreProductAttrValueServices $skuValueServices */
  548. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  549. $platProductIds = [];
  550. $platStoreProductIds = [];
  551. $storeProductIds = [];
  552. foreach ($cart_info as $cart) {
  553. $productInfo = $cart['productInfo'] ?? [];
  554. if (isset($productInfo['store_delivery']) && !$productInfo['store_delivery']) {//有商品不支持门店配送
  555. return [[], $cart_info];
  556. }
  557. switch ($productInfo['type'] ?? 0) {
  558. case 0://平台
  559. case 2://供应商
  560. $platProductIds[] = $cart['product_id'];
  561. break;
  562. case 1://门店
  563. if ($productInfo['pid']) {//门店自有商品
  564. $storeProductIds[] = $cart['product_id'];
  565. } else {
  566. $platStoreProductIds[] = $cart['product_id'];
  567. }
  568. break;
  569. }
  570. }
  571. /** @var StoreBranchProductServices $branchProductServics */
  572. $branchProductServics = app()->make(StoreBranchProductServices::class);
  573. //转换成平台商品
  574. if ($platStoreProductIds) {
  575. $ids = $branchProductServics->getStoreProductIds($platStoreProductIds);
  576. $platProductIds = array_merge($platProductIds, $ids);
  577. }
  578. $productCount = count($platProductIds);
  579. $result = [];
  580. foreach ($storeList as $store) {
  581. if ($storeProductIds && $store['id'] != $orderInfo['store_id']) {
  582. continue;
  583. }
  584. $is_show = $productCount == $branchProductServics->count(['pid' => $platProductIds, 'is_show' => 1, 'is_del' => 0, 'type' => 1, 'relation_id' => $store['id']]);//商品没下架 && 库存足够
  585. if (!$is_show) {
  586. continue;
  587. }
  588. $stock = true;
  589. foreach ($cart_info as $cart) {
  590. $productInfo = $cart['productInfo'] ?? [];
  591. if (!$productInfo) {
  592. $stock = false;
  593. break;
  594. }
  595. $applicable_store_ids = [];
  596. //验证商品适用门店
  597. if (isset($productInfo['applicable_store_id'])) $applicable_store_ids = is_array($productInfo['applicable_store_id']) ? $productInfo['applicable_store_id'] : explode(',', $productInfo['applicable_store_id']);
  598. if (!isset($productInfo['applicable_type']) || $productInfo['applicable_type'] == 0 || ($productInfo['applicable_type'] == 2 && !in_array($store['id'], $applicable_store_ids))) {
  599. $stock = false;
  600. break;
  601. }
  602. switch ($cart['type']) {
  603. case 0:
  604. case 6:
  605. case 8:
  606. case 9:
  607. case 10:
  608. $suk = $skuValueServices->value(['unique' => $cart['product_attr_unique'], 'product_id' => $cart['product_id'], 'type' => 0], 'suk');
  609. break;
  610. case 1:
  611. case 2:
  612. case 3:
  613. case 5:
  614. case 7:
  615. $suk = $skuValueServices->value(['unique' => $cart['product_attr_unique'], 'product_id' => $cart['activity_id'], 'type' => $cart['type']], 'suk');
  616. break;
  617. }
  618. $branchProductInfo = $branchProductServics->isValidStoreProduct((int)$cart['product_id'], $store['id']);
  619. if (!$branchProductInfo) {
  620. $stock = false;
  621. break;
  622. }
  623. $attrValue = $skuValueServices->get(['suk' => $suk, 'product_id' => $branchProductInfo['id'], 'type' => 0]);
  624. if (!$attrValue) {
  625. $stock = false;
  626. break;
  627. }
  628. if ($cart['cart_num'] > $attrValue['stock']) {
  629. $stock = false;
  630. break;
  631. }
  632. }
  633. if ($stock) {
  634. if ($getType == 1) {//只取一条门店数据
  635. $result[] = $store['id'];
  636. break;
  637. } else {
  638. $result[] = $store['id'];
  639. }
  640. }
  641. }
  642. return [$result, $cart_info];
  643. }
  644. /**
  645. * 计算距离
  646. * @param string $user_lat
  647. * @param string $user_lng
  648. * @param string $store_lat
  649. * @param $store_lng
  650. * @return string
  651. */
  652. public function distance(string $user_lat, string $user_lng, string $store_lat, $store_lng)
  653. {
  654. try {
  655. return (round(6378137 * 2 * asin(sqrt(pow(sin((($store_lat * pi()) / 180 - ($user_lat * pi()) / 180) / 2), 2) + cos(($user_lat * pi()) / 180) * cos(($store_lat * pi()) / 180) * pow(sin((($store_lng * pi()) / 180 - ($user_lng * pi()) / 180) / 2), 2)))));
  656. } catch (\Throwable $e) {
  657. return '0';
  658. }
  659. }
  660. /**
  661. * 验证门店配送范围
  662. * @param int $store_id
  663. * @param array $addressInfo
  664. * @param string $latitude
  665. * @param string $longitude
  666. * @return bool
  667. * @throws \think\db\exception\DataNotFoundException
  668. * @throws \think\db\exception\DbException
  669. * @throws \think\db\exception\ModelNotFoundException
  670. */
  671. public function checkStoreDeliveryScope(int $store_id, array $addressInfo = [], string $latitude = '', string $longitude = '')
  672. {
  673. //没登录 || 无添加地址 默认不验证
  674. if (!$store_id || (!$addressInfo && (!$latitude || !$longitude))) {
  675. return true;
  676. }
  677. //门店是否开启
  678. if (!sys_config('store_func_status', 1)) {
  679. return false;
  680. }
  681. $store_delivery_scope = (int)sys_config('store_delivery_scope', 0);
  682. if (!$store_delivery_scope) {//配送范围不验证
  683. return true;
  684. }
  685. $storeInfo = $this->getStoreDisposeCache($store_id);
  686. if (!$storeInfo) {
  687. return false;
  688. }
  689. if ($addressInfo) {
  690. $user_lat = $addressInfo['latitude'] ?? '';
  691. $user_lng = $addressInfo['longitude'] ?? '';
  692. if (!$user_lat || !$user_lng) {
  693. try {
  694. $user_address = $addressInfo['province'] . $addressInfo['city'] . $addressInfo['district'] . $addressInfo['street'] . $addressInfo['detail'];
  695. /** @var StoreDeliveryOrderServices $deliveryServices */
  696. $deliveryServices = app()->make(StoreDeliveryOrderServices::class);
  697. $addres = $deliveryServices->lbs_address($addressInfo['city'], $user_address);
  698. $user_lat = $addres['location']['lat'] ?? '';
  699. $user_lng = $addres['location']['lng'] ?? '';
  700. } catch (\Exception $e) {
  701. return false;
  702. }
  703. }
  704. } else {
  705. $user_lat = $latitude;
  706. $user_lng = $longitude;
  707. }
  708. $distance = $this->distance($user_lat, $user_lng, $storeInfo['latitude'], $storeInfo['longitude']);
  709. return $distance <= $storeInfo['valid_range'];
  710. }
  711. }