PrintingPlate.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Request;
  5. use function fast\e;
  6. /**
  7. * 印版库管理
  8. */
  9. class PrintingPlate extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 印版库管理左侧菜单
  15. * @return void
  16. */
  17. public function getTab()
  18. {
  19. if (!$this->request->isGet()) {
  20. $this->error('请求错误');
  21. }
  22. $data = [
  23. 'MN印版' => $this->buildTree('05'),
  24. '翌星印版' => $this->buildTree('Y05')
  25. ];
  26. $this->success('成功', $data);
  27. }
  28. //私有方法,构建数据
  29. private function buildTree($prefix)
  30. {
  31. $list = db('物料_存货结构')
  32. ->where('编号', 'like', $prefix . '%')
  33. ->order('编号', 'asc')
  34. ->select();
  35. $map = [];
  36. $tree = [];
  37. // 创建编号映射表
  38. foreach ($list as $item) {
  39. $map[$item['编号']] = $item['名称'];
  40. }
  41. // 构建树形结构
  42. foreach ($list as $item) {
  43. $code = $item['编号'];
  44. $name = $item['名称'];
  45. $key = "{$code} {$name}";
  46. $depth = strlen($code) - strlen($prefix);
  47. switch ($depth) {
  48. case 0: // 根节点 (05/Y05)
  49. $tree[$key] = [];
  50. break;
  51. case 2: // 二级节点 (0501/Y0501)
  52. $parentCode = substr($code, 0, strlen($prefix));
  53. $parentKey = "{$parentCode} {$map[$parentCode]}";
  54. $tree[$parentKey][$key] = [];
  55. break;
  56. case 4: // 三级节点 (050100/Y050101)
  57. $parentCode = substr($code, 0, strlen($prefix) + 2);
  58. $grandCode = substr($code, 0, strlen($prefix));
  59. $grandKey = "{$grandCode} {$map[$grandCode]}";
  60. $parentKey = "{$parentCode} {$map[$parentCode]}";
  61. if (isset($tree[$grandKey][$parentKey])) {
  62. $tree[$grandKey][$parentKey][] = $key;
  63. }
  64. break;
  65. }
  66. }
  67. return $tree;
  68. }
  69. /**
  70. * 存货编码列表
  71. * @return void
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public function MaterailCodeList()
  77. {
  78. if ($this->request->isGet() === false){
  79. $this->error('请求错误');
  80. }
  81. $param = $this->request->param();
  82. if (empty($param) || !isset($param['code'])){
  83. $this->error('参数错误');
  84. }
  85. //截取物料编码,带字母截取前七位,不带字母截取前五位
  86. if (preg_match('/[a-zA-Z]/', $param['code'])) {
  87. $code = substr($param['code'], 0, 7);
  88. } else {
  89. $code = substr($param['code'], 0, 6);
  90. }
  91. //根据物料编码分类查询
  92. $list = db('产品_印版库')
  93. ->alias('a')
  94. ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码')
  95. ->join('工单_印版领用记录 c', 'a.存货编码 = c.Yb_存货编码 AND a.供方批号 = c.Yb_供方批号', 'left')
  96. ->where('a.存货编码', 'like', $code . '%')
  97. ->group('a.存货编码, a.供方批号')
  98. ->order('报废日期, a.存货编码')
  99. ->field('
  100. rtrim(a.存货编码) as 存货编码,
  101. rtrim(b.物料名称) as 物料名称,
  102. rtrim(a.印版名称) as 印版名称,
  103. rtrim(a.供方批号) as 供方批号,
  104. DATE(a.制造日期) as 制造日期,
  105. CASE
  106. WHEN a.报废日期 = "1900-01-01 00:00:00" THEN NULL
  107. ELSE DATE(a.报废日期)
  108. END as 报废日期,
  109. a.原始印数,
  110. a.考核印数,
  111. a.UniqID,
  112. a.Sys_id as 创建用户,
  113. a.Sys_rq as 创建日期,
  114. a.Mod_rq as 修改时间,
  115. count(c.Yb_印数) as 累计印数
  116. ')
  117. ->select();
  118. if (empty($list)) {
  119. $this->error('失败');
  120. }else{
  121. $this->success('成功', $list);
  122. }
  123. }
  124. /**
  125. * 印版管理印版修改
  126. * @return void
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. * @throws \think\exception\DbException
  130. */
  131. public function MaterailDetail()
  132. {
  133. if ($this->request->isGet() === false){
  134. $this->error('请求错误');
  135. }
  136. $param = $this->request->param();
  137. if (empty($param) || !isset($param['UniqID'])){
  138. $this->error('参数错误');
  139. }
  140. $result = db('产品_印版库')
  141. ->alias('a')
  142. ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码')
  143. ->where('a.UniqID', $param['UniqID'])
  144. ->order('报废日期, a.存货编码')
  145. ->field('
  146. rtrim(a.存货编码) as 存货编码,
  147. rtrim(b.物料名称) as 物料名称,
  148. rtrim(a.印版名称) as 印版备注,
  149. rtrim(a.供方批号) as 供方批号,
  150. DATE(a.制造日期) as 制造日期,
  151. CASE
  152. WHEN a.报废日期 = "1900-01-01 00:00:00" THEN NULL
  153. ELSE DATE(a.报废日期)
  154. END as 报废日期,
  155. a.原始印数,
  156. a.考核印数,
  157. a.UniqID
  158. ')
  159. ->find();
  160. if (empty($result)) {
  161. $this->error('未找到数据');
  162. }else{
  163. $this->success('成功', $result);
  164. }
  165. }
  166. /**
  167. * 印版资料新增修改
  168. * @return void
  169. * @throws \think\Exception
  170. * @throws \think\db\exception\BindParamException
  171. * @throws \think\db\exception\DataNotFoundException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. * @throws \think\exception\DbException
  174. * @throws \think\exception\PDOException
  175. */
  176. public function MaterailEdit()
  177. {
  178. if ($this->request->isPost() === false){
  179. $this->error('请求错误');
  180. }
  181. $param = Request::instance()->post();
  182. if (empty($param) || !isset($param['code'])){
  183. $this->error('参数错误');
  184. }
  185. $data = [
  186. '存货编码' => $param['code'],
  187. '供方批号' => $param['batch'],
  188. '印版名称' => $param['desc'],
  189. '制造日期' => $param['Manufactur_date'],
  190. '原始印数' => $param['start_num'],
  191. '考核印数' => $param['Assessment_num'],
  192. '报废日期' => $param['Scrappe_date']?:'1900-01-01 00:00:00',
  193. 'Sys_id' => $param['sys_id']
  194. ];
  195. //查询数据是否存在,存在则修改,不存在则增加
  196. $res = db('产品_印版库')
  197. ->where('存货编码',$param['code'])
  198. ->where('供方批号',$param['batch'])
  199. ->find();
  200. if (empty($res)){
  201. $data['Sys_rq'] = date('Y-m-d H:i:s',time());
  202. $sql = db('产品_印版库')->fetchSql(true)->insert($data);
  203. }else{
  204. $data['Mod_rq'] = date('Y-m-d H:i:s',time());
  205. $sql = db('产品_印版库')->fetchSql(true)->update($data);
  206. }
  207. $result = db()->query($sql);
  208. if ($result === false){
  209. $this->error('修改失败');
  210. }else{
  211. $this->success('修改成功');
  212. }
  213. }
  214. /**
  215. * 印版新增->存货编码获取
  216. * @return void
  217. * @throws \think\db\exception\DataNotFoundException
  218. * @throws \think\db\exception\ModelNotFoundException
  219. * @throws \think\exception\DbException
  220. */
  221. public function getInventoryCode()
  222. {
  223. if (!$this->request->isGet()) {
  224. $this->error('请求错误');
  225. }
  226. $param = $this->request->param();
  227. if (empty($param) || !isset($param['code'])) {
  228. $this->error('参数错误');
  229. }
  230. // 提取前缀
  231. $codeLength = preg_match('/[a-zA-Z]/', $param['code']) ? 5 : 4;
  232. $code = substr($param['code'], 0, $codeLength);
  233. $where['物料代码'] = ['like', $code . '%'];
  234. if (!empty($param['search'])) {
  235. $where['物料名称'] = ['like', '%' . $param['search'] . '%'];
  236. }
  237. // 查询物料存货编码
  238. $list = db('物料_存货编码')
  239. ->where($where)
  240. ->field('rtrim(物料代码) as 物料代码, rtrim(物料名称) as 物料名称, rtrim(规格) as 规格')
  241. ->select();
  242. // 数据处理
  243. $data = [];
  244. foreach ($list as $item) {
  245. // 提取物料代码的前缀
  246. $prefixLength = preg_match('/[a-zA-Z]/', $item['物料代码']) ? [3, 5, 7] : [2, 4, 6];
  247. // 使用普通的数组映射替代箭头函数
  248. $num1 = substr($item['物料代码'], 0, $prefixLength[0]);
  249. $num2 = substr($item['物料代码'], 0, $prefixLength[1]);
  250. $num3 = substr($item['物料代码'], 0, $prefixLength[2]);
  251. // 批量查询物料结构名称
  252. $names = db('物料_存货结构')
  253. ->whereIn('编号', [$num1, $num2, $num3])
  254. ->column('名称', '编号');
  255. // 将数据组织进数组
  256. $name1 = isset($names[$num1]) ? $names[$num1] : '';
  257. $name2 = isset($names[$num2]) ? $names[$num2] : '';
  258. $name3 = isset($names[$num3]) ? $names[$num3] : '';
  259. if (!isset($data["$num1/$name1"][$num2][$num3.'/'.$name3])) {
  260. $data["$num1/$name1"][$num2][$num3.'/'.$name3] = [];
  261. }
  262. $data["$num1/$name1"][$num2][$num3.'/'.$name3][] = "{$item['物料代码']}/{$item['物料名称']}/{$item['规格']}";
  263. }
  264. $this->success('成功', $data);
  265. }
  266. }