ExportServices.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\other\export;
  12. use app\services\activity\bargain\StoreBargainServices;
  13. use app\services\activity\combination\StoreCombinationServices;
  14. use app\services\activity\seckill\StoreSeckillServices;
  15. use app\services\BaseServices;
  16. use app\services\order\StoreOrderServices;
  17. use app\services\product\product\StoreCategoryServices;
  18. use app\services\product\product\StoreDescriptionServices;
  19. use app\services\product\product\StoreProductServices;
  20. use app\services\product\sku\StoreProductAttrResultServices;
  21. use app\services\user\member\MemberCardServices;
  22. use app\services\user\UserServices;
  23. use crmeb\services\SpreadsheetExcelService;
  24. class ExportServices extends BaseServices
  25. {
  26. /**
  27. * 用户导出
  28. * @param $where
  29. * @return array
  30. */
  31. public function exportUserList($where)
  32. {
  33. /** @var UserServices $userServices */
  34. $userServices = app()->make(UserServices::class);
  35. $data = $userServices->index($where)['list'];
  36. $header = ['用户ID', '昵称', '真实姓名', '性别', '电话', '用户等级', '用户分组', '用户标签', '用户类型', '用户余额', '最后登录时间', '注册时间', '是否注销'];
  37. $filename = '用户列表_' . date('YmdHis', time());
  38. $export = $fileKey = [];
  39. if (!empty($data)) {
  40. $i = 0;
  41. foreach ($data as $item) {
  42. $one_data = [
  43. 'uid' => $item['uid'],
  44. 'nickname' => $item['nickname'],
  45. 'real_name' => $item['real_name'],
  46. 'sex' => $item['sex'],
  47. 'phone' => $item['phone'],
  48. 'level' => $item['level'],
  49. 'group_id' => $item['group_id'],
  50. 'labels' => $item['labels'],
  51. 'user_type' => $item['user_type'],
  52. 'now_money' => $item['now_money'],
  53. 'last_time' => date('Y-m-d H:i:s', $item['last_time']),
  54. 'add_time' => date('Y-m-d H:i:s', $item['add_time']),
  55. 'is_del' => $item['is_del'] ? '已注销' : '正常'
  56. ];
  57. $export[] = $one_data;
  58. if ($i == 0) {
  59. $fileKey = array_keys($one_data);
  60. }
  61. $i++;
  62. }
  63. }
  64. return compact('header', 'fileKey', 'export', 'filename');
  65. }
  66. /**
  67. * 订单导出
  68. * @param $where
  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 exportOrderList($where)
  75. {
  76. $header = ['订单号', '收货人姓名', '收货人电话', '收货地址', '商品名称', '规格', '数量', '价格', '总价格', '实际支付', '支付状态', '支付时间', '订单状态', '下单时间', '用户备注', '商家备注', '表单信息'];
  77. $filename = '订单列表_' . date('YmdHis', time());
  78. $export = $fileKey = [];
  79. /** @var StoreOrderServices $orderServices */
  80. $orderServices = app()->make(StoreOrderServices::class);
  81. $data = $orderServices->getOrderList($where)['data'];
  82. if (!empty($data)) {
  83. $i = 0;
  84. foreach ($data as $item) {
  85. if ($item['paid'] == 1) {
  86. switch ($item['pay_type']) {
  87. case 'weixin':
  88. $item['pay_type_name'] = '微信支付';
  89. break;
  90. case 'yue':
  91. $item['pay_type_name'] = '余额支付';
  92. break;
  93. case 'offline':
  94. $item['pay_type_name'] = '线下支付';
  95. break;
  96. default:
  97. $item['pay_type_name'] = '其他支付';
  98. break;
  99. }
  100. } else {
  101. switch ($item['pay_type']) {
  102. default:
  103. $item['pay_type_name'] = '未支付';
  104. break;
  105. case 'offline':
  106. $item['pay_type_name'] = '线下支付';
  107. break;
  108. }
  109. }
  110. if ($item['paid'] == 0 && $item['status'] == 0) {
  111. $item['status_name'] = '未支付';
  112. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  113. $item['status_name'] = '未发货';
  114. } else if ($item['paid'] == 1 && $item['status'] == 0 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  115. $item['status_name'] = '未核销';
  116. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 1 && $item['refund_status'] == 0) {
  117. $item['status_name'] = '待收货';
  118. } else if ($item['paid'] == 1 && $item['status'] == 1 && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  119. $item['status_name'] = '未核销';
  120. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  121. $item['status_name'] = '待评价';
  122. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  123. $item['status_name'] = '已完成';
  124. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  125. $item['status_name'] = '正在退款';
  126. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  127. $item['status_name'] = '已退款';
  128. }
  129. $custom_form = '';
  130. foreach ($item['custom_form'] as $custom_form_value) {
  131. if (is_string($custom_form_value['value'])) {
  132. $custom_form .= $custom_form_value['title'] . ':' . $custom_form_value['value'] . ';';
  133. } elseif (is_array($custom_form_value['value'])) {
  134. $custom_form .= $custom_form_value['title'] . ':' . implode(',', $custom_form_value['value']) . ';';
  135. }
  136. }
  137. // $goodsName = [];
  138. // foreach ($item['_info'] as $value) {
  139. // $_info = $value['cart_info'];
  140. // $sku = '';
  141. // if (isset($_info['productInfo']['attrInfo'])) {
  142. // if (isset($_info['productInfo']['attrInfo']['suk'])) {
  143. // $sku = '(' . $_info['productInfo']['attrInfo']['suk'] . ')';
  144. // }
  145. // }
  146. // if (isset($_info['productInfo']['store_name'])) {
  147. // $goodsName[] = implode(' ',
  148. // [$_info['productInfo']['store_name'],
  149. // $sku,
  150. // "[{$_info['cart_num']} * {$_info['truePrice']}]"
  151. // ]);
  152. // }
  153. // }
  154. // $one_data = [
  155. // 'order_id' => $item['order_id'],
  156. // 'real_name' => $item['real_name'],
  157. // 'user_phone' => $item['user_phone'],
  158. // 'user_address' => $item['user_address'],
  159. // 'goods_name' => $goodsName ? implode("\n", $goodsName) : '',
  160. // 'total_price' => $item['total_price'],
  161. // 'pay_price' => $item['pay_price'],
  162. // 'pay_type_name' => $item['pay_type_name'],
  163. // 'pay_time' => $item['pay_time'] > 0 ? date('Y-m-d H:i', (int)$item['pay_time']) : '暂无',
  164. // 'status_name' => $item['status_name'] ?? '未知状态',
  165. // 'add_time' => $item['add_time'],
  166. // 'mark' => $item['mark'],
  167. // 'remark' => $item['remark'],
  168. // 'custom_form' => $custom_form,
  169. // ];
  170. $goodsInfo = [];
  171. foreach ($item['_info'] as $value) {
  172. $goodsInfo[] = [
  173. $value['cart_info']['productInfo']['store_name'],
  174. $value['cart_info']['productInfo']['attrInfo']['suk'],
  175. $value['cart_info']['cart_num'],
  176. $value['cart_info']['truePrice'],
  177. ];
  178. }
  179. $one_data = [
  180. $item['order_id'],
  181. $item['real_name'],
  182. $item['user_phone'],
  183. $item['user_address'],
  184. $goodsInfo,
  185. $item['total_price'],
  186. $item['pay_price'],
  187. $item['pay_type_name'],
  188. $item['pay_time'] > 0 ? date('Y-m-d H:i', (int)$item['pay_time']) : '暂无',
  189. $item['status_name'] ?? '未知状态',
  190. $item['add_time'],
  191. $item['mark'],
  192. $item['remark'],
  193. $custom_form,
  194. ];
  195. $export[] = $one_data;
  196. if ($i == 0) {
  197. $fileKey = array_keys($one_data);
  198. }
  199. $i++;
  200. }
  201. }
  202. return compact('header', 'fileKey', 'export', 'filename');
  203. }
  204. /**
  205. * 订单导出
  206. * @return array
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\DbException
  209. * @throws \think\db\exception\ModelNotFoundException
  210. */
  211. public function exportOrderDeliveryList()
  212. {
  213. $header = ['订单ID', '订单号', '快递名称', '快递编码', '快递单号', '收货人姓名', '收货人电话', '收货地址', '商品信息', '实际支付', '用户备注'];
  214. $filename = '发货单_' . date('YmdHis', time());
  215. $export = $fileKey = [];
  216. /** @var StoreOrderServices $orderServices */
  217. $orderServices = app()->make(StoreOrderServices::class);
  218. $data = $orderServices->getOrderList(['status' => 1, 'shipping_type' => 1, 'virtual_type' => 0])['data'];
  219. if (!empty($data)) {
  220. $i = 0;
  221. foreach ($data as $item) {
  222. $goodsName = [];
  223. foreach ($item['_info'] as $value) {
  224. $_info = $value['cart_info'];
  225. $sku = '';
  226. if (isset($_info['productInfo']['attrInfo'])) {
  227. if (isset($_info['productInfo']['attrInfo']['suk'])) {
  228. $sku = '(' . $_info['productInfo']['attrInfo']['suk'] . ')';
  229. }
  230. }
  231. if (isset($_info['productInfo']['store_name'])) {
  232. $goodsName[] = implode(' ',
  233. [$_info['productInfo']['store_name'],
  234. $sku,
  235. "[{$_info['cart_num']} * {$_info['truePrice']}]"
  236. ]);
  237. }
  238. }
  239. $one_data = [
  240. 'id' => $item['id'],
  241. 'order_id' => $item['order_id'],
  242. 'delivery_name' => '',
  243. 'delivery_code' => '',
  244. 'delivery_id' => '',
  245. 'real_name' => $item['real_name'],
  246. 'user_phone' => $item['user_phone'],
  247. 'user_address' => $item['user_address'],
  248. 'goods_name' => $goodsName ? implode("\n", $goodsName) : '',
  249. 'pay_price' => $item['pay_price'],
  250. 'mark' => $item['mark'],
  251. ];
  252. $export[] = $one_data;
  253. if ($i == 0) {
  254. $fileKey = array_keys($one_data);
  255. }
  256. $i++;
  257. }
  258. }
  259. return compact('header', 'fileKey', 'export', 'filename');
  260. }
  261. /**
  262. * 商品导出
  263. * @param $where
  264. * @return array
  265. */
  266. public function exportProductList($where)
  267. {
  268. /** @var StoreProductServices $productServices */
  269. $productServices = app()->make(StoreProductServices::class);
  270. [$page, $limit] = $this->getPageValue();
  271. $cateIds = [];
  272. if (isset($where['cate_id']) && $where['cate_id']) {
  273. /** @var StoreCategoryServices $storeCategory */
  274. $storeCategory = app()->make(StoreCategoryServices::class);
  275. $cateIds = $storeCategory->getColumn(['pid' => $where['cate_id']], 'id');
  276. }
  277. if ($cateIds) {
  278. $cateIds[] = $where['cate_id'];
  279. $where['cate_id'] = $cateIds;
  280. }
  281. $productList = $productServices->dao->getList($where, $page, $limit);
  282. $header = [
  283. '商品编号',
  284. '商品名称', '商品类型', '商品分类(一级)', '商品分类(二级)', '商品单位',
  285. '已售数量', '起购数量',
  286. '规格类型', '规格名称', '售价', '划线价', '成本价', '库存', '重量', '体积', '商品编码', '条形码',
  287. '商品简介', '商品关键字', '商品口令',
  288. '购买送积分'
  289. ];
  290. $filename = '商品导出_' . date('YmdHis', time());
  291. $virtualType = ['普通商品', '卡密/网盘', '优惠券', '虚拟商品'];
  292. $export = $fileKey = [];
  293. if (!empty($productList)) {
  294. $productList = array_column($productList, null, 'id');
  295. $productIds = array_column($productList, 'id');
  296. $descriptionArr = app()->make(StoreDescriptionServices::class)->getColumn([['product_id', 'in', $productIds], ['type', '=', 0]], 'description', 'product_id');
  297. $cateIds = implode(',', array_column($productList, 'cate_id'));
  298. /** @var StoreCategoryServices $categoryService */
  299. $categoryService = app()->make(StoreCategoryServices::class);
  300. $cateList = $categoryService->getCateParentAndChildName($cateIds);
  301. $attrResultArr = app()->make(StoreProductAttrResultServices::class)->getColumn([['product_id', 'in', $productIds], ['type', '=', 0]], 'result', 'product_id');
  302. $i = 0;
  303. foreach ($attrResultArr as $product_id => &$attrResult) {
  304. $attrResult = json_decode($attrResult, true);
  305. foreach ($attrResult['value'] as &$value) {
  306. $productInfo = $productList[$product_id];
  307. $cateName = array_filter($cateList, function ($val) use ($productInfo) {
  308. if (in_array($val['id'], explode(',', $productInfo['cate_id']))) {
  309. return $val;
  310. }
  311. });
  312. $skuArr = array_combine(array_column($attrResult['attr'], 'value'), $value['detail']);
  313. $attrArr = [];
  314. foreach ($attrResult['attr'] as $attrArray) {
  315. // 将每个子数组的 'value' 和 'detail' 组合成字符串
  316. if (isset($attrArray['detail'][0]['value'])) {
  317. $attrArray['detail'] = array_column($attrArray['detail'], 'value');
  318. }
  319. $detailString = implode(',', $attrArray['detail']); // 将 detail 数组转换为逗号分隔的字符串
  320. $attrArr[] = $attrArray['value'] . '=' . $detailString;
  321. }
  322. $attrString = implode(';', $attrArr);
  323. $one_data = [
  324. 'id' => intval($product_id),
  325. 'store_name' => $productInfo['store_name'],
  326. 'virtual_type' => $virtualType[$productInfo['virtual_type']],
  327. 'cate_name_one' => reset($cateName)['one'] ?? '',
  328. 'cate_name_two' => reset($cateName)['two'] ?? '',
  329. 'unit_name' => $productInfo['unit_name'],
  330. 'ficti' => intval($productInfo['ficti']),
  331. 'min_qty' => intval($productInfo['min_qty']),
  332. 'spec_type' => intval($productInfo['spec_type']) == 1 ? '多规格' : '单规格',
  333. 'sku_name' => implode(',', $value['detail']),
  334. 'price' => floatval($value['price']),
  335. 'ot_price' => floatval($value['ot_price']),
  336. 'cost' => floatval($value['cost']),
  337. 'stock' => intval($value['stock']),
  338. 'volume' => intval($value['volume'] ?? 0),
  339. 'weight' => intval($value['weight'] ?? 0),
  340. 'bar_code' => $value['bar_code'] ?? '',
  341. 'bar_code_number' => $value['bar_code_number'] ?? '',
  342. 'store_info' => $productInfo['store_info'],
  343. 'keyword' => $productInfo['keyword'],
  344. 'command_word' => $productInfo['command_word'],
  345. 'give_integral' => $productInfo['give_integral'],
  346. ];
  347. $export[] = $one_data;
  348. if ($i == 0) {
  349. $fileKey = array_keys($one_data);
  350. }
  351. $i++;
  352. }
  353. }
  354. }
  355. return compact('header', 'fileKey', 'export', 'filename');
  356. }
  357. /**
  358. * 砍价商品导出
  359. * @param $where
  360. * @return array
  361. * @throws \think\db\exception\DataNotFoundException
  362. * @throws \think\db\exception\DbException
  363. * @throws \think\db\exception\ModelNotFoundException
  364. */
  365. public function exportBargainList($where)
  366. {
  367. $header = ['砍价名称', '起始价格', '最低价', '参与人数', '成功人数', '剩余库存', '活动状态', '活动时间', '添加时间'];
  368. $filename = '砍价列表_' . date('YmdHis', time());
  369. $export = $fileKey = [];
  370. /** @var StoreBargainServices $bargainServices */
  371. $bargainServices = app()->make(StoreBargainServices::class);
  372. $data = $bargainServices->getStoreBargainList($where)['list'];
  373. if (!empty($data)) {
  374. $i = 0;
  375. foreach ($data as $item) {
  376. $one_data = [
  377. 'title' => $item['title'],
  378. 'price' => $item['price'],
  379. 'min_price' => $item['min_price'],
  380. 'count_people_all' => $item['count_people_all'],
  381. 'count_people_success' => $item['count_people_success'],
  382. 'quota' => $item['quota'],
  383. 'start_name' => $item['start_name'],
  384. 'activity_time' => $item['start_time'] . '至' . $item['stop_time'],
  385. 'add_time' => $item['add_time']
  386. ];
  387. $export[] = $one_data;
  388. if ($i == 0) {
  389. $fileKey = array_keys($one_data);
  390. }
  391. $i++;
  392. }
  393. }
  394. return compact('header', 'fileKey', 'export', 'filename');
  395. }
  396. /**
  397. * 拼团商品导出
  398. * @param $where
  399. * @return array
  400. */
  401. public function exportCombinationList($where)
  402. {
  403. $header = ['拼团名称', '拼团价', '原价', '拼团人数', '参与人数', '成团数量', '剩余库存', '活动状态', '活动时间', '添加时间'];
  404. $filename = '拼团列表_' . date('YmdHis', time());
  405. $export = $fileKey = [];
  406. /** @var StoreCombinationServices $combinationServices */
  407. $combinationServices = app()->make(StoreCombinationServices::class);
  408. $data = $combinationServices->systemPage($where)['list'];
  409. if (!empty($data)) {
  410. $i = 0;
  411. foreach ($data as $item) {
  412. $one_data = [
  413. 'title' => $item['title'],
  414. 'price' => $item['price'],
  415. 'ot_price' => $item['ot_price'],
  416. 'count_people' => $item['count_people'],
  417. 'count_people_all' => $item['count_people_all'],
  418. 'count_people_pink' => $item['count_people_pink'],
  419. 'quota' => $item['quota'],
  420. 'start_name' => $item['start_name'],
  421. 'activity_time' => $item['start_time'] . '至' . $item['stop_time'],
  422. 'add_time' => $item['add_time']
  423. ];
  424. $export[] = $one_data;
  425. if ($i == 0) {
  426. $fileKey = array_keys($one_data);
  427. }
  428. $i++;
  429. }
  430. }
  431. return compact('header', 'fileKey', 'export', 'filename');
  432. }
  433. /**
  434. * 秒杀导出
  435. * @param $where
  436. * @return array
  437. * @throws \think\db\exception\DataNotFoundException
  438. * @throws \think\db\exception\DbException
  439. * @throws \think\db\exception\ModelNotFoundException
  440. */
  441. public function exportSeckillList($where)
  442. {
  443. $header = ['秒杀名称', '秒杀价', '原价', '剩余库存', '活动状态', '活动时间', '添加时间'];
  444. $filename = '秒杀列表_' . date('YmdHis', time());
  445. $export = $fileKey = [];
  446. /** @var StoreSeckillServices $seckillServices */
  447. $seckillServices = app()->make(StoreSeckillServices::class);
  448. $data = $seckillServices->systemPage($where)['list'];
  449. if (!empty($data)) {
  450. $i = 0;
  451. foreach ($data as $item) {
  452. $one_data = [
  453. 'title' => $item['title'],
  454. 'price' => $item['price'],
  455. 'ot_price' => $item['ot_price'],
  456. 'quota' => $item['quota'],
  457. 'start_name' => $item['start_name'],
  458. 'activity_time' => $item['start_time'] . '至' . $item['stop_time'],
  459. 'add_time' => $item['add_time']
  460. ];
  461. $export[] = $one_data;
  462. if ($i == 0) {
  463. $fileKey = array_keys($one_data);
  464. }
  465. $i++;
  466. }
  467. }
  468. return compact('header', 'fileKey', 'export', 'filename');
  469. }
  470. /**
  471. * 会员卡导出
  472. * @param $id
  473. * @return array
  474. * @throws \think\db\exception\DataNotFoundException
  475. * @throws \think\db\exception\DbException
  476. * @throws \think\db\exception\ModelNotFoundException
  477. */
  478. public function exportMemberCard($id)
  479. {
  480. /** @var MemberCardServices $memberCardServices */
  481. $memberCardServices = app()->make(MemberCardServices::class);
  482. $data = $memberCardServices->getExportData(['batch_card_id' => $id]);
  483. $header = ['会员卡号', '密码', '领取人', '领取人手机号', '领取时间', '是否使用'];
  484. $filename = $data['title'] . '批次列表_' . date('YmdHis', time());
  485. $export = $fileKey = [];
  486. if (!empty($data['data'])) {
  487. $userIds = array_column($data['data']->toArray(), 'use_uid');
  488. /** @var UserServices $userService */
  489. $userService = app()->make(UserServices::class);
  490. $userList = $userService->getColumn([['uid', 'in', $userIds]], 'nickname,phone,real_name', 'uid');
  491. $i = 0;
  492. foreach ($data['data'] as $item) {
  493. $one_data = [
  494. 'card_number' => $item['card_number'],
  495. 'card_password' => $item['card_password'],
  496. 'user_name' => $userList[$item['use_uid']]['real_name'] ?? $userList[$item['use_uid']]['nickname'] ?? '',
  497. 'user_phone' => $userList[$item['use_uid']]['phone'] ?? "",
  498. 'use_time' => $item['use_time'],
  499. 'use_uid' => $item['use_uid'] ? '已领取' : '未领取'
  500. ];
  501. $export[] = $one_data;
  502. if ($i == 0) {
  503. $fileKey = array_keys($one_data);
  504. }
  505. $i++;
  506. }
  507. }
  508. return compact('header', 'fileKey', 'export', 'filename');
  509. }
  510. /**
  511. * 真实请求导出
  512. * @param $header excel表头
  513. * @param $title 标题
  514. * @param array $export 填充数据
  515. * @param string $filename 保存文件名称
  516. * @param string $suffix 保存文件后缀
  517. * @param bool $is_save true|false 是否保存到本地
  518. * @return mixed
  519. */
  520. public function export($header, $title_arr, $export = [], $filename = '', $suffix = 'xlsx', $is_save = false)
  521. {
  522. $title = isset($title_arr[0]) && !empty($title_arr[0]) ? $title_arr[0] : '导出数据';
  523. $name = isset($title_arr[1]) && !empty($title_arr[1]) ? $title_arr[1] : '导出数据';
  524. $info = isset($title_arr[2]) && !empty($title_arr[2]) ? $title_arr[2] : date('Y-m-d H:i:s', time());
  525. $path = SpreadsheetExcelService::instance()->setExcelHeader($header)
  526. ->setExcelTile($title, $name, $info)
  527. ->setExcelContent($export)
  528. ->excelSave($filename, $suffix, $is_save);
  529. $path = $this->siteUrl() . $path;
  530. return [$path];
  531. }
  532. /**
  533. * 获取系统接口域名
  534. * @return string
  535. */
  536. public function siteUrl()
  537. {
  538. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  539. $domainName = $_SERVER['HTTP_HOST'];
  540. return $protocol . $domainName;
  541. }
  542. /**
  543. * 用户资金导出
  544. * @param $data 导出数据
  545. */
  546. public function userFinance($data = [])
  547. {
  548. $export = [];
  549. if (!empty($data)) {
  550. foreach ($data as $value) {
  551. $export[] = [
  552. $value['uid'],
  553. $value['nickname'],
  554. $value['pm'] == 0 ? '-' . $value['number'] : $value['number'],
  555. $value['title'],
  556. $value['mark'],
  557. $value['add_time'],
  558. ];
  559. }
  560. }
  561. $header = ['会员ID', '昵称', '金额/积分', '类型', '备注', '创建时间'];
  562. $title = ['资金监控', '资金监控', date('Y-m-d H:i:s', time())];
  563. $filename = '资金监控_' . date('YmdHis', time());
  564. $suffix = 'xlsx';
  565. $is_save = true;
  566. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  567. }
  568. /**
  569. * 用户佣金导出
  570. * @param $data 导出数据
  571. */
  572. public function userCommission($data = [])
  573. {
  574. $export = [];
  575. if (!empty($data)) {
  576. foreach ($data as &$value) {
  577. $export[] = [
  578. $value['nickname'],
  579. $value['sum_number'],
  580. $value['now_money'],
  581. $value['brokerage_price'],
  582. $value['extract_price'],
  583. ];
  584. }
  585. }
  586. $header = ['昵称/姓名', '总佣金金额', '账户余额', '账户佣金', '提现到账佣金'];
  587. $title = ['拥金记录', '拥金记录' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  588. $filename = '拥金记录_' . date('YmdHis', time());
  589. $suffix = 'xlsx';
  590. $is_save = true;
  591. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  592. }
  593. /**
  594. * 用户积分导出
  595. * @param $data 导出数据
  596. */
  597. public function userPoint($data = [])
  598. {
  599. $export = [];
  600. if (!empty($data)) {
  601. foreach ($data as $key => $item) {
  602. $export[] = [
  603. $item['id'],
  604. $item['title'],
  605. $item['balance'],
  606. $item['number'],
  607. $item['mark'],
  608. $item['nickname'],
  609. $item['add_time'],
  610. ];
  611. }
  612. }
  613. $header = ['编号', '标题', '变动前积分', '积分变动', '备注', '用户微信昵称', '添加时间'];
  614. $title = ['积分日志', '积分日志' . time(), '生成时间:' . date('Y-m-d H:i:s', time())];
  615. $filename = '积分日志_' . date('YmdHis', time());
  616. $suffix = 'xlsx';
  617. $is_save = true;
  618. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  619. }
  620. /**
  621. * 用户充值导出
  622. * @param $data 导出数据
  623. */
  624. public function userRecharge($data = [])
  625. {
  626. $export = [];
  627. if (!empty($data)) {
  628. foreach ($data as $item) {
  629. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '暂无';
  630. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  631. $item['paid_type'] = $item['paid'] ? '已支付' : '未支付';
  632. $export[] = [
  633. $item['nickname'],
  634. $item['order_id'],
  635. $item['price'],
  636. $item['paid_type'],
  637. $item['_recharge_type'],
  638. $item['_pay_time'],
  639. $item['paid'] == 1 && $item['refund_price'] == $item['price'] ? '已退款' : '未退款'
  640. ];
  641. }
  642. }
  643. $header = ['昵称/姓名', '订单号', '充值金额', '是否支付', '充值类型', '支付时间', '是否退款'];
  644. $title = ['充值记录', '充值记录' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  645. $filename = '充值记录_' . date('YmdHis', time());
  646. $suffix = 'xlsx';
  647. $is_save = true;
  648. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  649. }
  650. /**
  651. * 用户推广导出
  652. * @param $data 导出数据
  653. */
  654. public function userAgent($data = [])
  655. {
  656. $export = [];
  657. if (!empty($data)) {
  658. foreach ($data as $index => $item) {
  659. $export[] = [
  660. $item['uid'],
  661. $item['nickname'],
  662. $item['phone'],
  663. $item['spread_count'],
  664. $item['spread_order']['order_count'],
  665. $item['spread_order']['order_price'],
  666. $item['brokerage_money'],
  667. $item['extract_count_price'],
  668. $item['extract_count_num'],
  669. $item['brokerage_price'],
  670. $item['spread_name'],
  671. ];
  672. }
  673. }
  674. $header = ['用户编号', '昵称', '电话号码', '推广用户数量', '推广订单数量', '推广订单金额', '佣金金额', '已提现金额', '提现次数', '未提现金额', '上级推广人'];
  675. $title = ['推广用户', '推广用户导出' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  676. $filename = '推广用户_' . date('YmdHis', time());
  677. $suffix = 'xlsx';
  678. $is_save = true;
  679. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  680. }
  681. /**
  682. * 微信用户导出
  683. * @param $data 导出数据
  684. */
  685. public function wechatUser($data = [])
  686. {
  687. $export = [];
  688. if (!empty($data)) {
  689. foreach ($data as $index => $item) {
  690. $export[] = [
  691. $item['nickname'],
  692. $item['sex'],
  693. $item['country'] . $item['province'] . $item['city'],
  694. $item['subscribe'] == 1 ? '关注' : '未关注',
  695. ];
  696. }
  697. }
  698. $header = ['名称', '性别', '地区', '是否关注公众号'];
  699. $title = ['微信用户导出', '微信用户导出' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  700. $filename = '微信用户导出_' . date('YmdHis', time());
  701. $suffix = 'xlsx';
  702. $is_save = true;
  703. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  704. }
  705. /**
  706. * 订单资金导出
  707. * @param $data 导出数据
  708. */
  709. public function orderFinance($data = [])
  710. {
  711. $export = [];
  712. if (!empty($data)) {
  713. foreach ($data as $info) {
  714. $time = $info['pay_time'];
  715. $price = $info['total_price'] + $info['pay_postage'];
  716. $zhichu = $info['coupon_price'] + $info['deduction_price'] + $info['cost'];
  717. $profit = ($info['total_price'] + $info['pay_postage']) - ($info['coupon_price'] + $info['deduction_price'] + $info['cost']);
  718. $deduction = $info['deduction_price'];//积分抵扣
  719. $coupon = $info['coupon_price'];//优惠
  720. $cost = $info['cost'];//成本
  721. $export[] = [$time, $price, $zhichu, $cost, $coupon, $deduction, $profit];
  722. }
  723. }
  724. $header = ['时间', '营业额(元)', '支出(元)', '成本', '优惠', '积分抵扣', '盈利(元)'];
  725. $title = ['财务统计', '财务统计', date('Y-m-d H:i:s', time())];
  726. $filename = '财务统计_' . date('YmdHis', time());
  727. $suffix = 'xlsx';
  728. $is_save = true;
  729. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  730. }
  731. /**
  732. * 商铺砍价活动导出
  733. * @param $data 导出数据
  734. */
  735. public function storeBargain($data = [])
  736. {
  737. $export = [];
  738. if (!empty($data)) {
  739. foreach ($data as $index => $item) {
  740. $export[] = [
  741. $item['title'],
  742. $item['info'],
  743. '¥' . $item['price'],
  744. $item['bargain_num'],
  745. $item['status'] ? '开启' : '关闭',
  746. empty($item['start_time']) ? '' : date('Y-m-d H:i:s', (int)$item['start_time']),
  747. empty($item['stop_time']) ? '' : date('Y-m-d H:i:s', (int)$item['stop_time']),
  748. $item['sales'],
  749. $item['quota'],
  750. empty($item['add_time']) ? '' : $item['add_time'],
  751. ];
  752. }
  753. }
  754. $header = ['砍价活动名称', '砍价活动简介', '砍价金额', '用户每次砍价的次数', '砍价状态', '砍价开启时间', '砍价结束时间', '销量', '限量', '添加时间'];
  755. $title = ['砍价商品导出', '商品信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  756. $filename = '砍价商品导出_' . date('YmdHis', time());
  757. $suffix = 'xlsx';
  758. $is_save = true;
  759. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  760. }
  761. /**
  762. * 商铺拼团导出
  763. * @param $data 导出数据
  764. */
  765. public function storeCombination($data = [])
  766. {
  767. $export = [];
  768. if (!empty($data)) {
  769. foreach ($data as $item) {
  770. $export[] = [
  771. $item['id'],
  772. $item['title'],
  773. $item['ot_price'],
  774. $item['price'],
  775. $item['quota'],
  776. $item['count_people'],
  777. $item['count_people_all'],
  778. $item['count_people_pink'],
  779. $item['sales'] ?? 0,
  780. $item['is_show'] ? '开启' : '关闭',
  781. empty($item['stop_time']) ? '' : date('Y/m/d H:i:s', (int)$item['stop_time'])
  782. ];
  783. }
  784. }
  785. $header = ['编号', '拼团名称', '原价', '拼团价', '限量', '拼团人数', '参与人数', '成团数量', '销量', '商品状态', '结束时间'];
  786. $title = ['拼团商品导出', '商品信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  787. $filename = '拼团商品导出_' . date('YmdHis', time());
  788. $suffix = 'xlsx';
  789. $is_save = true;
  790. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  791. }
  792. /**
  793. * 商铺秒杀活动导出
  794. * @param $data 导出数据
  795. */
  796. public function storeSeckill($data = [])
  797. {
  798. $export = [];
  799. if (!empty($data)) {
  800. foreach ($data as $item) {
  801. if ($item['status']) {
  802. if ($item['start_time'] > time())
  803. $item['start_name'] = '活动未开始';
  804. else if ($item['stop_time'] < time())
  805. $item['start_name'] = '活动已结束';
  806. else if ($item['stop_time'] > time() && $item['start_time'] < time())
  807. $item['start_name'] = '正在进行中';
  808. } else {
  809. $item['start_name'] = '活动已结束';
  810. }
  811. $export[] = [
  812. $item['id'],
  813. $item['title'],
  814. $item['info'],
  815. $item['ot_price'],
  816. $item['price'],
  817. $item['quota'],
  818. $item['sales'],
  819. $item['start_name'],
  820. $item['stop_time'] ? date('Y-m-d H:i:s', $item['stop_time']) : '/',
  821. $item['status'] ? '开启' : '关闭',
  822. ];
  823. }
  824. }
  825. $header = ['编号', '活动标题', '活动简介', '原价', '秒杀价', '限量', '销量', '秒杀状态', '结束时间', '状态'];
  826. $title = ['秒杀商品导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time())];
  827. $filename = '秒杀商品导出_' . date('YmdHis', time());
  828. $suffix = 'xlsx';
  829. $is_save = true;
  830. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  831. }
  832. /**
  833. * 商铺商品导出
  834. * @param $data 导出数据
  835. */
  836. public function storeProduct($data = [])
  837. {
  838. $export = [];
  839. if (!empty($data)) {
  840. foreach ($data as $index => $item) {
  841. $export[] = [
  842. $item['store_name'],
  843. $item['store_info'],
  844. $item['cate_name'],
  845. '¥' . $item['price'],
  846. $item['stock'],
  847. $item['sales'],
  848. $item['visitor'],
  849. ];
  850. }
  851. }
  852. $header = ['商品名称', '商品简介', '商品分类', '价格', '库存', '销量', '浏览量'];
  853. $title = ['商品导出', '商品信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  854. $filename = '商品导出_' . date('YmdHis', time());
  855. $suffix = 'xlsx';
  856. $is_save = true;
  857. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  858. }
  859. /**
  860. * 商铺自提点导出
  861. * @param $data 导出数据
  862. */
  863. public function storeMerchant($data = [])
  864. {
  865. $export = [];
  866. if (!empty($data)) {
  867. foreach ($data as $index => $item) {
  868. $export[] = [
  869. $item['name'],
  870. $item['phone'],
  871. $item['address'] . '' . $item['detailed_address'],
  872. $item['day_time'],
  873. $item['is_show'] ? '开启' : '关闭'
  874. ];
  875. }
  876. }
  877. $header = ['提货点名称', '提货点', '地址', '营业时间', '状态'];
  878. $title = ['提货点导出', '提货点信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  879. $filename = '提货点导出_' . date('YmdHis', time());
  880. $suffix = 'xlsx';
  881. $is_save = true;
  882. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  883. }
  884. public function memberCard($data = [])
  885. {
  886. $export = [];
  887. if (!empty($data)) {
  888. foreach ($data['data'] as $index => $item) {
  889. $export[] = [
  890. $item['card_number'],
  891. $item['card_password'],
  892. $item['user_name'],
  893. $item['user_phone'],
  894. $item['use_time'],
  895. $item['use_uid'] ? '已领取' : '未领取'
  896. ];
  897. }
  898. }
  899. $header = ['会员卡号', '密码', '领取人', '领取人手机号', '领取时间', '是否使用'];
  900. $title = ['会员卡导出', '会员卡导出' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  901. $filename = $data['title'] ? ("卡密会员_" . trim(str_replace(["\r\n", "\r", "\\", "\n", "/", "<", ">", "=", " "], '', $data['title']))) : "";
  902. $suffix = 'xlsx';
  903. $is_save = true;
  904. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  905. }
  906. public function tradeData($data = [], $tradeTitle = "交易统计")
  907. {
  908. $export = $header = [];
  909. if (!empty($data)) {
  910. $header = ['时间'];
  911. $headerArray = array_column($data['series'], 'name');
  912. $header = array_merge($header, $headerArray);
  913. $export = [];
  914. foreach ($data['series'] as $index => $item) {
  915. foreach ($data['x'] as $k => $v) {
  916. $export[$v]['time'] = $v;
  917. $export[$v][] = $item['value'][$k];
  918. }
  919. }
  920. }
  921. $title = [$tradeTitle, $tradeTitle, ' 生成时间:' . date('Y-m-d H:i:s', time())];
  922. $filename = $tradeTitle;
  923. $suffix = 'xlsx';
  924. $is_save = true;
  925. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  926. }
  927. /**
  928. * 商品统计
  929. * @param $data 导出数据
  930. */
  931. public function productTrade($data = [])
  932. {
  933. $export = [];
  934. if (!empty($data)) {
  935. foreach ($data as &$value) {
  936. $export[] = [
  937. $value['time'],
  938. $value['browse'],
  939. $value['user'],
  940. $value['cart'],
  941. $value['order'],
  942. $value['payNum'],
  943. $value['pay'],
  944. $value['cost'],
  945. $value['refund'],
  946. $value['refundNum'],
  947. $value['changes'] . '%'
  948. ];
  949. }
  950. }
  951. $header = ['日期/时间', '商品浏览量', '商品访客数', '加购件数', '下单件数', '支付件数', '支付金额', '成本金额', '退款金额', '退款件数', '访客-支付转化率'];
  952. $title = ['商品统计', '商品统计' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  953. $filename = '商品统计_' . date('YmdHis', time());
  954. $suffix = 'xlsx';
  955. $is_save = true;
  956. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  957. }
  958. public function userTrade($data = [])
  959. {
  960. $export = [];
  961. if (!empty($data)) {
  962. foreach ($data as &$value) {
  963. $export[] = [
  964. $value['time'],
  965. $value['user'],
  966. $value['browse'],
  967. $value['new'],
  968. $value['paid'],
  969. $value['vip'],
  970. ];
  971. }
  972. }
  973. $header = ['日期/时间', '访客数', '浏览量', '新增用户数', '成交用户数', '付费会员数'];
  974. $title = ['用户统计', '用户统计' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time())];
  975. $filename = '用户统计_' . date('YmdHis', time());
  976. $suffix = 'xlsx';
  977. $is_save = true;
  978. return $this->export($header, $title, $export, $filename, $suffix, $is_save);
  979. }
  980. }