OverviewPanel.php 74 KB

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