PartLib.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 PartLib extends Api
  14. {
  15. protected $noNeedLogin = ['*'];
  16. protected $noNeedRight = ['*'];
  17. public function index(){
  18. $this->success('产品_部件资料库');
  19. }
  20. /**
  21. * 新增产品部件库
  22. * 支持批量新增,自动生成 BJ000001 格式编码
  23. */
  24. public function ParAdd(){
  25. if (Request::instance()->isPost() == false){
  26. $this->error('非法请求');
  27. }
  28. $params = Request::instance()->param();
  29. $sys_id = $params['sys_id'];
  30. $part_list = $params['part_list'];
  31. if (empty($part_list)) {
  32. $this->error('请选择要新增的部件');
  33. }
  34. // 获取表中最大的部件编码
  35. // 例如:BJ000007 -> 取 7 -> +1 -> 8 -> 拼接为 BJ000008
  36. $maxCode = Db::name('产品_部件库')
  37. ->whereNull('mod_rq')
  38. ->order('part_code desc')
  39. ->value('part_code');
  40. $nextNum = 1; // 默认从1开始
  41. if ($maxCode) {
  42. // 提取数字部分并加1
  43. $num = intval(str_replace('BJ', '', $maxCode)) + 1;
  44. $nextNum = $num;
  45. }
  46. //批量组装数据
  47. $insertData = [];
  48. foreach ($part_list as $item) {
  49. $part_name = $item['part_name'];
  50. if (empty($part_name)) continue;
  51. // 生成格式化编码 (BJ000001)
  52. $part_code = 'BJ' . str_pad($nextNum, 6, '0', STR_PAD_LEFT);
  53. $insertData[] = [
  54. 'part_code' => $part_code,
  55. 'part_name' => $part_name,
  56. 'sys_id' => $sys_id,
  57. 'sys_rq' => date('Y-m-d H:i:s'),
  58. 'updatetime' => null,
  59. 'mod_rq' => null
  60. ];
  61. $nextNum++; // 序号自增
  62. }
  63. if (empty($insertData)) {
  64. $this->error('无有效数据');
  65. }
  66. $res = Db::name('产品_部件库')->insertAll($insertData);
  67. if ($res !== false){
  68. $this->success('新增成功');
  69. }else{
  70. $this->error('新增失败');
  71. }
  72. }
  73. /**
  74. * 获取产品部件库
  75. */
  76. public function ParList(){
  77. if (Request::instance()->isGet() == false){
  78. $this->error('非法请求');
  79. }
  80. $params = Request::instance()->param();
  81. $where = [];
  82. if (!empty($params['search'])){
  83. $where['part_code|part_name'] = array('like','%'.$params['search'].'%');
  84. }
  85. $limit = $params['limit'];
  86. if (empty($limit)){
  87. $limit = 30;
  88. }
  89. $pages = $params['page'];
  90. if (empty($pages)){
  91. $pages = 1;
  92. }
  93. $list = Db::name('产品_部件库')
  94. ->field('
  95. id,
  96. part_code as 部件编码,
  97. part_name as 部件名称,
  98. sys_id,
  99. sys_rq as 创建时间,
  100. updatetime as 修改时间
  101. ')
  102. ->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
  103. $total = Db::name('产品_部件库')->whereNull('mod_rq')->where($where)->count();
  104. $data['list'] = $list;
  105. $data['total'] = $total;
  106. $this->success('获取成功',$data);
  107. }
  108. /**
  109. * 修改产品部件库
  110. */
  111. public function ParEdit(){
  112. if (Request::instance()->isPost() == false){
  113. $this->error('非法请求');
  114. }
  115. $params = Request::instance()->param();
  116. //参数校验
  117. if (empty($params['id']) || empty($params['part_name'])) {
  118. $this->error('参数不能为空');
  119. }
  120. //修改数据
  121. $data = [
  122. 'part_name' => $params['part_name'],
  123. 'sys_id' => $params['sys_id'],
  124. 'updatetime' => date('Y-m-d H:i:s')
  125. ];
  126. $res = Db::name('产品_部件库')
  127. ->where('id', $params['id'])
  128. ->whereNull('mod_rq')
  129. ->update($data);
  130. if ($res !== false) {
  131. $this->success('修改成功');
  132. } else {
  133. $this->error('修改失败');
  134. }
  135. }
  136. /**
  137. * 删除产品部件库
  138. */
  139. public function ParDelete(){
  140. if (Request::instance()->isPost() == false){
  141. $this->error('非法请求');
  142. }
  143. $params = Request::instance()->param();
  144. if (empty($params['id'])) {
  145. $this->error('请选择要删除的数据');
  146. }
  147. $res = Db::name('产品_部件库')
  148. ->where('id', $params['id'])
  149. ->update(['mod_rq' => date('Y-m-d H:i:s')]);
  150. if ($res !== false) {
  151. $this->success('删除成功');
  152. } else {
  153. $this->error('删除失败');
  154. }
  155. }
  156. }