NowReportHandle.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use app\admin\model\Test1;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\Output;
  8. use think\facade\Cache;
  9. use think\facade\Db;
  10. //处理报表预约记录,生成报表文件
  11. class NowReportHandle extends Command
  12. {
  13. //redis队列的key,在ReportReserve控制器中也有定义,要同步修改(轻易不要修改)
  14. private $key = 'wanyuhengtong_report_reserve';
  15. //各个报表的导出字段(这里的字段要和下面的方法中查询字段个数对应)
  16. private $field = [
  17. 'A' => ['退货单号', '流程进度', '退货发起日期', '公司名称', '退货人', '退货数量', '退货销售货款', '退货备注', '客户名称', '客户编码', '一级组织', '二级组织', '三级组织', '平台名称', 'PO编号', '确认单编号', '确认单类型', '确认单下单时间', '业务人员', '确认单产品编号', '产品名称', '产品编码', '一级分类'],
  18. 'B' => [],
  19. 'C' => [],
  20. ];
  21. protected function configure()
  22. {
  23. // 指令配置
  24. $this->setName('handleData')
  25. ->setDescription('处理报表预约记录,生成报表文件');
  26. }
  27. //处理报表预约记录,生成报表文件
  28. protected function execute(Input $input, Output $output)
  29. {
  30. try {
  31. $pk_id = Cache::store('redis')->handler()->rpop($this->key);
  32. if ($pk_id) {
  33. //有数据,开始处理
  34. //查询预约记录
  35. $rs = Db::name('report_form_reserve')
  36. ->field('id,name,code,start_time,end_time')
  37. ->where(['id' => $pk_id, 'status' => 1])
  38. ->find();
  39. if ($rs) {
  40. //状态改为处理中
  41. Db::name('report_form_reserve')
  42. ->where(['id' => $pk_id, 'status' => 1])//status==1 待处理
  43. ->update(['status' => 2, 'updatetime' => date('Y-m-d H:i:s')]);//status==2 处理中
  44. //处理数据的方法
  45. switch ($rs['code']) {
  46. case 'A':
  47. $res = $this->A($rs['start_time'], $rs['end_time']);
  48. break;
  49. }
  50. $file = excelSaveFile($res, $this->field[$rs['code']], $rs['name'] . date('YmdHis'));
  51. Db::name('report_form_reserve')
  52. ->where(['id' => $pk_id, 'status' => 2])//status==2 处理中
  53. ->update([
  54. 'status' => 3, //status==3 处理完成
  55. 'down_url' => $file,
  56. 'updatetime' => date('Y-m-d H:i:s'),
  57. 'expiretime' => date('Y-m-d H:i:s', strtotime('+1 month'))
  58. ]);
  59. $output->writeln('【' . $pk_id . '】该预约记录处理成功');
  60. } else $output->writeln('没有查询到该预约记录');
  61. } else $output->writeln('没有可供处理的报表预约记录');
  62. } catch (\Exception $exception) {
  63. $output->writeln($exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  64. }
  65. }
  66. //数据处理脚本,每个报表一个方法
  67. //退货台账-业务口径
  68. private function A(string $start_date = '', string $end_date = '')
  69. {
  70. $data = Db::name('sale_return')
  71. ->alias('sr')
  72. ->field('sr.returnCode 退货单号,sr.status 流程进度,sr.addtime 退货发起日期,sup.name 公司名称,sr.apply_name 退货人,sr.num 退货数量,sr.total_fee 退货销售货款,sr.remark 退货备注,c.companyName 客户名称,or.customer_code 客户编码,"" 一级组织,"" 二级组织,"" 三级组织,p.platform_name 平台名称,s.poNo PO编号,sr.orderCode 确认单编号,s.order_type 确认单类型,s.addtime 确认单下单时间,s.apply_name 业务人员,sr.orderCode 确认单产品编号,sr.good_name 产品名称,sr.good_code 产品编码,"" 一级分类,s.cat_id,c.itemid')
  73. ->leftJoin('platform p', 'p.id = sr.platform_id')
  74. ->leftJoin('order_return or', 'or.orderCode = sr.orderCode')
  75. ->leftJoin('customer_info c', 'c.companyNo = or.customer_code')
  76. ->leftJoin('sale s', 's.orderCode = sr.orderCode')
  77. ->leftJoin('supplier sup', 'sup.code = s.supplierNo')
  78. ->where('sr.is_del', 0)
  79. ->whereBetween('sr.addtime', [$start_date . ' 00:00:00', $end_date . ' 23:59:59'])
  80. ->cursor();
  81. $all_sale_return_status = [1 => '待业务审批', 2 => '待专员审批', 3 => '待主管审批', 4 => '退货完成', 5 => '业务驳回', 6 => '采购驳回', 7 => '专员审批不通过'];
  82. $all_sale_order_type = [1 => '备库', 2 => '非库存', 3 => '咨询采反', 4 => '项目采反', 5 => '平台部订单销售',];
  83. $list = [];
  84. foreach ($data as $value) {
  85. $value['流程进度'] = isset($all_sale_return_status[$value['流程进度']]) ? $all_sale_return_status[$value['流程进度']] : '';
  86. $value['确认单类型'] = isset($all_sale_order_type[$value['确认单类型']]) ? $all_sale_order_type[$value['确认单类型']] : '';
  87. $top = made($value['cat_id']);
  88. $value['一级分类'] = isset($top[0]['name']) ? $top[0]['name'] : '';
  89. if (!empty($value['itemid'])) {
  90. $customer_org1 = get_top_customer_org($value['itemid']);
  91. foreach ($customer_org1 as $vv) {
  92. switch ($vv['level']) {
  93. case 1:
  94. $value['一级组织'] = $vv['name'];
  95. break;
  96. case 2:
  97. $value['二级组织'] = $vv['name'];
  98. break;
  99. case 3:
  100. $value['三级组织'] = $vv['name'];
  101. break;
  102. }
  103. }
  104. }
  105. unset($value['cat_id']);
  106. unset($value['itemid']);
  107. yield $list[] = $value;
  108. }
  109. return $list;
  110. }
  111. //库存预警汇总表
  112. private function B(string $start_date = '', string $end_date = '')
  113. {
  114. $data = Db::name('sale_return')
  115. ->alias('sr')
  116. ->field('sr.returnCode 退货单号,sr.status 流程进度,sr.addtime 退货发起日期,sup.name 公司名称,sr.apply_name 退货人,sr.num 退货数量,sr.total_fee 退货销售货款,sr.remark 退货备注,c.companyName 客户名称,or.customer_code 客户编码,"" 一级组织,"" 二级组织,"" 三级组织,p.platform_name 平台名称,s.poNo PO编号,sr.orderCode 确认单编号,s.order_type 确认单类型,s.addtime 确认单下单时间,s.apply_name 业务人员,sr.orderCode 确认单产品编号,sr.good_name 产品名称,sr.good_code 产品编码,"" 一级分类,s.cat_id,c.itemid')
  117. ->leftJoin('platform p', 'p.id = sr.platform_id')
  118. ->leftJoin('order_return or', 'or.orderCode = sr.orderCode')
  119. ->leftJoin('customer_info c', 'c.companyNo = or.customer_code')
  120. ->leftJoin('sale s', 's.orderCode = sr.orderCode')
  121. ->leftJoin('supplier sup', 'sup.code = s.supplierNo')
  122. ->where('sr.is_del', 0)
  123. ->whereBetween('sr.addtime', [$start_date . ' 00:00:00', $end_date . ' 23:59:59'])
  124. ->cursor();
  125. $all_sale_return_status = [1 => '待业务审批', 2 => '待专员审批', 3 => '待主管审批', 4 => '退货完成', 5 => '业务驳回', 6 => '采购驳回', 7 => '专员审批不通过'];
  126. $all_sale_order_type = [1 => '备库', 2 => '非库存', 3 => '咨询采反', 4 => '项目采反', 5 => '平台部订单销售',];
  127. $list = [];
  128. foreach ($data as $value) {
  129. $value['流程进度'] = isset($all_sale_return_status[$value['流程进度']]) ? $all_sale_return_status[$value['流程进度']] : '';
  130. $value['确认单类型'] = isset($all_sale_order_type[$value['确认单类型']]) ? $all_sale_order_type[$value['确认单类型']] : '';
  131. $top = made($value['cat_id']);
  132. $value['一级分类'] = isset($top[0]['name']) ? $top[0]['name'] : '';
  133. if (!empty($value['itemid'])) {
  134. $customer_org1 = get_top_customer_org($value['itemid']);
  135. foreach ($customer_org1 as $vv) {
  136. switch ($vv['level']) {
  137. case 1:
  138. $value['一级组织'] = $vv['name'];
  139. break;
  140. case 2:
  141. $value['二级组织'] = $vv['name'];
  142. break;
  143. case 3:
  144. $value['三级组织'] = $vv['name'];
  145. break;
  146. }
  147. }
  148. }
  149. unset($value['cat_id']);
  150. unset($value['itemid']);
  151. yield $list[] = $value;
  152. }
  153. return $list;
  154. }
  155. }