OverviewPanel.php 69 KB

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