MonthlyGarments.php 8.6 KB

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