You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemStoreServices.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace app\services\store;
  3. use app\dao\store\SystemStoreDao;
  4. use app\services\BaseServices;
  5. use app\services\order\store\BranchOrderServices;
  6. use app\services\order\StoreOrderServices;
  7. use app\services\product\branch\StoreBranchProductServices;
  8. use app\services\store\finance\StoreFinanceFlowServices;
  9. use app\services\system\SystemRoleServices;
  10. use crmeb\exceptions\AdminException;
  11. use crmeb\services\erp\Erp;
  12. use crmeb\services\FormBuilder;
  13. use think\exception\ValidateException;
  14. use think\facade\Cache;
  15. use think\facade\Log;
  16. use think\Model;
  17. /**
  18. * 门店
  19. * Class SystemStoreServices
  20. * @package app\services\system\store
  21. * @method update($id, array $data, ?string $key = null) 修改数据
  22. * @method get(int $id, ?array $field = []) 获取数据
  23. * @method Model getDistanceShortStore(string $latitude = '', string $longitude = '') 获取最近距离距离内的一个门店
  24. * @method getDistanceShortStoreList(string $latitude = '', string $longitude = '', int $limit = 10) 获取距离距离生序门店列表
  25. * @method getStoreByAddressInfo(string $addressInfo = '', string $field = '*', int $limit = 0) 根据地址信息获取门店列表
  26. */
  27. class SystemStoreServices extends BaseServices
  28. {
  29. /**
  30. * 创建form表单
  31. * @var Form
  32. */
  33. protected $builder;
  34. /**
  35. * 构造方法
  36. * SystemStoreServices constructor.
  37. * @param SystemStoreDao $dao
  38. * @param FormBuilder $builder
  39. */
  40. public function __construct(SystemStoreDao $dao, FormBuilder $builder)
  41. {
  42. $this->dao = $dao;
  43. $this->builder = $builder;
  44. }
  45. /**
  46. * 获取单个门店信息
  47. * @param int $id
  48. * @return array|\think\Model|null
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getStoreInfo(int $id)
  54. {
  55. $storeInfo = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  56. if (!$storeInfo) {
  57. throw new ValidateException('获取门店信息失败');
  58. }
  59. $storeInfo['day_time'] = $storeInfo['day_time'] ? explode('-', $storeInfo['day_time']) : [];
  60. return $storeInfo->toArray();
  61. }
  62. /**
  63. * 获取提货点列表
  64. * @param array $where
  65. * @param array $field
  66. * @param string $latitude
  67. * @param string $longitude
  68. * @param int $product_id
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function getStoreList(array $where, array $field = ['*'], string $latitude = '', string $longitude = '', int $product_id = 0)
  75. {
  76. [$page, $limit] = $this->getPageValue();
  77. $order = 0;
  78. if (isset($where['order_id']) && $where['order_id']) {
  79. /** @var StoreOrderServices $storeOrderServices */
  80. $storeOrderServices = app()->make(StoreOrderServices::class);
  81. $user_location = $storeOrderServices->value(['id' => $where['order_id']], 'user_location');
  82. [$longitude, $latitude] = explode(' ', $user_location);
  83. unset($where['order_id']);
  84. }
  85. if ($longitude && $latitude) {
  86. $order = 1;
  87. }
  88. //该商品上架的门店
  89. if ($product_id) {
  90. /** @var StoreBranchProductServices $productServices */
  91. $productServices = app()->make(StoreBranchProductServices::class);
  92. $ids = $productServices->getColumn(['product_id' => $product_id, 'is_show' => 1, 'is_del' => 0],'store_id');
  93. if ($ids) $where['ids'] = $ids;
  94. }
  95. $list = $this->dao->getStoreList($where, $field, $page, $limit, $latitude, $longitude, $order);
  96. foreach ($list as &$item) {
  97. if (isset($item['distance'])) {
  98. $item['range'] = bcdiv($item['distance'], '1000', 1);
  99. }
  100. if ($item['is_show'] == 1) {
  101. $item['status_name'] = '营业中';
  102. } else {
  103. $item['status_name'] = '已停业';
  104. }
  105. }
  106. $count = $this->dao->count($where);
  107. return compact('list', 'count');
  108. }
  109. /**
  110. * 获取提货点头部统计信息
  111. * @return mixed
  112. */
  113. public function getStoreData()
  114. {
  115. $data['show'] = [
  116. 'name' => '显示中的提货点',
  117. 'num' => $this->dao->count(['type' => 0]),
  118. ];
  119. $data['hide'] = [
  120. 'name' => '隐藏中的提货点',
  121. 'num' => $this->dao->count(['type' => 1]),
  122. ];
  123. $data['recycle'] = [
  124. 'name' => '回收站的提货点',
  125. 'num' => $this->dao->count(['type' => 2])
  126. ];
  127. return $data;
  128. }
  129. /**
  130. * 门店重置账号密码表单
  131. * @param int $id
  132. * @return array
  133. * @throws \FormBuilder\Exception\FormBuilderException
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function storeAdminAccountForm(int $id)
  139. {
  140. $storeInfo = $this->getStoreInfo($id);
  141. /** @var SystemStoreStaffServices $staffServices */
  142. $staffServices = app()->make(SystemStoreStaffServices::class);
  143. $staffInfo = $staffServices->getOne(['store_id' => $storeInfo['id'], 'level' => 0, 'is_admin' => 1, 'is_manager' => 1, 'is_del' => 0]);
  144. $field[] = $this->builder->hidden('staff_id', $staffInfo['id'] ?? 0);
  145. $field[] = $this->builder->input('account', '登录账号', $staffInfo['account'] ?? '')->col(24)->required('请输入账号');
  146. $field[] = $this->builder->input('password', '登录密码')->type('password')->col(24)->required('请输入密码');
  147. $field[] = $this->builder->input('true_password', '确认密码')->type('password')->col(24)->required('请再次确认密码');
  148. return create_form('门店重置账号密码', $field, $this->url('/store/store/reset_admin/' . $id));
  149. }
  150. /**
  151. * 获取erp门店列表
  152. * @return array|mixed
  153. * @throws \Exception
  154. */
  155. public function erpShopList()
  156. {
  157. [$page, $limit] = $this->getPageValue();
  158. try {
  159. /** @var Erp $erpService */
  160. $erpService = app()->make(Erp::class);
  161. $res = Cache::tag('erp_shop')->remember('list_' . $page . '_' . $limit, function () use ($page, $limit, $erpService) {
  162. return $erpService->serviceDriver('Comment')->getShopList($page, $limit);
  163. }, 60);
  164. } catch (\Throwable $e) {
  165. Log::error([
  166. 'message' => '读取ERP门店信息失败',
  167. 'file' => $e->getFile(),
  168. 'line' => $e->getLine()
  169. ]);
  170. }
  171. //假数据
  172. $res1 = '[{"nick":"self_5528051628271830245","shop_id":12709731,"session_expired":"------\u6c38\u4e45\u6388\u6743------","created":"2022-03-12 11:46:41.000","shop_url":"","organization":"","co_id":11669619,"session_uid":null,"short_name":"pro","shop_site":"\u5546\u5bb6\u81ea\u6709\u5546\u57ce","shop_name":"CRMEB-PRO"},{"nick":"self_5744639822394629139","shop_id":12801369,"session_expired":"------\u6c38\u4e45\u6388\u6743------","created":"2022-04-01 11:56:09.000","shop_url":"","organization":"","co_id":11669619,"session_uid":null,"short_name":"erp01","shop_site":"\u5546\u5bb6\u81ea\u6709\u5546\u57ce","shop_name":"erp\u5e97\u94fa"},{"nick":null,"shop_id":0,"session_expired":null,"created":null,"shop_url":"","organization":null,"co_id":0,"session_uid":null,"short_name":null,"shop_site":null,"shop_name":"{\u7ebf\u4e0b}"}]';
  173. $res1 = json_decode($res1, true);
  174. $res2 = collect($res1)->slice($page, $limit)->toArray();
  175. return $res['data']['datas'] ?? $res2;
  176. }
  177. /**
  178. * 保存或修改门店
  179. * @param int $id
  180. * @param array $data
  181. * @param array $staff_data
  182. * @return mixed
  183. */
  184. public function saveStore(int $id, array $data, array $staff_data = [])
  185. {
  186. return $this->transaction(function () use ($id, $data, $staff_data) {
  187. if ($id) {
  188. $is_new = 0;
  189. if ($this->dao->update($id, $data)) {
  190. return [$id, $is_new];
  191. } else {
  192. throw new AdminException('修改失败或者您没有修改什么!');
  193. }
  194. } else {
  195. $is_new = 1;
  196. $data['add_time'] = time();
  197. if ($res = $this->dao->save($data)) {
  198. if ($staff_data) {
  199. $staffServices = app()->make(SystemStoreStaffServices::class);
  200. if ($staffServices->count(['phone' => $staff_data['phone'], 'is_del' => 0])) {
  201. throw new AdminException('该手机号已经存在');
  202. }
  203. if ($staffServices->count(['account' => $staff_data['account'], 'is_del' => 0])) {
  204. throw new AdminException('管理员账号已存在');
  205. }
  206. $staff_data['level'] = 0;
  207. $staff_data['store_id'] = $res->id;
  208. $staff_data['is_admin'] = 1;
  209. $staff_data['verify_status'] = 1;
  210. $staff_data['is_manager'] = 1;
  211. $staff_data['is_cashier'] = 1;
  212. $staff_data['add_time'] = time();
  213. $staff_data['pwd'] = $this->passwordHash($staff_data['pwd']);
  214. if (!$staffServices->save($staff_data)) {
  215. throw new AdminException('创建门店管理员失败!');
  216. }
  217. $data = [
  218. ['role_name' => '店员', 'type' => 2, 'store_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'],
  219. ['role_name' => '管理员', 'type' => 2, 'store_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']
  220. ];
  221. /** @var SystemRoleServices $systemRoleServices */
  222. $systemRoleServices = app()->make(SystemRoleServices::class);
  223. $systemRoleServices->saveAll($data);
  224. }
  225. return [(int)$res->id, $is_new];
  226. } else {
  227. throw new AdminException('保存失败!');
  228. }
  229. }
  230. });
  231. }
  232. /**
  233. * 后台获取提货点详情
  234. * @param int $id
  235. * @param string $felid
  236. * @return array|false|mixed|string|string[]|\think\Model|null
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\DbException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. */
  241. public function getStoreDispose(int $id, string $felid = '')
  242. {
  243. if ($felid) {
  244. return $this->dao->value(['id' => $id], $felid);
  245. } else {
  246. $storeInfo = $this->dao->get($id);
  247. if ($storeInfo) {
  248. $storeInfo['latlng'] = $storeInfo['latitude'] . ',' . $storeInfo['longitude'];
  249. $storeInfo['dataVal'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
  250. $storeInfo['timeVal'] = $storeInfo['day_time'] ? explode(' - ', $storeInfo['day_time']) : [];
  251. $storeInfo['address2'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
  252. return $storeInfo;
  253. }
  254. return false;
  255. }
  256. }
  257. /**
  258. * 获取门店不分页
  259. * @return array
  260. * @throws \think\db\exception\DataNotFoundException
  261. * @throws \think\db\exception\DbException
  262. * @throws \think\db\exception\ModelNotFoundException
  263. */
  264. public function getStore()
  265. {
  266. return $this->dao->getStore(['type' => 0]);
  267. }
  268. /**
  269. * 获得导出店员列表
  270. * @param array $where
  271. * @return array
  272. * @throws \think\db\exception\DataNotFoundException
  273. * @throws \think\db\exception\DbException
  274. * @throws \think\db\exception\ModelNotFoundException
  275. */
  276. public function getExportData(array $where)
  277. {
  278. return $this->dao->getStoreList($where, ['*']);
  279. }
  280. /**
  281. * 平台门店运营统计
  282. * @param array $time
  283. * @return array
  284. * @throws \think\db\exception\DataNotFoundException
  285. * @throws \think\db\exception\DbException
  286. * @throws \think\db\exception\ModelNotFoundException
  287. */
  288. public function storeChart(array $time)
  289. {
  290. $list = $this->dao->getStoreList(['is_del' => 0, 'is_show' => 1], ['id', 'name', 'image']);
  291. /** @var StoreUserServices $storeUserServices */
  292. $storeUserServices = app()->make(StoreUserServices::class);
  293. /** @var BranchOrderServices $orderServices */
  294. $orderServices = app()->make(BranchOrderServices::class);
  295. /** @var StoreFinanceFlowServices $storeFinancFlowServices */
  296. $storeFinancFlowServices = app()->make(StoreFinanceFlowServices::class);
  297. $where = ['time' => $time];
  298. $order_where = ['paid' => 1, 'pid' => 0, 'is_del' => 0, 'is_system_del' => 0, 'refund_status' => [0, 3]];
  299. foreach ($list as &$item) {
  300. $store_where = ['store_id' => $item['id']];
  301. $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);
  302. $item['store_product_count'] = $orderServices->sum($where + $store_where + $order_where, 'total_num', true);
  303. $item['store_order_price'] = $orderServices->sum($where + $store_where + $order_where, 'pay_price', true);
  304. $item['store_user_count'] = $storeUserServices->count($where + $store_where);
  305. }
  306. return $list;
  307. }
  308. }