MountHours.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\db\exception\BindParamException;
  5. use think\exception\PDOException;
  6. use \think\Request;
  7. use \think\Db;
  8. /**
  9. * 装版工时审核页面
  10. */
  11. class MountHours extends Api
  12. {
  13. protected $noNeedLogin = ['*'];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 装版工时审核页面左侧菜单
  17. * @return void
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function getTab()
  23. {
  24. if (!$this->request->isGet()) {
  25. $this->error('请求错误');
  26. }
  27. $data = $this->fetchAndFormatPlateChangeData();
  28. empty($data) ? $this->error('未找到换版数据') : $this->success('成功', $data);
  29. }
  30. /**
  31. * 获取并格式化装版数据
  32. */
  33. private function fetchAndFormatPlateChangeData()
  34. {
  35. $departments = ['胶印车间', '凹丝印车间', '印后车间', '检验车间'];
  36. $list = \db('设备_装版工时')
  37. ->alias('a')
  38. ->join('设备_基本资料 b', 'a.jtbh = b.设备编号')
  39. ->field([
  40. 'DATE_FORMAT(a.sys_rq, "%Y-%m-%d") as day',
  41. 'DATE_FORMAT(a.sys_rq, "%Y%m") as month',
  42. 'b.使用部门'
  43. ])
  44. ->where('b.使用部门', 'in', $departments)
  45. ->group('day, b.使用部门')
  46. ->order('day DESC')
  47. ->select();
  48. return array_reduce($list, function ($result, $item) {
  49. $month = $item['month'];
  50. $department = $item['使用部门'];
  51. $day = $item['day'];
  52. $result[$month][$department][] = $day;
  53. return $result;
  54. }, []);
  55. }
  56. /**
  57. * 装版工时审核数据
  58. * @return void
  59. */
  60. public function getMountList()
  61. {
  62. // 1. 请求验证
  63. if (!$this->request->isGet()) {
  64. $this->error('请求错误');
  65. }
  66. // 2. 参数验证
  67. $params = $this->validateParams($this->request->param());
  68. // 3. 获取数据
  69. $list = $this->fetchMountData($params);
  70. if (empty($list)) {
  71. $this->error('未找到数据');
  72. }
  73. // 4. 处理数据
  74. $processedList = $this->processListData($list);
  75. $this->success('成功', $processedList);
  76. }
  77. /**
  78. * 参数验证
  79. */
  80. private function validateParams($params)
  81. {
  82. if (empty($params['day']) || empty($params['sist'])) {
  83. $this->error('参数错误:day和sist不能为空');
  84. }
  85. // 验证日期格式
  86. if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $params['day'])) {
  87. $this->error('参数错误:日期格式不正确');
  88. }
  89. return [
  90. 'day' => $params['day'],
  91. 'sist' => $params['sist']
  92. ];
  93. }
  94. /**
  95. * 获取装版数据
  96. */
  97. private function fetchMountData($params)
  98. {
  99. $fields = [
  100. 'a.gdbh' => '工单编号',
  101. 'a.yjno' => '印件号',
  102. 'a.gxh' => '工序号',
  103. 'b.sczl_gxmc' => '工序名称',
  104. 'a.jtbh' => '机台编号',
  105. 'a.startTime' => '开始时间',
  106. 'a.endTime' => '结束时间',
  107. 'a.换版总工时' => '装版总工时',
  108. 'a.换版联拼数' => '装版联拼数',
  109. 'a.换版类型' => '装版类型',
  110. 'a.sys_rq' => '创建时间',
  111. 'a.coefficient' => '系数',
  112. 'a.装版补产工时',
  113. 'a.leader' => '班组长',
  114. 'a.leaderConfirm' => '班组长确认时间',
  115. 'a.statistics' => '统计人员',
  116. 'a.statisticsConfirm' => '统计确认时间',
  117. 'a.sys_id' => '上报机台',
  118. 'a.Uniqid',
  119. 'a.yieldUid' => '关联ID'
  120. ];
  121. return \db('设备_装版工时')
  122. ->alias('a')
  123. ->join('设备_产量计酬 b', 'a.yieldUid = b.UniqId')
  124. ->join('设备_基本资料 c', 'a.jtbh = c.设备编号')
  125. ->field($fields)
  126. ->where('a.sys_rq', 'like', $params['day'] . '%')
  127. ->where('c.使用部门', $params['sist'])
  128. ->group('a.gdbh, a.yjno, a.gxh, a.jtbh, a.换版类型')
  129. ->order('a.jtbh')
  130. ->select();
  131. }
  132. /**
  133. * 处理列表数据
  134. */
  135. private function processListData($list)
  136. {
  137. return array_map(function ($item) {
  138. return [
  139. '工单编号' => $item['工单编号'],
  140. '印件号' => $item['印件号'],
  141. '工序号' => $item['工序号'],
  142. '工序名称' => $item['工序名称'],
  143. '机台编号' => $item['机台编号'],
  144. '开始时间' => $item['开始时间'],
  145. '结束时间' => $item['结束时间'],
  146. '装版总工时' => $item['装版总工时'],
  147. '装版联拼数' => $item['装版联拼数'],
  148. '装版类型' => $item['装版类型'],
  149. '创建时间' => $item['创建时间'],
  150. '系数' => $item['系数'],
  151. '装版补产工时' => $item['装版补产工时'],
  152. '班组长' => $item['班组长'],
  153. '班组长确认时间' => $item['班组长确认时间'],
  154. '统计人员' => $item['统计人员'],
  155. '统计确认时间' => $item['统计确认时间'],
  156. '上报机台' => $item['上报机台'],
  157. 'Uniqid' => $item['Uniqid'],
  158. '班组长确认状态' => $this->getConfirmationStatus($item['班组长'], $item['班组长确认时间']),
  159. '统计确认状态' => $this->getConfirmationStatus($item['统计人员'], $item['统计确认时间']),
  160. '关联ID' => $item['关联ID'],
  161. ];
  162. }, $list);
  163. }
  164. /**
  165. * 获取确认状态
  166. */
  167. private function getConfirmationStatus($person, $confirmTime)
  168. {
  169. return !empty($person) && !empty($confirmTime) ? '已确认' : '未确认';
  170. }
  171. /**
  172. * 班组长审核
  173. * @return void
  174. * @throws BindParamException
  175. * @throws PDOException
  176. * @throws \think\Exception
  177. */
  178. public function leaderConfirm()
  179. {
  180. if (!$this->request->isPost()) {
  181. $this->error('请求错误');
  182. }
  183. $param = Request::instance()->post();
  184. if (empty($param)) {
  185. $this->error('参数错误');
  186. }
  187. $coefficient = $param['coefficient'];
  188. $leader = $param['leader'];
  189. $leaderConfirm = date('Y-m-d H:i:s');
  190. $sql = \db('设备_装版工时')
  191. ->where('Uniqid', $param['Uniqid'])
  192. ->fetchSql(true)
  193. ->update(['coefficient' => $coefficient, 'leaderConfirm' => $leaderConfirm, 'leader' => $leader]);
  194. $res = \db()->query($sql);
  195. if ($res !== false) {
  196. $this->success('确认成功');
  197. }else{
  198. $this->error('确认失败');
  199. }
  200. }
  201. /**
  202. * 统计员审核
  203. * @return void
  204. * @throws BindParamException
  205. * @throws PDOException
  206. * @throws \think\Exception
  207. */
  208. public function statisticsConfirm()
  209. {
  210. if (!$this->request->isPost()) {
  211. $this->error('请求错误');
  212. }
  213. $param = Request::instance()->post();
  214. if (empty($param)) {
  215. $this->error('参数错误');
  216. }
  217. $statisticsConfirm = date('Y-m-d H:i:s',time());
  218. $sql = \db('设备_装版工时')
  219. ->where('Uniqid', $param['Uniqid'])
  220. ->fetchSql(true)
  221. ->update(['装版补产工时' => $param['装版补产工时'], 'statisticsConfirm' => $statisticsConfirm, 'statistics' => $param['statistics']]);
  222. $res = \db()->query($sql);
  223. $clSql = \db('设备_产量计酬')
  224. ->where('UniqId', $param['yieldUid'])
  225. ->fetchSql(true)
  226. ->update(['sczl_装版工时' => $param['装版补产工时']]);
  227. $clRes = \db()->query($clSql);
  228. if ($res !== false && $clRes !== false) {
  229. $this->success('确认成功');
  230. }else{
  231. $this->error('确认失败');
  232. }
  233. }
  234. }