| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use Monolog\Handler\IFTTTHandler;
- use Overtrue\Socialite\Providers\WeWorkProvider;
- use think\Db;
- use think\Request;
- use function fast\e;
- /**
- *
- * 产品部件库
- */
- class PartLib extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function index(){
- $this->success('产品_部件资料库');
- }
- /**
- * 新增产品部件库
- * 支持批量新增,自动生成 BJ000001 格式编码
- */
- public function ParAdd(){
- if (Request::instance()->isPost() == false){
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- $sys_id = $params['sys_id'];
- $part_list = $params['part_list'];
- if (empty($part_list)) {
- $this->error('请选择要新增的部件');
- }
- // 获取表中最大的部件编码
- // 例如:BJ000007 -> 取 7 -> +1 -> 8 -> 拼接为 BJ000008
- $maxCode = Db::name('产品_部件库')
- ->whereNull('mod_rq')
- ->order('part_code desc')
- ->value('part_code');
- $nextNum = 1; // 默认从1开始
- if ($maxCode) {
- // 提取数字部分并加1
- $num = intval(str_replace('BJ', '', $maxCode)) + 1;
- $nextNum = $num;
- }
- //批量组装数据
- $insertData = [];
- foreach ($part_list as $item) {
- $part_name = $item['part_name'];
- if (empty($part_name)) continue;
- // 生成格式化编码 (BJ000001)
- $part_code = 'BJ' . str_pad($nextNum, 6, '0', STR_PAD_LEFT);
- $insertData[] = [
- 'part_code' => $part_code,
- 'part_name' => $part_name,
- 'sys_id' => $sys_id,
- 'sys_rq' => date('Y-m-d H:i:s'),
- 'updatetime' => null,
- 'mod_rq' => null
- ];
- $nextNum++; // 序号自增
- }
- if (empty($insertData)) {
- $this->error('无有效数据');
- }
- $res = Db::name('产品_部件库')->insertAll($insertData);
- if ($res !== false){
- $this->success('新增成功');
- }else{
- $this->error('新增失败');
- }
- }
- /**
- * 获取产品部件库
- */
- public function ParList(){
- if (Request::instance()->isGet() == false){
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- $where = [];
- if (!empty($params['search'])){
- $where['part_code|part_name'] = array('like','%'.$params['search'].'%');
- }
- $limit = $params['limit'];
- if (empty($limit)){
- $limit = 30;
- }
- $pages = $params['page'];
- if (empty($pages)){
- $pages = 1;
- }
- $list = Db::name('产品_部件库')
- ->field('
- id,
- part_code as 部件编码,
- part_name as 部件名称,
- sys_id,
- sys_rq as 创建时间,
- updatetime as 修改时间
- ')
- ->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
- $total = Db::name('产品_部件库')->whereNull('mod_rq')->where($where)->count();
- $data['list'] = $list;
- $data['total'] = $total;
- $this->success('获取成功',$data);
- }
- /**
- * 修改产品部件库
- */
- public function ParEdit(){
- if (Request::instance()->isPost() == false){
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- //参数校验
- if (empty($params['id']) || empty($params['part_name'])) {
- $this->error('参数不能为空');
- }
- //修改数据
- $data = [
- 'part_name' => $params['part_name'],
- 'sys_id' => $params['sys_id'],
- 'updatetime' => date('Y-m-d H:i:s')
- ];
- $res = Db::name('产品_部件库')
- ->where('id', $params['id'])
- ->whereNull('mod_rq')
- ->update($data);
- if ($res !== false) {
- $this->success('修改成功');
- } else {
- $this->error('修改失败');
- }
- }
- /**
- * 删除产品部件库
- */
- public function ParDelete(){
- if (Request::instance()->isPost() == false){
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- if (empty($params['id'])) {
- $this->error('请选择要删除的数据');
- }
- $res = Db::name('产品_部件库')
- ->where('id', $params['id'])
- ->update(['mod_rq' => date('Y-m-d H:i:s')]);
- if ($res !== false) {
- $this->success('删除成功');
- } else {
- $this->error('删除失败');
- }
- }
- }
|