OverviewPanel.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. <?php
  2. namespace App\Http\Controllers\manager\Statistics;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\Statistics\OverviewPanel as request;
  5. use App\Models\Manager\Process\LowPriceGoods as LowPriceGoodsModel;
  6. use App\Models\Manager\Process\ViolationProduct as ViolationProductModel;
  7. use Illuminate\Support\Facades\DB;
  8. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  9. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  10. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  11. use Illuminate\Support\Carbon;
  12. use App\Models\Manager\Process\LowPriceGoodsMember as LowPriceGoodsMemberModel;
  13. use App\Models\Manager\Process\ViolationProductMember as ViolationProductMemberModel;
  14. use App\Servers\Aliyun\Oss;
  15. /**
  16. * 报表统计-概览面板
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2025-12-26
  20. *
  21. */
  22. class OverviewPanel extends Controller
  23. {
  24. /**
  25. * 禁止挂网链接数统计
  26. * @author 唐远望
  27. * @version 1.0
  28. * @date 2025-12-26
  29. *
  30. */
  31. public function getViolationLinkCount(request $request, ViolationProductModel $violationProductModel)
  32. {
  33. $request->scene('getViolationLinkCount')->validate();
  34. $limit = request('limit', config('page_num', 10));
  35. $start_time = request('start_time', '');
  36. $end_time = request('end_time', '');
  37. // 时间条件
  38. $map = [];
  39. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  40. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  41. $result = $violationProductModel->where($map)->where('status', 0)
  42. ->select(['company_name', DB::raw('count(link_url) as count')])->orderby('count', 'desc')
  43. ->groupby('company_name')->paginate($limit);
  44. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  45. }
  46. /**
  47. * 禁止挂网链接数统计-导出
  48. * @author 唐远望
  49. * @version 1.0
  50. * @date 2025-12-26
  51. *
  52. */
  53. public function Violation_export(request $request, ViolationProductModel $violationProductModel)
  54. {
  55. $start_time = request('start_time', '');
  56. $end_time = request('end_time', '');
  57. // 时间条件
  58. $map = [];
  59. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  60. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  61. $result = $violationProductModel->where($map)->where('status', 0)
  62. ->select(['company_name', DB::raw('count(link_url) as count')])->orderby('count', 'desc')
  63. ->groupby('company_name')->get()->toarray();
  64. //执行下载
  65. $oss_url = $this->Violation_export_download($result);
  66. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $oss_url]);
  67. }
  68. /**
  69. * 禁止挂网链接数统计-导出下载
  70. * @author 唐远望
  71. * @version 1.0
  72. * @date 2025-06-17
  73. */
  74. public function Violation_export_download($data)
  75. {
  76. // 创建一个新的 Spreadsheet 对象
  77. $spreadsheet = new Spreadsheet();
  78. $sheet = $spreadsheet->getActiveSheet();
  79. //合并单元格
  80. $sheet->mergeCells('A1:B1');
  81. $sheet->setCellValue('A1', '禁止挂网链接数统计数据(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  82. // 获取合并后的单元格样式对象
  83. $style = $sheet->getStyle('A1');
  84. // 设置水平居中和垂直居中
  85. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  86. // 然后设置行高以适应两行文本
  87. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  88. // 设置表头
  89. $sheet->setCellValue('A2', '公司名称');
  90. $sheet->setCellValue('B2', '链接挂网数量');
  91. // 填充数据
  92. $row = 3; // 从第3行开始
  93. foreach ($data as $item) {
  94. $sheet->setCellValue('A' . $row, $item['company_name']);
  95. $sheet->setCellValue('B' . $row, $item['count']);
  96. $row++;
  97. }
  98. $file_id = date('YmdHis');
  99. // 创建Excel文件
  100. $filename = '禁止挂网链接数统计数据' . $file_id . '.xlsx';
  101. $path = public_path('uploads/exports/');
  102. $fullPath = $path. $filename;
  103. if (!is_dir($path)) mkdir($path, 0777, true);
  104. // 生成 Excel 文件
  105. $writer = new Xlsx($spreadsheet);
  106. $writer->save($fullPath);
  107. // 清理
  108. $spreadsheet->disconnectWorksheets();
  109. unset($spreadsheet, $writer);
  110. $Oss = new Oss();
  111. $oss_url = $Oss->uploadFile($filename, $fullPath);
  112. if ($oss_url) @unlink($fullPath);
  113. return $oss_url;
  114. }
  115. /**
  116. * 禁止挂网公司月度统计
  117. * @author 唐远望
  118. * @version 1.0
  119. * @date 2025-12-26
  120. *
  121. */
  122. public function get_violation_company_count(request $request, ViolationProductModel $violationProductModel)
  123. {
  124. $request->scene('get_violation_company_count')->validate();
  125. $limit = request('limit', config('page_num', 10));
  126. $start_time = request('start_time', '');
  127. $end_time = request('end_time', '');
  128. // 时间条件
  129. $map = [];
  130. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  131. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  132. $result = $violationProductModel->where($map)->where('status', 0)
  133. ->select(['company_name', DB::raw('count(company_name) as count')])->orderby('count', 'desc')
  134. ->groupby('company_name')->paginate($limit);
  135. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  136. }
  137. /**
  138. * 禁止挂网公司月度统计-导出
  139. * @author 唐远望
  140. * @version 1.0
  141. * @date 2025-12-26
  142. *
  143. */
  144. public function violation_company_export(request $request, ViolationProductModel $violationProductModel)
  145. {
  146. $start_time = request('start_time', '');
  147. $end_time = request('end_time', '');
  148. // 时间条件
  149. $map = [];
  150. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  151. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  152. $result = $violationProductModel->where($map)->where('status', 0)
  153. ->select(['company_name', DB::raw('count(company_name) as count')])->orderby('count', 'desc')
  154. ->groupby('company_name')->get()->toarray();
  155. //执行下载
  156. $oss_url = $this->ViolationCompany_export_download($result);
  157. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $oss_url]);
  158. }
  159. /**
  160. * 禁止挂网公司月度统计-导出下载
  161. * @author 唐远望
  162. * @version 1.0
  163. * @date 2025-12-26
  164. *
  165. */
  166. public function ViolationCompany_export_download($data)
  167. {
  168. // 创建一个新的 Spreadsheet 对象
  169. $spreadsheet = new Spreadsheet();
  170. $sheet = $spreadsheet->getActiveSheet();
  171. //合并单元格
  172. $sheet->mergeCells('A1:B1');
  173. $sheet->setCellValue('A1', '禁止挂网公司月度统计数据(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  174. // 获取合并后的单元格样式对象
  175. $style = $sheet->getStyle('A1');
  176. // 设置水平居中和垂直居中
  177. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  178. // 然后设置行高以适应两行文本
  179. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  180. // 设置表头
  181. $sheet->setCellValue('A2', '公司名称');
  182. $sheet->setCellValue('B2', '挂网数量');
  183. // 填充数据
  184. $row = 3; // 从第3行开始
  185. foreach ($data as $item) {
  186. $sheet->setCellValue('A' . $row, $item['company_name']);
  187. $sheet->setCellValue('B' . $row, $item['count']);
  188. $row++;
  189. }
  190. $file_id = date('YmdHis');
  191. // 创建Excel文件
  192. $filename = '禁止挂网公司月度统计数据' . $file_id . '.xlsx';
  193. $path = public_path('uploads/exports/');
  194. $fullPath = $path. $filename;
  195. if (!is_dir($path)) mkdir($path, 0777, true);
  196. // 生成 Excel 文件
  197. $writer = new Xlsx($spreadsheet);
  198. $writer->save($fullPath);
  199. // 清理
  200. $spreadsheet->disconnectWorksheets();
  201. unset($spreadsheet, $writer);
  202. $Oss = new Oss();
  203. $oss_url = $Oss->uploadFile($filename, $fullPath);
  204. if ($oss_url) @unlink($fullPath);
  205. return $oss_url;
  206. }
  207. /*
  208. * 低价违规挂网链接数统计
  209. * @author 唐远望
  210. * @version 1.0
  211. * @date 2025-12-26
  212. *
  213. */
  214. public function getLowPriceLinkCount(request $request, LowPriceGoodsModel $LowPriceGoodsModel)
  215. {
  216. $request->scene('getLowPriceLinkCount')->validate();
  217. $limit = request('limit', config('page_num', 10));
  218. $start_time = request('start_time', '');
  219. $end_time = request('end_time', '');
  220. // 时间条件
  221. $map = [];
  222. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  223. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  224. $result = $LowPriceGoodsModel->where($map)->where('status', 0)
  225. ->select(['company_name', DB::raw('count(link_url) as count')])->orderby('count', 'desc')
  226. ->groupby('company_name')->paginate($limit);
  227. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  228. }
  229. /**
  230. * 低价违规挂网链接数统计-导出
  231. * @author 唐远望
  232. * @version 1.0
  233. * @date 2025-12-26
  234. *
  235. */
  236. public function low_price_export(request $request, LowPriceGoodsModel $LowPriceGoodsModel)
  237. {
  238. $start_time = request('start_time', '');
  239. $end_time = request('end_time', '');
  240. // 时间条件
  241. $map = [];
  242. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  243. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  244. $result = $LowPriceGoodsModel->where($map)->where('status', 0)
  245. ->select(['company_name', DB::raw('count(link_url) as count')])->orderby('count', 'desc')
  246. ->groupby('company_name')->get()->toarray();
  247. //执行下载
  248. $oss_url = $this->LowPrice_export_download($result);
  249. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $oss_url]);
  250. }
  251. /**
  252. * 低价违规挂网链接数统计-导出下载
  253. * @author 唐远望
  254. * @version 1.0
  255. * @date 2025-12-26
  256. *
  257. */
  258. public function LowPrice_export_download($data)
  259. {
  260. // 创建一个新的 Spreadsheet 对象
  261. $spreadsheet = new Spreadsheet();
  262. $sheet = $spreadsheet->getActiveSheet();
  263. //合并单元格
  264. $sheet->mergeCells('A1:B1');
  265. $sheet->setCellValue('A1', '低价违规挂网链接数统计数据(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  266. // 获取合并后的单元格样式对象
  267. $style = $sheet->getStyle('A1');
  268. // 设置水平居中和垂直居中
  269. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  270. // 然后设置行高以适应两行文本
  271. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  272. // 设置表头
  273. $sheet->setCellValue('A2', '公司名称');
  274. $sheet->setCellValue('B2', '链接挂网数量');
  275. // 填充数据
  276. $row = 3; // 从第3行开始
  277. foreach ($data as $item) {
  278. $sheet->setCellValue('A' . $row, $item['company_name']);
  279. $sheet->setCellValue('B' . $row, $item['count']);
  280. $row++;
  281. }
  282. $file_id = date('YmdHis');
  283. // 创建Excel文件
  284. $filename = '低价违规挂网链接数统计数据' . $file_id . '.xlsx';
  285. $path = public_path('uploads/exports/');
  286. $fullPath = $path. $filename;
  287. if (!is_dir($path)) mkdir($path, 0777, true);
  288. // 生成 Excel 文件
  289. $writer = new Xlsx($spreadsheet);
  290. $writer->save($fullPath);
  291. // 清理
  292. $spreadsheet->disconnectWorksheets();
  293. unset($spreadsheet, $writer);
  294. $Oss = new Oss();
  295. $oss_url = $Oss->uploadFile($filename, $fullPath);
  296. if ($oss_url) @unlink($fullPath);
  297. return $oss_url;
  298. }
  299. /*
  300. * 低价违规挂网公司月度统计
  301. * @author 唐远望
  302. * @version 1.0
  303. * @date 2025-12-26
  304. *
  305. */
  306. public function getLowPriceCompanyCount(request $request, LowPriceGoodsModel $LowPriceGoodsModel)
  307. {
  308. $request->scene('getLowPriceCompanyCount')->validate();
  309. $limit = request('limit', config('page_num', 10));
  310. $start_time = request('start_time', '');
  311. $end_time = request('end_time', '');
  312. // 时间条件
  313. $map = [];
  314. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  315. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  316. $result = $LowPriceGoodsModel->where($map)->where('status', 0)
  317. ->select(['company_name', DB::raw('count(company_name) as count')])->orderby('count', 'desc')
  318. ->groupby('company_name')->paginate($limit);
  319. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  320. }
  321. /**
  322. * 低价违规挂网公司月度统计-导出
  323. * @author 唐远望
  324. * @version 1.0
  325. * @date 2025-12-26
  326. *
  327. */
  328. public function low_price_company_export(request $request, LowPriceGoodsModel $LowPriceGoodsModel)
  329. {
  330. $start_time = request('start_time', '');
  331. $end_time = request('end_time', '');
  332. // 时间条件
  333. $map = [];
  334. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  335. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  336. $result = $LowPriceGoodsModel->where($map)->where('status', 0)
  337. ->select(['company_name', DB::raw('count(company_name) as count')])->orderby('count', 'desc')
  338. ->groupby('company_name')->get()->toarray();
  339. //执行下载
  340. $oss_url = $this->LowPriceCompany_export_download($result);
  341. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $oss_url]);
  342. }
  343. /**
  344. * 低价违规挂网公司月度统计-导出下载
  345. * @author 唐远望
  346. * @version 1.0
  347. * @date 2025-12-26
  348. *
  349. */
  350. public function LowPriceCompany_export_download($data)
  351. {
  352. // 创建一个新的 Spreadsheet 对象
  353. $spreadsheet = new Spreadsheet();
  354. $sheet = $spreadsheet->getActiveSheet();
  355. //合并单元格
  356. $sheet->mergeCells('A1:B1');
  357. $sheet->setCellValue('A1', '低价违规挂网公司月度统计数据(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  358. // 获取合并后的单元格样式对象
  359. $style = $sheet->getStyle('A1');
  360. // 设置水平居中和垂直居中
  361. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  362. // 然后设置行高以适应两行文本
  363. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  364. // 设置表头
  365. $sheet->setCellValue('A2', '公司名称');
  366. $sheet->setCellValue('B2', '挂网数量');
  367. // 填充数据
  368. $row = 3; // 从第3行开始
  369. foreach ($data as $item) {
  370. $sheet->setCellValue('A' . $row, $item['company_name']);
  371. $sheet->setCellValue('B' . $row, $item['count']);
  372. $row++;
  373. }
  374. $file_id = date('YmdHis');
  375. // 创建Excel文件
  376. $filename = '低价违规挂网公司月度统计数据' . $file_id . '.xlsx';
  377. $path = public_path('uploads/exports/');
  378. $fullPath = $path. $filename;
  379. if (!is_dir($path)) mkdir($path, 0777, true);
  380. // 生成 Excel 文件
  381. $writer = new Xlsx($spreadsheet);
  382. $writer->save($fullPath);
  383. // 清理
  384. $spreadsheet->disconnectWorksheets();
  385. unset($spreadsheet, $writer);
  386. $Oss = new Oss();
  387. $oss_url = $Oss->uploadFile($filename, $fullPath);
  388. if ($oss_url) @unlink($fullPath);
  389. return $oss_url;
  390. }
  391. /**
  392. * 商品数量趋势
  393. * @author 唐远望
  394. * @version 1.0
  395. * @date 2025-12-29
  396. *
  397. */
  398. public function get_product_trend(request $request, LowPriceGoodsModel $LowPriceGoodsModel, ViolationProductModel $violationProductModel, LowPriceGoodsMemberModel $LowPriceGoodsMemberModel, ViolationProductMemberModel $ViolationProductMemberModel)
  399. {
  400. $request->scene('get_product_trend')->validate();
  401. // 查询条件
  402. $map = [];
  403. $status = request('status', '');
  404. $start_time = request('start_time', '');
  405. $end_time = request('end_time', '');
  406. $product_name = request('product_name', '');
  407. $product_names = request('product_names', '');
  408. $first_responsible_person = request('first_responsible_person', '');
  409. $responsible_person = request('responsible_person', '');
  410. $platform = request('platform', '');
  411. $company_name = request('company_name', '');
  412. $store_name = request('store_name', '');
  413. $store_names = request('store_names', '');
  414. $source_responsible_person = request('source_responsible_person', '');
  415. $processing_status = request('processing_status', '');
  416. $product_specs = request('product_specs', '');
  417. $online_posting_count = request('online_posting_count', '');
  418. $category_name = request('category_name', '');
  419. // 其他条件
  420. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  421. if ($store_name) $map[] = ['store_name', 'like', "%$store_name%"];
  422. if ($category_name) $map[] = ['category_name', 'like', "%$category_name%"];
  423. //多选平台查询
  424. if ($platform && is_string($platform)) {
  425. $platform = explode(',', $platform);
  426. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('platform', $platform);
  427. $violationProductModel = $violationProductModel->whereIn('platform', $platform);
  428. }
  429. //多选处理状态查询
  430. if ($processing_status && is_string($processing_status)) {
  431. $processing_status = explode(',', $processing_status);
  432. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('processing_status', $processing_status);
  433. $violationProductModel = $violationProductModel->whereIn('processing_status', $processing_status);
  434. }
  435. //多选状态查询
  436. if ($status && is_string($status)) {
  437. $status = explode(',', $status);
  438. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('status', $status);
  439. $violationProductModel = $violationProductModel->whereIn('status', $status);
  440. }
  441. //多选店铺名称查询
  442. if ($store_names && is_string($store_names)) {
  443. $store_names = explode(',', $store_names);
  444. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('store_name', $store_names);
  445. $violationProductModel = $violationProductModel->whereIn('store_name', $store_names);
  446. }
  447. //多选违规挂网次数查询
  448. if ($online_posting_count && is_string($online_posting_count)) {
  449. $online_posting_count = explode(',', $online_posting_count);
  450. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('online_posting_count', $online_posting_count);
  451. $violationProductModel = $violationProductModel->whereIn('online_posting_count', $online_posting_count);
  452. }
  453. //多选规格查询
  454. if ($product_specs && is_string($product_specs)) {
  455. $product_specs = explode(',', $product_specs);
  456. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('product_specs', $product_specs);
  457. $violationProductModel = $violationProductModel->whereIn('product_specs', $product_specs);
  458. }
  459. //多选商品查询
  460. if ($product_names && is_string($product_names)) {
  461. $product_names = explode(',', $product_names);
  462. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('product_name', $product_names);
  463. $violationProductModel = $violationProductModel->whereIn('product_name', $product_names);
  464. }
  465. //多选公司查询
  466. if ($company_name && is_string($company_name)) {
  467. $company_name = explode(',', $company_name);
  468. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('company_name', $company_name);
  469. $violationProductModel = $violationProductModel->whereIn('company_name', $company_name);
  470. }
  471. //多选第一责任人
  472. if ($first_responsible_person && is_string($first_responsible_person)) {
  473. $first_responsible_person = explode(',', $first_responsible_person);
  474. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  475. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  476. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  477. });
  478. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  479. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  480. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  481. });
  482. }
  483. //多选责任人
  484. if ($responsible_person && is_string($responsible_person)) {
  485. $responsible_person = explode(',', $responsible_person);
  486. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  487. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  488. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  489. });
  490. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  491. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  492. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  493. });
  494. }
  495. //多选溯源责任人
  496. if ($source_responsible_person && is_string($source_responsible_person)) {
  497. $source_responsible_person = explode(',', $source_responsible_person);
  498. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  499. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  500. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  501. });
  502. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  503. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  504. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  505. });
  506. }
  507. if ($start_time == '' || $end_time == '') {
  508. $start_time = Carbon::now()->startOfMonth()->getTimestamp(); // 本月开始时间
  509. $end_time = Carbon::today()->endOfDay()->getTimestamp(); // 今天结束时间 23:59:59
  510. } else {
  511. $start_time = strtotime($start_time . ' 00:00:00');
  512. $end_time = strtotime($end_time . ' 23:59:59');
  513. }
  514. $low_price_result = $LowPriceGoodsModel->where('insert_time', '>=', $start_time)
  515. ->where('insert_time', '<=', $end_time)->where($map)
  516. ->select(
  517. DB::raw("FROM_UNIXTIME(insert_time, '%Y-%m-%d') AS date"),
  518. DB::raw('COUNT(id) as daily_total'),
  519. )
  520. ->groupBy('date')
  521. ->orderBy('date')
  522. ->get()->keyBy('date')->toarray();
  523. $violation_product_result = $violationProductModel->where('insert_time', '>=', $start_time)
  524. ->where('insert_time', '<=', $end_time)->where($map)
  525. ->select(
  526. DB::raw("FROM_UNIXTIME(insert_time, '%Y-%m-%d') AS date"),
  527. DB::raw('COUNT(id) as daily_total'),
  528. )
  529. ->groupBy('date')
  530. ->orderBy('date')
  531. ->get()->keyBy('date')->toarray();
  532. $moth_list = [];
  533. $key_index = 0;
  534. for ($i = $end_time; $i >= $start_time; $i -= 86400) {
  535. $key_day = Carbon::createFromTimestamp($i)->format('Y-m-d');
  536. $moth_list[$key_index]['date'] = $key_day;
  537. $moth_list[$key_index]['low_price_totle'] = 0;
  538. $moth_list[$key_index]['violation_product_totle'] = 0;
  539. $moth_list[$key_index]['product_totle'] = 0;
  540. if (isset($low_price_result[$key_day])) {
  541. $moth_list[$key_index]['low_price_totle'] = $low_price_result[$key_day]['daily_total'];
  542. }
  543. if (isset($violation_product_result[$key_day])) {
  544. $moth_list[$key_index]['violation_product_totle'] = $violation_product_result[$key_day]['daily_total'];
  545. }
  546. $moth_list[$key_index]['product_totle'] = $moth_list[$key_index]['low_price_totle'] + $moth_list[$key_index]['violation_product_totle'];
  547. $key_index++;
  548. }
  549. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $moth_list]);
  550. }
  551. /**
  552. * 商品数量趋势-导出
  553. * @author 唐远望
  554. * @version 1.0
  555. * @date 2025-12-29
  556. *
  557. */
  558. public function product_trend_export(request $request, LowPriceGoodsModel $LowPriceGoodsModel, ViolationProductModel $violationProductModel, LowPriceGoodsMemberModel $LowPriceGoodsMemberModel, ViolationProductMemberModel $ViolationProductMemberModel)
  559. {
  560. $request->scene('product_trend_export')->validate();
  561. // 查询条件
  562. $map = [];
  563. $status = request('status', '');
  564. $start_time = request('start_time', '');
  565. $end_time = request('end_time', '');
  566. $product_name = request('product_name', '');
  567. $product_names = request('product_names', '');
  568. $first_responsible_person = request('first_responsible_person', '');
  569. $responsible_person = request('responsible_person', '');
  570. $platform = request('platform', '');
  571. $company_name = request('company_name', '');
  572. $store_name = request('store_name', '');
  573. $store_names = request('store_names', '');
  574. $source_responsible_person = request('source_responsible_person', '');
  575. $processing_status = request('processing_status', '');
  576. $product_specs = request('product_specs', '');
  577. $online_posting_count = request('online_posting_count', '');
  578. $category_name = request('category_name', '');
  579. // 其他条件
  580. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  581. if ($store_name) $map[] = ['store_name', 'like', "%$store_name%"];
  582. if ($category_name) $map[] = ['category_name', 'like', "%$category_name%"];
  583. //多选平台查询
  584. if ($platform && is_string($platform)) {
  585. $platform = explode(',', $platform);
  586. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('platform', $platform);
  587. $violationProductModel = $violationProductModel->whereIn('platform', $platform);
  588. }
  589. //多选处理状态查询
  590. if ($processing_status && is_string($processing_status)) {
  591. $processing_status = explode(',', $processing_status);
  592. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('processing_status', $processing_status);
  593. $violationProductModel = $violationProductModel->whereIn('processing_status', $processing_status);
  594. }
  595. //多选状态查询
  596. if ($status && is_string($status)) {
  597. $status = explode(',', $status);
  598. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('status', $status);
  599. $violationProductModel = $violationProductModel->whereIn('status', $status);
  600. }
  601. //多选店铺名称查询
  602. if ($store_names && is_string($store_names)) {
  603. $store_names = explode(',', $store_names);
  604. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('store_name', $store_names);
  605. $violationProductModel = $violationProductModel->whereIn('store_name', $store_names);
  606. }
  607. //多选违规挂网次数查询
  608. if ($online_posting_count && is_string($online_posting_count)) {
  609. $online_posting_count = explode(',', $online_posting_count);
  610. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('online_posting_count', $online_posting_count);
  611. $violationProductModel = $violationProductModel->whereIn('online_posting_count', $online_posting_count);
  612. }
  613. //多选规格查询
  614. if ($product_specs && is_string($product_specs)) {
  615. $product_specs = explode(',', $product_specs);
  616. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('product_specs', $product_specs);
  617. $violationProductModel = $violationProductModel->whereIn('product_specs', $product_specs);
  618. }
  619. //多选商品查询
  620. if ($product_names && is_string($product_names)) {
  621. $product_names = explode(',', $product_names);
  622. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('product_name', $product_names);
  623. $violationProductModel = $violationProductModel->whereIn('product_name', $product_names);
  624. }
  625. //多选公司查询
  626. if ($company_name && is_string($company_name)) {
  627. $company_name = explode(',', $company_name);
  628. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('company_name', $company_name);
  629. $violationProductModel = $violationProductModel->whereIn('company_name', $company_name);
  630. }
  631. //多选第一责任人
  632. if ($first_responsible_person && is_string($first_responsible_person)) {
  633. $first_responsible_person = explode(',', $first_responsible_person);
  634. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  635. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  636. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  637. });
  638. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  639. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  640. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  641. });
  642. }
  643. //多选责任人
  644. if ($responsible_person && is_string($responsible_person)) {
  645. $responsible_person = explode(',', $responsible_person);
  646. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  647. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  648. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  649. });
  650. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  651. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  652. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  653. });
  654. }
  655. //多选溯源责任人
  656. if ($source_responsible_person && is_string($source_responsible_person)) {
  657. $source_responsible_person = explode(',', $source_responsible_person);
  658. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  659. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  660. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  661. });
  662. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  663. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  664. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  665. });
  666. }
  667. if ($start_time == '' || $end_time == '') {
  668. $start_time = Carbon::now()->startOfMonth()->getTimestamp(); // 本月开始时间
  669. $end_time = Carbon::today()->endOfDay()->getTimestamp(); // 今天结束时间 23:59:59
  670. } else {
  671. $start_time = strtotime($start_time . ' 00:00:00');
  672. $end_time = strtotime($end_time . ' 23:59:59');
  673. }
  674. $low_price_result = $LowPriceGoodsModel->where('insert_time', '>=', $start_time)
  675. ->where('insert_time', '<=', $end_time)->where($map)
  676. ->select(
  677. DB::raw("FROM_UNIXTIME(insert_time, '%Y-%m-%d') AS date"),
  678. DB::raw('COUNT(id) as daily_total'),
  679. )
  680. ->groupBy('date')
  681. ->orderBy('date')
  682. ->get()->keyBy('date')->toarray();
  683. $violation_product_result = $violationProductModel->where('insert_time', '>=', $start_time)
  684. ->where('insert_time', '<=', $end_time)->where($map)
  685. ->select(
  686. DB::raw("FROM_UNIXTIME(insert_time, '%Y-%m-%d') AS date"),
  687. DB::raw('COUNT(id) as daily_total'),
  688. )
  689. ->groupBy('date')
  690. ->orderBy('date')
  691. ->get()->keyBy('date')->toarray();
  692. $moth_list = [];
  693. $key_index = 0;
  694. for ($i = $end_time; $i >= $start_time; $i -= 86400) {
  695. $key_day = Carbon::createFromTimestamp($i)->format('Y-m-d');
  696. $moth_list[$key_index]['date'] = $key_day;
  697. $moth_list[$key_index]['low_price_totle'] = 0;
  698. $moth_list[$key_index]['violation_product_totle'] = 0;
  699. $moth_list[$key_index]['product_totle'] = 0;
  700. if (isset($low_price_result[$key_day])) {
  701. $moth_list[$key_index]['low_price_totle'] = $low_price_result[$key_day]['daily_total'];
  702. }
  703. if (isset($violation_product_result[$key_day])) {
  704. $moth_list[$key_index]['violation_product_totle'] = $violation_product_result[$key_day]['daily_total'];
  705. }
  706. $moth_list[$key_index]['product_totle'] = $moth_list[$key_index]['low_price_totle'] + $moth_list[$key_index]['violation_product_totle'];
  707. $key_index++;
  708. }
  709. //执行下载
  710. $oss_url = $this->product_trend_export_download($moth_list);
  711. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $oss_url]);
  712. }
  713. /**
  714. * 商品数量趋势-导出下载
  715. * @author 唐远望
  716. * @version 1.0
  717. * @date 2025-12-29
  718. *
  719. */
  720. public function product_trend_export_download($data)
  721. {
  722. // 创建一个新的 Spreadsheet 对象
  723. $spreadsheet = new Spreadsheet();
  724. $sheet = $spreadsheet->getActiveSheet();
  725. //合并单元格
  726. $sheet->mergeCells('A1:D1');
  727. $sheet->setCellValue('A1', '商品数量趋势统计数据(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  728. // 获取合并后的单元格样式对象
  729. $style = $sheet->getStyle('A1');
  730. // 设置水平居中和垂直居中
  731. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  732. // 然后设置行高以适应两行文本
  733. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  734. // 设置表头
  735. $sheet->setCellValue('A2', '日期');
  736. $sheet->setCellValue('B2', '低价商品数量');
  737. $sheet->setCellValue('C2', '禁止商品数量');
  738. $sheet->setCellValue('D2', '总商品数量');
  739. // 填充数据
  740. $row = 3; // 从第3行开始
  741. foreach ($data as $item) {
  742. $sheet->setCellValue('A' . $row, $item['date']);
  743. $sheet->setCellValue('B' . $row, $item['low_price_totle']);
  744. $sheet->setCellValue('C' . $row, $item['violation_product_totle']);
  745. $sheet->setCellValue('D' . $row, $item['product_totle']);
  746. $row++;
  747. }
  748. $file_id = date('YmdHis');
  749. // 创建Excel文件
  750. $filename = '商品数量趋势统计数据' . $file_id . '.xlsx';
  751. $path = public_path('uploads/exports/');
  752. $fullPath = $path. $filename;
  753. if (!is_dir($path)) mkdir($path, 0777, true);
  754. // 生成 Excel 文件
  755. $writer = new Xlsx($spreadsheet);
  756. $writer->save($fullPath);
  757. // 清理
  758. $spreadsheet->disconnectWorksheets();
  759. unset($spreadsheet, $writer);
  760. $Oss = new Oss();
  761. $oss_url = $Oss->uploadFile($filename, $fullPath);
  762. if ($oss_url) @unlink($fullPath);
  763. return $oss_url;
  764. }
  765. /**
  766. * 商家数量趋势
  767. * @author 唐远望
  768. * @version 1.0
  769. * @date 2025-12-29
  770. *
  771. */
  772. public function get_store_trend(Request $request, LowPriceGoodsModel $LowPriceGoodsModel, ViolationProductModel $violationProductModel, LowPriceGoodsMemberModel $LowPriceGoodsMemberModel, ViolationProductMemberModel $ViolationProductMemberModel)
  773. {
  774. $request->scene('get_store_trend')->validate();
  775. // 查询条件
  776. $map = [];
  777. $status = request('status', '');
  778. $start_time = request('start_time', '');
  779. $end_time = request('end_time', '');
  780. $product_name = request('product_name', '');
  781. $product_names = request('product_names', '');
  782. $first_responsible_person = request('first_responsible_person', '');
  783. $responsible_person = request('responsible_person', '');
  784. $platform = request('platform', '');
  785. $company_name = request('company_name', '');
  786. $store_name = request('store_name', '');
  787. $store_names = request('store_names', '');
  788. $source_responsible_person = request('source_responsible_person', '');
  789. $processing_status = request('processing_status', '');
  790. $product_specs = request('product_specs', '');
  791. $online_posting_count = request('online_posting_count', '');
  792. $category_name = request('category_name', '');
  793. // 其他条件
  794. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  795. if ($store_name) $map[] = ['store_name', 'like', "%$store_name%"];
  796. if ($category_name) $map[] = ['category_name', 'like', "%$category_name%"];
  797. //多选平台查询
  798. if ($platform && is_string($platform)) {
  799. $platform = explode(',', $platform);
  800. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('platform', $platform);
  801. $violationProductModel = $violationProductModel->whereIn('platform', $platform);
  802. }
  803. //多选处理状态查询
  804. if ($processing_status && is_string($processing_status)) {
  805. $processing_status = explode(',', $processing_status);
  806. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('processing_status', $processing_status);
  807. $violationProductModel = $violationProductModel->whereIn('processing_status', $processing_status);
  808. }
  809. //多选状态查询
  810. if ($status && is_string($status)) {
  811. $status = explode(',', $status);
  812. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('status', $status);
  813. $violationProductModel = $violationProductModel->whereIn('status', $status);
  814. }
  815. //多选店铺名称查询
  816. if ($store_names && is_string($store_names)) {
  817. $store_names = explode(',', $store_names);
  818. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('store_name', $store_names);
  819. $violationProductModel = $violationProductModel->whereIn('store_name', $store_names);
  820. }
  821. //多选违规挂网次数查询
  822. if ($online_posting_count && is_string($online_posting_count)) {
  823. $online_posting_count = explode(',', $online_posting_count);
  824. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('online_posting_count', $online_posting_count);
  825. $violationProductModel = $violationProductModel->whereIn('online_posting_count', $online_posting_count);
  826. }
  827. //多选规格查询
  828. if ($product_specs && is_string($product_specs)) {
  829. $product_specs = explode(',', $product_specs);
  830. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('product_specs', $product_specs);
  831. $violationProductModel = $violationProductModel->whereIn('product_specs', $product_specs);
  832. }
  833. //多选商品查询
  834. if ($product_names && is_string($product_names)) {
  835. $product_names = explode(',', $product_names);
  836. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('product_name', $product_names);
  837. $violationProductModel = $violationProductModel->whereIn('product_name', $product_names);
  838. }
  839. //多选公司查询
  840. if ($company_name && is_string($company_name)) {
  841. $company_name = explode(',', $company_name);
  842. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('company_name', $company_name);
  843. $violationProductModel = $violationProductModel->whereIn('company_name', $company_name);
  844. }
  845. //多选第一责任人
  846. if ($first_responsible_person && is_string($first_responsible_person)) {
  847. $first_responsible_person = explode(',', $first_responsible_person);
  848. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  849. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  850. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  851. });
  852. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  853. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  854. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  855. });
  856. }
  857. //多选责任人
  858. if ($responsible_person && is_string($responsible_person)) {
  859. $responsible_person = explode(',', $responsible_person);
  860. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  861. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  862. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  863. });
  864. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  865. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  866. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  867. });
  868. }
  869. //多选溯源责任人
  870. if ($source_responsible_person && is_string($source_responsible_person)) {
  871. $source_responsible_person = explode(',', $source_responsible_person);
  872. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  873. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  874. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  875. });
  876. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  877. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  878. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  879. });
  880. }
  881. if ($start_time == '' || $end_time == '') {
  882. $start_time = Carbon::now()->startOfMonth()->getTimestamp(); // 本月开始时间
  883. $end_time = Carbon::today()->endOfDay()->getTimestamp(); // 今天结束时间 23:59:59
  884. } else {
  885. $start_time = strtotime($start_time . ' 00:00:00');
  886. $end_time = strtotime($end_time . ' 23:59:59');
  887. }
  888. $low_price_result = $LowPriceGoodsModel->where('insert_time', '>=', $start_time)
  889. ->where('insert_time', '<=', $end_time)->where($map)
  890. ->select(
  891. DB::raw("FROM_UNIXTIME(insert_time, '%Y-%m-%d') AS date"),
  892. DB::raw('COUNT(DISTINCT store_name) as daily_total'),
  893. )
  894. ->groupBy('date')
  895. ->orderBy('date')
  896. ->get()->keyBy('date')->toarray();
  897. $violation_product_result = $violationProductModel->where('insert_time', '>=', $start_time)
  898. ->where('insert_time', '<=', $end_time)->where($map)
  899. ->select(
  900. DB::raw("FROM_UNIXTIME(insert_time, '%Y-%m-%d') AS date"),
  901. DB::raw('COUNT(DISTINCT store_name) as daily_total'),
  902. )
  903. ->groupBy('date')
  904. ->orderBy('date')
  905. ->get()->keyBy('date')->toarray();
  906. $moth_list = [];
  907. $key_index = 0;
  908. for ($i = $end_time; $i >= $start_time; $i -= 86400) {
  909. $key_day = Carbon::createFromTimestamp($i)->format('Y-m-d');
  910. $moth_list[$key_index]['date'] = $key_day;
  911. $moth_list[$key_index]['low_price_store_totle'] = 0;
  912. $moth_list[$key_index]['violation_product_store_totle'] = 0;
  913. $moth_list[$key_index]['product_store_totle'] = 0;
  914. if (isset($low_price_result[$key_day])) {
  915. $moth_list[$key_index]['low_price_store_totle'] = $low_price_result[$key_day]['daily_total'];
  916. }
  917. if (isset($violation_product_result[$key_day])) {
  918. $moth_list[$key_index]['violation_product_store_totle'] = $violation_product_result[$key_day]['daily_total'];
  919. }
  920. $moth_list[$key_index]['product_store_totle'] = $moth_list[$key_index]['low_price_store_totle'] + $moth_list[$key_index]['violation_product_store_totle'];
  921. $key_index++;
  922. }
  923. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $moth_list]);
  924. }
  925. /**
  926. * 商家数量趋势-导出
  927. * @author 唐远望
  928. * @version 1.0
  929. * @date 2025-12-29
  930. *
  931. */
  932. public function store_trend_export(Request $request, LowPriceGoodsModel $LowPriceGoodsModel, ViolationProductModel $violationProductModel, LowPriceGoodsMemberModel $LowPriceGoodsMemberModel, ViolationProductMemberModel $ViolationProductMemberModel)
  933. {
  934. $request->scene('store_trend_export')->validate();
  935. // 查询条件
  936. $map = [];
  937. $status = request('status', '');
  938. $start_time = request('start_time', '');
  939. $end_time = request('end_time', '');
  940. $product_name = request('product_name', '');
  941. $product_names = request('product_names', '');
  942. $first_responsible_person = request('first_responsible_person', '');
  943. $responsible_person = request('responsible_person', '');
  944. $platform = request('platform', '');
  945. $company_name = request('company_name', '');
  946. $store_name = request('store_name', '');
  947. $store_names = request('store_names', '');
  948. $source_responsible_person = request('source_responsible_person', '');
  949. $processing_status = request('processing_status', '');
  950. $product_specs = request('product_specs', '');
  951. $online_posting_count = request('online_posting_count', '');
  952. $category_name = request('category_name', '');
  953. // 其他条件
  954. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  955. if ($store_name) $map[] = ['store_name', 'like', "%$store_name%"];
  956. if ($category_name) $map[] = ['category_name', 'like', "%$category_name%"];
  957. //多选平台查询
  958. if ($platform && is_string($platform)) {
  959. $platform = explode(',', $platform);
  960. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('platform', $platform);
  961. $violationProductModel = $violationProductModel->whereIn('platform', $platform);
  962. }
  963. //多选处理状态查询
  964. if ($processing_status && is_string($processing_status)) {
  965. $processing_status = explode(',', $processing_status);
  966. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('processing_status', $processing_status);
  967. $violationProductModel = $violationProductModel->whereIn('processing_status', $processing_status);
  968. }
  969. //多选状态查询
  970. if ($status && is_string($status)) {
  971. $status = explode(',', $status);
  972. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('status', $status);
  973. $violationProductModel = $violationProductModel->whereIn('status', $status);
  974. }
  975. //多选店铺名称查询
  976. if ($store_names && is_string($store_names)) {
  977. $store_names = explode(',', $store_names);
  978. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('store_name', $store_names);
  979. $violationProductModel = $violationProductModel->whereIn('store_name', $store_names);
  980. }
  981. //多选违规挂网次数查询
  982. if ($online_posting_count && is_string($online_posting_count)) {
  983. $online_posting_count = explode(',', $online_posting_count);
  984. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('online_posting_count', $online_posting_count);
  985. $violationProductModel = $violationProductModel->whereIn('online_posting_count', $online_posting_count);
  986. }
  987. //多选规格查询
  988. if ($product_specs && is_string($product_specs)) {
  989. $product_specs = explode(',', $product_specs);
  990. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('product_specs', $product_specs);
  991. $violationProductModel = $violationProductModel->whereIn('product_specs', $product_specs);
  992. }
  993. //多选商品查询
  994. if ($product_names && is_string($product_names)) {
  995. $product_names = explode(',', $product_names);
  996. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('product_name', $product_names);
  997. $violationProductModel = $violationProductModel->whereIn('product_name', $product_names);
  998. }
  999. //多选公司查询
  1000. if ($company_name && is_string($company_name)) {
  1001. $company_name = explode(',', $company_name);
  1002. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('company_name', $company_name);
  1003. $violationProductModel = $violationProductModel->whereIn('company_name', $company_name);
  1004. }
  1005. //多选第一责任人
  1006. if ($first_responsible_person && is_string($first_responsible_person)) {
  1007. $first_responsible_person = explode(',', $first_responsible_person);
  1008. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  1009. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  1010. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  1011. });
  1012. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $first_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  1013. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  1014. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  1015. });
  1016. }
  1017. //多选责任人
  1018. if ($responsible_person && is_string($responsible_person)) {
  1019. $responsible_person = explode(',', $responsible_person);
  1020. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  1021. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  1022. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  1023. });
  1024. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  1025. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  1026. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  1027. });
  1028. }
  1029. //多选溯源责任人
  1030. if ($source_responsible_person && is_string($source_responsible_person)) {
  1031. $source_responsible_person = explode(',', $source_responsible_person);
  1032. $subQuery = $LowPriceGoodsMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  1033. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query1) use ($subQuery) {
  1034. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  1035. });
  1036. $subQuery = $ViolationProductMemberModel->whereIn('employee_id', $source_responsible_person)->distinct('lowprice_product_logid')->select('lowprice_product_logid');
  1037. $violationProductModel = $violationProductModel->whereIn('id', function ($query1) use ($subQuery) {
  1038. $query1->select('lowprice_product_logid')->fromSub($subQuery, 'sub1');
  1039. });
  1040. }
  1041. if ($start_time == '' || $end_time == '') {
  1042. $start_time = Carbon::now()->startOfMonth()->getTimestamp(); // 本月开始时间
  1043. $end_time = Carbon::today()->endOfDay()->getTimestamp(); // 今天结束时间 23:59:59
  1044. } else {
  1045. $start_time = strtotime($start_time . ' 00:00:00');
  1046. $end_time = strtotime($end_time . ' 23:59:59');
  1047. }
  1048. $low_price_result = $LowPriceGoodsModel->where('insert_time', '>=', $start_time)
  1049. ->where('insert_time', '<=', $end_time)->where($map)
  1050. ->select(
  1051. DB::raw("FROM_UNIXTIME(insert_time, '%Y-%m-%d') AS date"),
  1052. DB::raw('COUNT(DISTINCT store_name) as daily_total'),
  1053. )
  1054. ->groupBy('date')
  1055. ->orderBy('date')
  1056. ->get()->keyBy('date')->toarray();
  1057. $violation_product_result = $violationProductModel->where('insert_time', '>=', $start_time)
  1058. ->where('insert_time', '<=', $end_time)->where($map)
  1059. ->select(
  1060. DB::raw("FROM_UNIXTIME(insert_time, '%Y-%m-%d') AS date"),
  1061. DB::raw('COUNT(DISTINCT store_name) as daily_total'),
  1062. )
  1063. ->groupBy('date')
  1064. ->orderBy('date')
  1065. ->get()->keyBy('date')->toarray();
  1066. $moth_list = [];
  1067. $key_index = 0;
  1068. for ($i = $end_time; $i >= $start_time; $i -= 86400) {
  1069. $key_day = Carbon::createFromTimestamp($i)->format('Y-m-d');
  1070. $moth_list[$key_index]['date'] = $key_day;
  1071. $moth_list[$key_index]['low_price_store_totle'] = 0;
  1072. $moth_list[$key_index]['violation_product_store_totle'] = 0;
  1073. $moth_list[$key_index]['product_store_totle'] = 0;
  1074. if (isset($low_price_result[$key_day])) {
  1075. $moth_list[$key_index]['low_price_store_totle'] = $low_price_result[$key_day]['daily_total'];
  1076. }
  1077. if (isset($violation_product_result[$key_day])) {
  1078. $moth_list[$key_index]['violation_product_store_totle'] = $violation_product_result[$key_day]['daily_total'];
  1079. }
  1080. $moth_list[$key_index]['product_store_totle'] = $moth_list[$key_index]['low_price_store_totle'] + $moth_list[$key_index]['violation_product_store_totle'];
  1081. $key_index++;
  1082. }
  1083. // 执行下载
  1084. $oss_url = $this->store_trend_export_download($moth_list);
  1085. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $oss_url]);
  1086. }
  1087. /**
  1088. * 商家数量趋势-导出下载
  1089. * @author 唐远望
  1090. * @version 1.0
  1091. * @date 2025-12-29
  1092. *
  1093. */
  1094. public function store_trend_export_download($data)
  1095. {
  1096. // 创建一个新的 Spreadsheet 对象
  1097. $spreadsheet = new Spreadsheet();
  1098. $sheet = $spreadsheet->getActiveSheet();
  1099. //合并单元格
  1100. $sheet->mergeCells('A1:D1');
  1101. $sheet->setCellValue('A1', '商家数量趋势统计数据(导出时间:' . date('Y-m-d H:i:s', time()) . ')'); // 设置合并后的单元格内容
  1102. // 获取合并后的单元格样式对象
  1103. $style = $sheet->getStyle('A1');
  1104. // 设置水平居中和垂直居中
  1105. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  1106. // 然后设置行高以适应两行文本
  1107. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  1108. // 设置表头
  1109. $sheet->setCellValue('A2', '日期');
  1110. $sheet->setCellValue('B2', '低价商品商家数量');
  1111. $sheet->setCellValue('C2', '禁止商品商家数量');
  1112. $sheet->setCellValue('D2', '总商品商家数量');
  1113. // 填充数据
  1114. $row = 3; // 从第3行开始
  1115. foreach ($data as $item) {
  1116. $sheet->setCellValue('A' . $row, $item['date']);
  1117. $sheet->setCellValue('B' . $row, $item['low_price_store_totle']);
  1118. $sheet->setCellValue('C' . $row, $item['violation_product_store_totle']);
  1119. $sheet->setCellValue('D' . $row, $item['product_store_totle']);
  1120. $row++;
  1121. }
  1122. $file_id = date('YmdHis');
  1123. // 创建Excel文件
  1124. $filename = '商家数量趋势统计数据_' . $file_id . '.xlsx';
  1125. $path = public_path('uploads/exports/');
  1126. $fullPath = $path. $filename;
  1127. if (!is_dir($path)) mkdir($path, 0777, true);
  1128. // 生成 Excel 文件
  1129. $writer = new Xlsx($spreadsheet);
  1130. $writer->save($fullPath);
  1131. // 清理
  1132. $spreadsheet->disconnectWorksheets();
  1133. unset($spreadsheet, $writer);
  1134. $Oss = new Oss();
  1135. $oss_url = $Oss->uploadFile($filename, $fullPath);
  1136. if ($oss_url) @unlink($fullPath);
  1137. return $oss_url;
  1138. }
  1139. }