MonthlyGarments.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use Monolog\Handler\IFTTTHandler;
  5. use Overtrue\Socialite\Providers\WeWorkProvider;
  6. use think\Db;
  7. use think\Request;
  8. use function fast\e;
  9. /**
  10. *
  11. * 生产单衣进度管理
  12. */
  13. class MonthlyGarments extends Api
  14. {
  15. protected $noNeedLogin = ['*'];
  16. protected $noNeedRight = ['*'];
  17. /**
  18. * 样衣进度左侧数据
  19. */
  20. public function getDateTree()
  21. {
  22. if (!$this->request->isGet()) {
  23. $this->error('请求错误');
  24. }
  25. $results = \db('月度样衣进度')
  26. ->field([
  27. 'DATE_FORMAT(Sys_rq, "%Y-%m")' => '年月',
  28. '客户编号',
  29. 'COUNT(*) as total'
  30. ])
  31. ->whereNull('Mod_rq')
  32. ->group('年月, 客户编号')
  33. ->order('年月 DESC')
  34. ->select();
  35. $formattedData = [];
  36. foreach ($results as $row) {
  37. $yearMonth = $row['年月'];
  38. $customerCode = $row['客户编号'];
  39. $total = $row['total'];
  40. if (!isset($formattedData[$yearMonth])) {
  41. $formattedData[$yearMonth] = [];
  42. }
  43. $formattedData[$yearMonth][] = [
  44. '客户编号' => $customerCode,
  45. 'total' => $total
  46. ];
  47. }
  48. krsort($formattedData);
  49. $this->success('成功', $formattedData);
  50. }
  51. /**
  52. * 样衣进度列表数据(支持分页)
  53. * @param string sys_rq 年月,如 2026-09
  54. * @param string customer 客户编号
  55. * @param string search 搜索
  56. * @param int page 页码,默认1
  57. * @param int pageSize|limit 每页条数,默认30
  58. */
  59. public function getMonthlyGarmentsList()
  60. {
  61. if (!$this->request->isGet()) {
  62. $this->error('请求错误');
  63. }
  64. $yearMonth = $this->request->param('sys_rq', '');
  65. $customerCode = $this->request->param('customer', '');
  66. $search = $this->request->param('search', '');
  67. $page = max(1, intval($this->request->param('page', 1)));
  68. $limit = intval($this->request->param('pageSize', 0));
  69. if ($limit <= 0) {
  70. $limit = intval($this->request->param('limit', 30));
  71. }
  72. $limit = max(1, min(200, $limit));
  73. $offset = ($page - 1) * $limit;
  74. // 不要 clone 查询对象:ThinkPHP 预处理绑定会被 count 吃掉,再 select 会报 2031
  75. $applyFilters = function ($query) use ($yearMonth, $customerCode, $search) {
  76. $query->whereNull('Mod_rq');
  77. if ($yearMonth) {
  78. $monthStart = $yearMonth . '-01 00:00:00';
  79. $monthEnd = date('Y-m-d H:i:s', strtotime($monthStart . ' +1 month'));
  80. $query->where('Sys_rq', '>=', $monthStart)
  81. ->where('Sys_rq', '<', $monthEnd);
  82. }
  83. if ($customerCode === '__EMPTY__') {
  84. $query->where(function ($q) {
  85. $q->whereNull('客户编号')->whereOr('客户编号', '');
  86. });
  87. } elseif ($customerCode) {
  88. $query->where('客户编号', $customerCode);
  89. }
  90. if ($search) {
  91. $query->where(function ($q) use ($search) {
  92. $q->where('客户编号', 'like', "%{$search}%")
  93. ->whereOr('款号', 'like', "%{$search}%")
  94. ->whereOr('款式', 'like', "%{$search}%");
  95. });
  96. }
  97. return $query;
  98. };
  99. $total = (int)$applyFilters(\db('月度样衣进度'))->count();
  100. $results = $applyFilters(\db('月度样衣进度'))
  101. ->order('UniqId DESC')
  102. ->limit($offset, $limit)
  103. ->select();
  104. foreach ($results as &$row) {
  105. if (!empty($row['下单日期'])) {
  106. $row['下单日期'] = date('Y-m-d', strtotime($row['下单日期']));
  107. }
  108. if (!empty($row['计划交办日期'])) {
  109. $row['计划交办日期'] = date('Y-m-d', strtotime($row['计划交办日期']));
  110. }
  111. }
  112. unset($row);
  113. $this->success('成功', [
  114. 'result' => $results,
  115. 'total' => $total,
  116. 'page' => $page,
  117. 'pageSize' => $limit,
  118. ]);
  119. }
  120. /**
  121. * 样衣进度新增数据
  122. */
  123. public function saveMonthlyGarments()
  124. {
  125. if (!$this->request->isPost()) {
  126. $this->error('请求错误');
  127. }
  128. $data = $this->request->post();
  129. $customerCode = $data['客户编号'] ?? '';
  130. $styleNo = $data['款号'] ?? '';
  131. $style = $data['款式'] ?? '';
  132. $sampleType = $data['样办种类'] ?? '';
  133. $department = $data['做办部门'] ?? '';
  134. $quantity = isset($data['数量']) && $data['数量'] !== '' ? (int)$data['数量'] : 0;
  135. $orderDate = (!empty($data['下单日期'])) ? "'{$data['下单日期']}'" : 'NULL';
  136. $fabricProgress = $data['面料进度'] ?? '';
  137. $auxiliaryProgress = $data['辅料进度'] ?? '';
  138. $planDeliveryDate = (!empty($data['计划交办日期'])) ? "'{$data['计划交办日期']}'" : 'NULL';
  139. $actualDeliveryDate = (!empty($data['实际交办日期'])) ? "'{$data['实际交办日期']}'" : 'NULL';
  140. $remark = $data['备注'] ?? '';
  141. $sysId = $data['Sys_id'] ?? '';
  142. $sysRq = date('Y-m-d H:i:s');
  143. $sql = "INSERT INTO `月度样衣进度` (`客户编号`, `款号`, `款式`, `样办种类`, `做办部门`, `数量`, `下单日期`, `面料进度`, `辅料进度`, `计划交办日期`, `实际交办日期`, `备注`, `Sys_id`, `Sys_rq`, `Mod_rq`) VALUES ('{$customerCode}', '{$styleNo}', '{$style}', '{$sampleType}', '{$department}', {$quantity}, {$orderDate}, '{$fabricProgress}', '{$auxiliaryProgress}', {$planDeliveryDate}, {$actualDeliveryDate}, '{$remark}', '{$sysId}', '{$sysRq}', NULL)";
  144. $result = \db()->execute($sql);
  145. if ($result !== false) {
  146. $lastId = \db('月度样衣进度')->getLastInsID();
  147. $this->success('保存成功', ['id' => $lastId]);
  148. } else {
  149. $this->error('保存失败');
  150. }
  151. }
  152. /**
  153. * 样衣进度修改数据
  154. */
  155. public function updateMonthlyGarments()
  156. {
  157. if (!$this->request->isPost()) {
  158. $this->error('请求错误');
  159. }
  160. $data = $this->request->post();
  161. $uniqId = $data['UniqId'] ?? '';
  162. $nickName = $data['nickName'] ?? '';
  163. if (!$uniqId) {
  164. $this->error('UniqId不能为空');
  165. }
  166. $oldData = \db('月度样衣进度')->where('UniqId', $uniqId)->find();
  167. $customerCode = $data['客户编号'] ?? '';
  168. $styleNo = $data['款号'] ?? '';
  169. $style = $data['款式'] ?? '';
  170. $sampleType = $data['样办种类'] ?? '';
  171. $department = $data['做办部门'] ?? '';
  172. $quantity = isset($data['数量']) && $data['数量'] !== '' ? (int)$data['数量'] : 0;
  173. $orderDate = (!empty($data['下单日期'])) ? "'{$data['下单日期']}'" : 'NULL';
  174. $fabricProgress = $data['面料进度'] ?? '';
  175. $auxiliaryProgress = $data['辅料进度'] ?? '';
  176. $planDeliveryDate = (!empty($data['计划交办日期'])) ? "'{$data['计划交办日期']}'" : 'NULL';
  177. $actualDeliveryDate = (!empty($data['实际交办日期'])) ? "'{$data['实际交办日期']}'" : 'NULL';
  178. $remark = $data['备注'] ?? '';
  179. $sysRq = date('Y-m-d H:i:s');
  180. $sql = "UPDATE `月度样衣进度` SET `客户编号`='{$customerCode}', `款号`='{$styleNo}', `款式`='{$style}', `样办种类`='{$sampleType}', `做办部门`='{$department}', `数量`={$quantity}, `下单日期`={$orderDate}, `面料进度`='{$fabricProgress}', `辅料进度`='{$auxiliaryProgress}', `计划交办日期`={$planDeliveryDate}, `实际交办日期`={$actualDeliveryDate}, `备注`='{$remark}', `Sys_rq`='{$sysRq}' WHERE `UniqId`='{$uniqId}'";
  181. $result = \db()->query($sql);
  182. if ($result !== false) {
  183. $this->saveLogs($uniqId, $oldData, $data, $nickName);
  184. $this->success('修改成功');
  185. } else {
  186. $this->error('修改失败');
  187. }
  188. }
  189. public function deleteMonthlyGarments()
  190. {
  191. if (!$this->request->isPost()) {
  192. $this->error('请求错误');
  193. }
  194. $data = $this->request->post();
  195. $uniqId = $data['UniqId'] ?? '';
  196. $nickName = $data['nickName'] ?? '';
  197. if (!$uniqId) {
  198. $this->error('UniqId不能为空');
  199. }
  200. // 支持多个 UniqId(逗号分隔),逐条删除
  201. $idList = array_filter(array_map('trim', explode(',', $uniqId)));
  202. if (empty($idList)) {
  203. $this->error('UniqId不能为空');
  204. }
  205. $modRq = date('Y-m-d H:i:s');
  206. $successCount = 0;
  207. foreach ($idList as $id) {
  208. $safeId = addslashes($id);
  209. $sql = "UPDATE `月度样衣进度` SET `Mod_rq`='{$modRq}' WHERE `UniqId`='{$safeId}'";
  210. $result = \db()->query($sql);
  211. if ($result !== false) {
  212. $successCount++;
  213. }
  214. }
  215. if ($successCount > 0) {
  216. $this->success('删除成功');
  217. } else {
  218. $this->error('删除失败');
  219. }
  220. }
  221. /**
  222. * 样衣进度存储日志数据
  223. */
  224. private function saveLogs($uniqId, $oldData, $newData, $operator)
  225. {
  226. $fields = ['客户编号', '款号', '款式', '样办种类', '做办部门', '数量', '下单日期', '面料进度', '辅料进度', '计划交办日期', '实际交办日期', '备注'];
  227. $modifyTime = date('Y-m-d H:i:s');
  228. foreach ($fields as $field) {
  229. $oldValue = $oldData[$field] ?? '';
  230. $newValue = $newData[$field] ?? '';
  231. if ((string)$oldValue !== (string)$newValue) {
  232. $sql = "INSERT INTO `样衣进度日志` (`Uniqid`, `ModifyField`, `OriginalValue`, `Modified`, `修改人`, `修改日期`) VALUES ('{$uniqId}', '{$field}', '{$oldValue}', '{$newValue}', '{$operator}', '{$modifyTime}')";
  233. \db()->query($sql);
  234. }
  235. }
  236. }
  237. /**
  238. * 样衣进度日志数据
  239. */
  240. public function getLogs()
  241. {
  242. if (!$this->request->isGet()) {
  243. $this->error('请求错误');
  244. }
  245. $uniqId = $this->request->get('UniqId') ?? '';
  246. if (!$uniqId) {
  247. $this->error('UniqId不能为空');
  248. }
  249. $logs = \db('样衣进度日志')
  250. ->where('Uniqid', $uniqId)
  251. ->order('修改日期', 'desc')
  252. ->select();
  253. $this->success('获取成功', ['result' => $logs, 'total' => count($logs)]);
  254. }
  255. }