OrderSuperLoss.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Cache;
  5. use think\Exception;
  6. use think\Log;
  7. use \think\Request;
  8. use \think\Db;
  9. /**
  10. * 报表接口
  11. */
  12. class OrderSuperLoss extends Api
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 首页-已核查成品编号
  18. *
  19. */
  20. public function index()
  21. {
  22. $this->success('请求成功');
  23. }
  24. /**
  25. * 获取工单接单日期的年月列表(左侧菜单栏)
  26. * @return void
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\exception\DbException
  30. */
  31. public function getWorkOrderDates()
  32. {
  33. if ($this->request->isGet() === false) {
  34. $this->error('请求错误');
  35. }
  36. $list = \think\Db::table('工单_基本资料')
  37. ->whereNull('Mod_rq') // 排除 Mod_rq 不为 null 的数据
  38. ->where('落货日期', '<>', '') // 排除空值
  39. ->field(\think\Db::raw('DATE_FORMAT(落货日期, "%Y-%m") as 年月'))
  40. ->group('年月')
  41. ->order('年月', 'desc') // 按年月倒序排列,最新的在前面
  42. ->select();
  43. if (empty($list)) {
  44. $this->success('', []);
  45. }
  46. // 提取年月值组成数组
  47. $data = [];
  48. foreach ($list as $item) {
  49. $data[] = $item['年月'];
  50. }
  51. $this->success('成功', $data);
  52. }
  53. /**
  54. * 根据月份获取工单数据(排除已修改记录)
  55. * @return void
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. */
  60. public function getWorkOrdersByMonth()
  61. {
  62. if ($this->request->isGet() === false) {
  63. $this->error('请求错误');
  64. }
  65. $rq = $this->request->get('rq');
  66. if (empty($rq) || !preg_match('/^\d{4}-\d{2}$/', $rq)) {
  67. $this->error('参数格式错误,请使用 YYYY-MM 格式');
  68. }
  69. // 生成缓存key
  70. $cacheKey = 'work_orders_' . $rq;
  71. // 尝试从缓存获取
  72. $cachedData = cache($cacheKey);
  73. if ($cachedData !== false) {
  74. $this->success('成功', $cachedData);
  75. return;
  76. }
  77. // 计算月份起止日期
  78. list($year, $month) = explode('-', $rq);
  79. $start = "{$year}-{$month}-01";
  80. $end = date('Y-m-d', strtotime("{$year}-{$month}-01 +1 month"));
  81. // 使用原生SQL查询,避免框架对中文字段名的校验
  82. $sql = "SELECT
  83. g.Uniqid,
  84. g.订单编号,
  85. g.审核 as 核批,
  86. g.审核日期 as 核批日期,
  87. g.接单日期 as 下单日期,
  88. g.落货日期 as 货期,
  89. g.生产款号 as 款号,
  90. g.订单数量,
  91. g.工单入仓数量 as 入库数量,
  92. g.款式,
  93. g.客户编号 as 客人编号,
  94. g.辅料计划入库时间 as 辅料计划入库时间,
  95. (SELECT MIN(rq) FROM 库存_出入库明细 WHERE order_id = g.订单编号 AND name = '入库') as 面料入库时间,
  96. (SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = '裁剪' AND Mod_rq IS NULL) as 开裁日期,
  97. (SELECT SUM(数量) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = '裁剪' AND Mod_rq IS NULL) as 实裁数量,
  98. (SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = '车缝' AND Mod_rq IS NULL) as 上车位日期,
  99. (SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = '车缝' AND 尾包 = 1 AND Mod_rq IS NULL) as 车位完成日期,
  100. (SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = '手工' AND 尾包 = 1 AND Mod_rq IS NULL) as 后道完成日期,
  101. (SELECT SUM(数量) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = '包装' AND Mod_rq IS NULL) as 已完成数量,
  102. (SELECT GROUP_CONCAT(DISTINCT SUBSTRING(sczl_jtbh, 3, 2) ORDER BY SUBSTRING(sczl_jtbh, 3, 2) SEPARATOR '、')
  103. FROM 设备_产量计酬
  104. WHERE 订单编号 = g.订单编号 AND 工序名称 = '车缝' AND Mod_rq IS NULL) as 生产组别,
  105. NULL as 辅料入库时间,
  106. NULL as 台产,
  107. NULL as 产前样批核,
  108. NULL as 备注
  109. FROM 工单_基本资料 g
  110. WHERE g.落货日期 >= '{$start}'
  111. AND g.落货日期 < '{$end}'
  112. AND g.Mod_rq IS NULL
  113. GROUP BY g.订单编号
  114. ORDER BY g.落货日期 ASC";
  115. $list = Db::query($sql);
  116. if (empty($list)) {
  117. $this->success('', []);
  118. }
  119. // 处理日期字段,去掉时分秒
  120. $dateFields = ['下单日期', '货期', '面料入库时间', '辅料入库时间', '开裁日期', '上车位日期', '车位完成日期', '后道完成日期'];
  121. foreach ($list as &$item) {
  122. foreach ($dateFields as $field) {
  123. if (isset($item[$field]) && !empty($item[$field])) {
  124. $item[$field] = date('Y-m-d', strtotime($item[$field]));
  125. }
  126. }
  127. if (empty($item['生产组别'])) {
  128. $item['生产组别'] = '';
  129. }
  130. // 确保数值字段为整数
  131. $item['实裁数量'] = intval($item['实裁数量'] ?? 0);
  132. $item['已完成数量'] = intval($item['已完成数量'] ?? 0);
  133. $item['订单数量'] = intval($item['订单数量'] ?? 0);
  134. $item['入库数量'] = intval($item['入库数量'] ?? 0);
  135. }
  136. // 存入缓存,有效期1小时
  137. cache($cacheKey, $list, 3600);
  138. $this->success('成功', $list);
  139. }
  140. /**
  141. * 清除工单数据缓存
  142. * @param string $rq 月份(Y-m格式),为空则清除所有当前月份的缓存
  143. */
  144. public function clearWorkOrderCache()
  145. {
  146. $rq = $this->request->get('rq');
  147. if (empty($rq)) {
  148. $this->error('请指定要清除缓存的月份');
  149. }
  150. if (!preg_match('/^\d{4}-\d{2}$/', $rq)) {
  151. $this->error('参数格式错误,请使用 YYYY-MM 格式');
  152. }
  153. $cacheKey = 'work_orders_' . $rq;
  154. cache($cacheKey, null);
  155. $this->success('缓存已清除');
  156. }
  157. }