MonthlyGarments.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. $query = \db('月度样衣进度')
  75. ->whereNull('Mod_rq');
  76. if ($yearMonth) {
  77. $query->where('DATE_FORMAT(Sys_rq, "%Y-%m")', $yearMonth);
  78. }
  79. if ($customerCode === '__EMPTY__') {
  80. $query->where(function ($q) {
  81. $q->whereNull('客户编号')->whereOr('客户编号', '');
  82. });
  83. } elseif ($customerCode) {
  84. $query->where('客户编号', $customerCode);
  85. }
  86. if ($search) {
  87. $query->where(function ($q) use ($search) {
  88. $q->where('客户编号', 'like', "%{$search}%")
  89. ->whereOr('款号', 'like', "%{$search}%")
  90. ->whereOr('款式', 'like', "%{$search}%");
  91. });
  92. }
  93. $total = (int)(clone $query)->count();
  94. $results = $query
  95. ->order('UniqId DESC')
  96. ->limit($offset, $limit)
  97. ->select();
  98. foreach ($results as &$row) {
  99. if (!empty($row['下单日期'])) {
  100. $row['下单日期'] = date('Y-m-d', strtotime($row['下单日期']));
  101. }
  102. if (!empty($row['计划交办日期'])) {
  103. $row['计划交办日期'] = date('Y-m-d', strtotime($row['计划交办日期']));
  104. }
  105. }
  106. unset($row);
  107. $this->success('成功', [
  108. 'result' => $results,
  109. 'total' => $total,
  110. 'page' => $page,
  111. 'pageSize' => $limit,
  112. ]);
  113. }
  114. /**
  115. * 样衣进度新增数据
  116. */
  117. public function saveMonthlyGarments()
  118. {
  119. if (!$this->request->isPost()) {
  120. $this->error('请求错误');
  121. }
  122. $data = $this->request->post();
  123. $customerCode = $data['客户编号'] ?? '';
  124. $styleNo = $data['款号'] ?? '';
  125. $style = $data['款式'] ?? '';
  126. $sampleType = $data['样办种类'] ?? '';
  127. $department = $data['做办部门'] ?? '';
  128. $quantity = isset($data['数量']) && $data['数量'] !== '' ? (int)$data['数量'] : 0;
  129. $orderDate = (!empty($data['下单日期'])) ? "'{$data['下单日期']}'" : 'NULL';
  130. $fabricProgress = $data['面料进度'] ?? '';
  131. $auxiliaryProgress = $data['辅料进度'] ?? '';
  132. $planDeliveryDate = (!empty($data['计划交办日期'])) ? "'{$data['计划交办日期']}'" : 'NULL';
  133. $actualDeliveryDate = (!empty($data['实际交办日期'])) ? "'{$data['实际交办日期']}'" : 'NULL';
  134. $remark = $data['备注'] ?? '';
  135. $sysId = $data['Sys_id'] ?? '';
  136. $sysRq = date('Y-m-d H:i:s');
  137. $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)";
  138. $result = \db()->execute($sql);
  139. if ($result !== false) {
  140. $lastId = \db('月度样衣进度')->getLastInsID();
  141. $this->success('保存成功', ['id' => $lastId]);
  142. } else {
  143. $this->error('保存失败');
  144. }
  145. }
  146. /**
  147. * 样衣进度修改数据
  148. */
  149. public function updateMonthlyGarments()
  150. {
  151. if (!$this->request->isPost()) {
  152. $this->error('请求错误');
  153. }
  154. $data = $this->request->post();
  155. $uniqId = $data['UniqId'] ?? '';
  156. $nickName = $data['nickName'] ?? '';
  157. if (!$uniqId) {
  158. $this->error('UniqId不能为空');
  159. }
  160. $oldData = \db('月度样衣进度')->where('UniqId', $uniqId)->find();
  161. $customerCode = $data['客户编号'] ?? '';
  162. $styleNo = $data['款号'] ?? '';
  163. $style = $data['款式'] ?? '';
  164. $sampleType = $data['样办种类'] ?? '';
  165. $department = $data['做办部门'] ?? '';
  166. $quantity = isset($data['数量']) && $data['数量'] !== '' ? (int)$data['数量'] : 0;
  167. $orderDate = (!empty($data['下单日期'])) ? "'{$data['下单日期']}'" : 'NULL';
  168. $fabricProgress = $data['面料进度'] ?? '';
  169. $auxiliaryProgress = $data['辅料进度'] ?? '';
  170. $planDeliveryDate = (!empty($data['计划交办日期'])) ? "'{$data['计划交办日期']}'" : 'NULL';
  171. $actualDeliveryDate = (!empty($data['实际交办日期'])) ? "'{$data['实际交办日期']}'" : 'NULL';
  172. $remark = $data['备注'] ?? '';
  173. $sysRq = date('Y-m-d H:i:s');
  174. $sql = "UPDATE `月度样衣进度` SET `客户编号`='{$customerCode}', `款号`='{$styleNo}', `款式`='{$style}', `样办种类`='{$sampleType}', `做办部门`='{$department}', `数量`={$quantity}, `下单日期`={$orderDate}, `面料进度`='{$fabricProgress}', `辅料进度`='{$auxiliaryProgress}', `计划交办日期`={$planDeliveryDate}, `实际交办日期`={$actualDeliveryDate}, `备注`='{$remark}', `Sys_rq`='{$sysRq}' WHERE `UniqId`='{$uniqId}'";
  175. $result = \db()->query($sql);
  176. if ($result !== false) {
  177. $this->saveLogs($uniqId, $oldData, $data, $nickName);
  178. $this->success('修改成功');
  179. } else {
  180. $this->error('修改失败');
  181. }
  182. }
  183. public function deleteMonthlyGarments()
  184. {
  185. if (!$this->request->isPost()) {
  186. $this->error('请求错误');
  187. }
  188. $data = $this->request->post();
  189. $uniqId = $data['UniqId'] ?? '';
  190. $nickName = $data['nickName'] ?? '';
  191. if (!$uniqId) {
  192. $this->error('UniqId不能为空');
  193. }
  194. $modRq = date('Y-m-d H:i:s');
  195. $sql = "UPDATE `月度样衣进度` SET `Mod_rq`='{$modRq}' WHERE `UniqId`='{$uniqId}'";
  196. $result = \db()->query($sql);
  197. if ($result !== false) {
  198. $this->success('删除成功');
  199. } else {
  200. $this->error('删除失败');
  201. }
  202. }
  203. /**
  204. * 样衣进度存储日志数据
  205. */
  206. private function saveLogs($uniqId, $oldData, $newData, $operator)
  207. {
  208. $fields = ['客户编号', '款号', '款式', '样办种类', '做办部门', '数量', '下单日期', '面料进度', '辅料进度', '计划交办日期', '实际交办日期', '备注'];
  209. $modifyTime = date('Y-m-d H:i:s');
  210. foreach ($fields as $field) {
  211. $oldValue = $oldData[$field] ?? '';
  212. $newValue = $newData[$field] ?? '';
  213. if ((string)$oldValue !== (string)$newValue) {
  214. $sql = "INSERT INTO `样衣进度日志` (`Uniqid`, `ModifyField`, `OriginalValue`, `Modified`, `修改人`, `修改日期`) VALUES ('{$uniqId}', '{$field}', '{$oldValue}', '{$newValue}', '{$operator}', '{$modifyTime}')";
  215. \db()->query($sql);
  216. }
  217. }
  218. }
  219. /**
  220. * 样衣进度日志数据
  221. */
  222. public function getLogs()
  223. {
  224. if (!$this->request->isGet()) {
  225. $this->error('请求错误');
  226. }
  227. $uniqId = $this->request->get('UniqId') ?? '';
  228. if (!$uniqId) {
  229. $this->error('UniqId不能为空');
  230. }
  231. $logs = \db('样衣进度日志')
  232. ->where('Uniqid', $uniqId)
  233. ->order('修改日期', 'desc')
  234. ->select();
  235. $this->success('获取成功', ['result' => $logs, 'total' => count($logs)]);
  236. }
  237. }