Files
swiftadmin/app/admin/controller/PdmPartlist.php
2025-05-03 08:52:41 +08:00

165 lines
5.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\AdminController;
use Webman\Http\Request;
use app\admin\model\PdmPartlist as PdmPartlistModel;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* pdm_partlist
* BOM查询
* <!--partlist-->
* Class PdmPartlist
* @package app\admin\controller
*/
class PdmPartlist extends AdminController
{
/**
* PdmPartlist模型对象
* @var \app\admin\model\PdmPartlist
*/
public function __construct()
{
parent::__construct();
$this->model = new PdmPartlistModel;
}
/**
* 默认生成的方法为index/add/edit/del/status 五个方法
* 当创建CURD的时候DIY的函数体和模板为空请自行编写代码
*/
/**
* 字典首页
* @return Response
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function index(): \support\Response
{
// $post = input();
// $post = request()->post();
$post = request()->all();
// $post['partnumber'] = input('partnumber');
$pid = input('pid');
$limit = input('limit') ?? 10;
$page = input('page') ?? 1;
if ($pid == null) {
$pid = (string)$this->model->minId();
}
if (request()->isAjax()) {
// 生成查询数据
$pid = !str_contains($pid, ',') ? $pid : explode(',',$pid);
$page = (int)input('page', 1);
$limit = (int)input('limit', 10);
$where = $this->buildSelectParams();
$fieldList = $this->model->getFields();
// $order = !array_key_exists('sort', $fieldList) ? 'id' : 'sort';
$order = 'id' ;
$where[] = ['pid','in',$pid];
if (!empty($post['keyword'])) {
$where[] = ['partnumber|reference|content|item','like','%'.$post['keyword'].'%'];
// $where[] = ['reference','like','%'.$post['keyword'].'%'];
// $where[] = ['content','like','%'.$post['keyword'].'%'];
// $where[] = ['item','like','%'.$post['keyword'].'%'];
}else{
// $where[] = ['pid','in',$pid];
// $where[] = ['partnumber','like','%'.$post['partnumber'].'%'];
}
// throw new \Exception(json_encode($where));
$count = $this->model->where($where)->count();
$export = input('export',0);
if($export>0){
$subQuery = $this->model->field('id')->where($where)->order($order, 'asc')->buildSql(); // asc or desc
}else{
$subQuery = $this->model->field('id')->where($where)->order($order, 'asc')->limit($limit)->page($page)->buildSql();
}
$subQuery = '( SELECT object.id FROM ' . $subQuery . ' AS object )';
$list = $this->model->with($this->relationModel)->where('id in' . $subQuery)->order($order, 'asc')->select() // 如果是'desc' list_to_tree 函数返回为空!
// $list = $this->model->where($where)->limit((int)$limit)->page((int)$page)->select()
->each(function($item,$key) use ($pid){
if ($key == 0 && $pid == '0') {
$item['LAY_CHECKED'] = true;
}
// throw new \Exception(json_encode($item->admin['nickname']));
$item['creator'] = $item->admin ==null?'': $item->admin['nickname'];
// $item['purchasecode'] = $item->partitemview == null?'': $item->partitemview['purchasecode'];
$item->partitemview ;
return $item;
});
// throw new \Exception(json_encode($list));
return $this->success('查询成功', null, $list, $count);
}
return view('/pdm_partlist/index',[ 'pid' => $pid]);
}
/**
* 更新数据到CIS数据表
* @return Response
* @throws DbException
*/
public function sync(): Response
{
//$lists = json_decode( input('data'));
$lists = input('data');
$bomid = input('bomid') ;
// throw new \Exception(json_encode($lists));
if(!empty($bomid)){
$status = $this->model->destroy(['pid' => $bomid]);
if($status){
} else{
return $this->error('BOM ID:'.$bomid.',删除数据失败!');
}
}
foreach( $lists as &$item){
// 添加或修改 'pid' 键
$item['pid'] = $bomid;
// 检查 '备注' 键是否存在
// if (isset($item['备注'])) {
// // 将 '备注' 的值赋给 'content'
// $item['content'] = $item['备注'];
// // 移除 '备注' 键
// unset($item['备注']);
// } else {
// // 可选:如果 '备注' 键不存在,可以设置一个默认值或记录日志
// $item['content'] = ''; // 或其他默认值
// // error_log("缺少 '备注' 字段,键值: " . $key);
// }
// 可选:处理 'id' 键
if (!empty($item['id'])) {
unset($item['id']);
}
}
// throw new \Exception(json_encode($lists));
$status = $this->model->saveAll($lists);
if($status){
return $this->success('BOM ID:'.$bomid.'更新数据成功。');
// return $this->success();
} else{
return $this->error('BOM ID:'.$bomid.',更新数据失败!');
}
return $this->error('更新失败,请检查您的参数!');
}
}