| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\QcodeClassification;
- use app\admin\model\QcodeCompany;
- use app\common\controller\Backend;
- use think\Db;
- use \think\Session;
- /**
- *
- *
- * @icon fa fa-circle-o
- */
- class QcodeProduct extends Backend
- {
- /**
- * Product模型对象
- * @var \app\admin\model\QcodeProduct
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\QcodeProduct();
- $qcodeClassification = new QcodeClassification();
- $codeList = $qcodeClassification->where(['status'=>1,'delete_time'=>''])->select();
- $this->view->assign("codeList", $codeList);
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- public function index()
- {
- // 设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- $db3 = \think\Db::connect(config('database.db3'));
- // 获取请求参数
- $req = $this->request->param();
- $sort = isset($req['sort']) ? $req['sort'] : 'Uniqid';
- $order = isset($req['order']) ? $req['order'] : 'desc';
- $offset = isset($req['offset']) ? $req['offset'] : 0;
- $limit = isset($req['limit']) ? $req['limit'] : 10;
- $search = isset($req['search']) ? $req['search'] : '';
- // 先获取基础数据(不包含Gd_cpdh=成品代号的条件)
- $query = $db3->name('工单_基本资料')
- ->field('Gd_gdbh, Gd_cpdh, Gd_cpmc, 成品代号, Uniqid')
- ->where('Gd_cpdh', '<>', '')
- ->where('Gd_cpmc', '<>', '');
- // 搜索条件
- if ($search) {
- $query->where(function($q) use ($search) {
- $q->where('Gd_gdbh', 'like', "%{$search}%")
- ->whereOr('Gd_cpdh', 'like', "%{$search}%")
- ->whereOr('Gd_cpmc', 'like', "%{$search}%");
- });
- }
- // 获取所有符合基础条件的数据
- $allData = $query->order($sort, $order)
- ->select();
- // 用PHP循环过滤出Gd_cpdh等于成品代号的记录
- $filteredData = [];
- foreach ($allData as $item) {
- if ($item['Gd_cpdh'] == $item['成品代号']) {
- $filteredData[] = $item;
- }
- }
- // 手动分页处理
- $total = count($filteredData);
- $rows = array_slice($filteredData, $offset, $limit);
- return json([
- 'total' => $total,
- 'rows' => $rows
- ]);
- }
- return $this->view->fetch();
- //当前是否为关联查询
- // $this->relationSearch = false;
- //设置过滤方法
- // $this->request->filter(['strip_tags', 'trim']);
- // if ($this->request->isAjax()) {
- // //如果发送的来源是Selectpage,则转发到Selectpage
- // if ($this->request->request('keyField')) {
- // return $this->selectpage();
- // }
- // $where = [
- // 'delete_time'=> ''
- // ];
- //
- // $req = input();
- // $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
- // $order = $req['order'];
- // $offset = $req['offset'];
- // $limit = $req['limit'];
- //
- // // 构造模糊查询条件
- //// $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
- //// $filter = ['field' => $regex];
- //
- // $filter = json_decode($req['filter'], true);
- // foreach ($filter as $k => $v){
- // $where[$k] = new \MongoDB\BSON\Regex($v);
- // }
- //
- //
- // $list = $this->model->where($where)
- // ->order($sort,$order)
- // ->limit($limit)
- // ->skip($offset)
- // ->select();
- // foreach ($list as $k=>$v) {
- // $oid = $v['_id']->jsonSerialize();
- // $list[$k]['id'] = $oid['$oid'];
- // }
- //
- // $result = array("total" => count($list), "rows" => $list);
- //
- // return json($result);
- // }
- // return $this->view->fetch();
- }
- /**
- * 产品总列表
- */
- public function products()
- {
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- $where = [
- 'delete_time'=> ''
- ];
- $req = input();
- $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
- $order = $req['order'];
- $offset = $req['offset'];
- $limit = $req['limit'];
- // 构造模糊查询条件
- // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
- // $filter = ['field' => $regex];
- $filter = json_decode($req['filter'], true);
- foreach ($filter as $k => $v){
- $where[$k] = new \MongoDB\BSON\Regex($v);
- }
- $total = $this->model->where($where)->count();
- $list = $this->model->where($where)
- ->order($sort,$order)
- ->limit($limit)
- ->skip($offset)
- ->select();
- foreach ($list as $k=>$v) {
- $oid = $v['_id']->jsonSerialize();
- $list[$k]['id'] = $oid['$oid'];
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 厂商产品列表
- */
- public function product()
- {
- // 设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- // 获取用户信息和请求参数
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $req = $this->request->param();
- // 处理请求参数
- $sort = $req['sort'] ?? 'id';
- $order = $req['order'] ?? 'desc';
- $offset = $req['offset'] ?? 0;
- $limit = $req['limit'] ?? 10;
- $search = $req['search'] ?? '';
- // $filter = json_decode($req['filter'], true);
- $filter = json_decode($req['filter'], true);
- $where = [];
- foreach ($filter as $k => $v){
- $where[$k] = $v;
- }
- // 1. 从qcode_company表获取所有Uniqid
- $db = new QcodeCompany();
- $tableName = $company_id.'_qcode_company';
- $records = $db->name($tableName)
- ->where(['delete_time' => ''])
- ->select();
- // 提取有效的Uniqid值
- $uniqIds = [];
- foreach ($records as $record) {
- if (isset($record['Uniqid'])) {
- $uniqIds[] = $record['Uniqid'];
- }
- }
- // 去重并过滤空值
- $uniqIds = array_unique(array_filter($uniqIds));
- // 如果没有有效的Uniqid,直接返回空结果
- if (empty($uniqIds)) {
- return json([
- 'total' => 0,
- 'rows' => []
- ]);
- }
- // 2. 连接工单基本资料数据库
- $db3 = \think\Db::connect(config('database.db3'));
- // 构建并执行查询(合并版本)
- $result = $db3->name('工单_基本资料')
- ->field('Gd_gdbh, Gd_cpdh, Gd_cpmc, 成品代号, Uniqid')
- ->whereIn('Uniqid', $uniqIds)
- ->where($where)
- // ->where('Gd_gdbh', 'like', "%{$search}%")
- ->limit($offset, $limit)
- ->select();
- // 获取总数(需要单独查询)
- $total = $db3->name('工单_基本资料')
- ->field('Gd_gdbh, Gd_cpdh, Gd_cpmc, 成品代号, Uniqid')
- ->whereIn('Uniqid', $uniqIds)
- ->where('Gd_gdbh', 'like', "%{$search}%")
- ->limit($offset, $limit)
- ->count();
- return json([
- 'total' => $total,
- 'rows' => $result
- ]);
- }
- return $this->view->fetch();
- }
- // public function product()
- // {
- // //当前是否为关联查询
- // $this->relationSearch = false;
- // //设置过滤方法
- // $this->request->filter(['strip_tags', 'trim']);
- // if ($this->request->isAjax()) {
- // //如果发送的来源是Selectpage,则转发到Selectpage
- // if ($this->request->request('keyField')) {
- // return $this->selectpage();
- // }
- // $userInfo = Session::get('admin');
- // $company_id = (int)$userInfo['company'];
- // $where = [
- //// 'company_id'=>(int)$userInfo['company'],
- // 'delete_time'=> ''
- // ];
- //
- // $req = input();
- // $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
- // $order = $req['order'];
- // $offset = $req['offset'];
- // $limit = $req['limit'];
- //
- // // 构造模糊查询条件
- //// $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
- //// $filter = ['field' => $regex];
- //
- // $filter = json_decode($req['filter'], true);
- // foreach ($filter as $k => $v){
- // $where[$k] = new \MongoDB\BSON\Regex($v);
- // }
- //
- // $db = new QcodeCompany();
- // $rows = $db->name($company_id.'_'."qcode_company")
- // ->where('delete_time','')
- // ->column('product_id');
- //
- // $rows = array_values($rows);
- //
- // $total = $this->model->where($where)->where('_id','in',$rows)->count();
- //
- // $list = $this->model->where($where)
- // ->where('_id','in',$rows)
- // ->order($sort,$order)
- // ->limit($limit)
- // ->skip($offset)
- // ->select();
- // foreach ($list as $k=>$v) {
- // $oid = $v['_id']->jsonSerialize();
- // $list[$k]['id'] = $oid['$oid'];
- // }
- //
- // $result = array("total" => $total, "rows" => $list);
- //
- // return json($result);
- // }
- // return $this->view->fetch();
- // }
- /**
- * 新增产品
- */
- public function add()
- {
- if ($this->request->isAjax()){
- $req = $this->request->post();
- if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
- $this->error('请填写编号');
- }else{
- $data = [
- 'product_code'=>$req['row']['product_code'],
- 'delete_time'=>'',
- ];
- if ($this->model->where($data)->find()){
- $this->error('编号已存在');
- }
- }
- if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
- $this->error('请填写名称');
- }else{
- $data = [
- 'product_name'=>$req['row']['product_name'],
- 'delete_time'=>'',
- ];
- if ($this->model->where($data)->find()){
- $this->error('名称已存在');
- }
- }
- if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
- $this->error('请填写辅料代号');
- }
- if(!isset($req['row']['main_unit'])){
- $this->error('请填写主计量单位');
- }
- if(!isset($req['row']['sec_unit'])){
- $this->error('请填写辅计量单位');
- }
- if(!isset($req['row']['proportion'])){
- $this->error('请填写换算关系');
- }
- $data = [
- 'product_code' =>$req['row']['product_code'],
- 'product_name' =>$req['row']['product_name'],
- 'temple' =>$req['row']['temple'],
- 'main_unit' =>$req['row']['main_unit'],
- 'sec_unit' =>$req['row']['sec_unit'],
- 'proportion' =>$req['row']['proportion'],
- 'code' =>substr($req['row']['product_code'],0,4),
- 'create_time' =>time(),
- ];
- //插入数据
- $re = $this->model->save($data);
- if($re){
- $this->success('成功');
- }else{
- $this->error('失败');
- }
- }
- return $this->view->fetch();
- }
- /**
- * 编辑产品
- */
- public function edit($ids = NULL)
- {
- if ($this->request->isAjax()){
- $req = $this->request->post();
- if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
- $this->error('请填写编号');
- }else{
- $data = [
- 'product_code'=>$req['row']['product_code'],
- 'delete_time'=>'',
- ];
- if ($this->model->where($data)->where('_id','neq',$ids)->find()){
- $this->error('编号已存在');
- }
- }
- if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
- $this->error('请填写名称');
- }else{
- $data = [
- 'product_name'=>$req['row']['product_name'],
- 'delete_time'=>'',
- ];
- if ($this->model->where($data)->where('_id','neq',$ids)->find()){
- $this->error('名称已存在');
- }
- }
- if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
- $this->error('请填写辅料代号');
- }
- if(!isset($req['row']['main_unit'])){
- $this->error('请填写主计量单位');
- }
- if(!isset($req['row']['sec_unit'])){
- $this->error('请填写辅计量单位');
- }
- if(!isset($req['row']['proportion'])){
- $this->error('请填写换算关系');
- }
- $data = [
- 'product_code' =>$req['row']['product_code'],
- 'product_name' =>$req['row']['product_name'],
- 'temple' =>$req['row']['temple'],
- 'main_unit' =>$req['row']['main_unit'],
- 'sec_unit' =>$req['row']['sec_unit'],
- 'proportion' =>$req['row']['proportion'],
- 'update_time' =>time(),
- ];
- //修改数据
- $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
- $re = $department->save($data);
- if($re){
- $this->success('修改成功');
- }else{
- $this->error('修改失败');
- }
- }
- $row = $this->model->where('_id',$ids)->find();
- $this->view->assign('row',$row);
- return $this->view->fetch();
- }
- /**
- * 删除产品
- */
- public function del($ids = NULL)
- {
- $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
- $department->delete_time = time();
- $re = $department->save();
- if($re){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- /**
- * 配置产品
- */
- public function bind($ids = NULL)
- {
- // 连接数据库并查询工单基本资料
- $db3 = \think\Db::connect(config('database.db3'));
- $results = $db3->name('工单_基本资料')
- ->whereIn('Uniqid', $ids)
- ->field('Gd_gdbh, Gd_cpdh, Gd_cpmc, 成品代号, Uniqid')
- ->select();
- // 获取用户信息
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $db = new QcodeCompany();
- $tableName = $company_id.'_'."qcode_company";
- // 循环处理每个查询结果
- foreach ($results as $item) {
- // 查询是否已经添加
- $row = $db->name($tableName)
- ->where([
- 'delete_time' => '',
- 'Uniqid' => $item['Uniqid'] // 将product_id改为Uniqid
- ])
- ->find();
- if ($row) {
- continue; // 如果已存在则跳过
- }
- // 准备插入数据
- $data = [
- 'Uniqid' => $item['Uniqid'], // 改为Uniqid
- 'Gd_gdbh' => $item['Gd_gdbh'], // 添加工单编号
- 'Gd_cpdh' => $item['Gd_cpdh'], // 添加产品代号
- 'Gd_cpmc' => $item['Gd_cpmc'], // 添加产品名称
- 'delete_time' => '',
- 'create_time' => time(),
- ];
- // 插入数据
- $bool = $db->name($tableName)->insert($data);
- if (!$bool) {
- $this->error('添加失败: Uniqid='.$item['Uniqid']);
- }
- }
- $this->success('添加完成');
- // $userInfo = Session::get('admin');
- // $company_id = (int)$userInfo['company'];
- // $db = new QcodeCompany();
- // //查询是否已经添加
- // $row = $db->name($company_id.'_'."qcode_company")
- // ->where(['delete_time'=>'','product_id'=>$ids])
- // ->find();
- // if($row) $this->success('已添加');
- //
- // $data = [
- // 'product_id'=>$ids,
- // 'create_time'=>time(),
- // ];
- //
- // //插入qcode_company数据
- // $bool = $db->save($data);
- // if($bool){
- // $this->success('添加成功');
- // }else{
- // $this->error('添加失败');
- // }
- }
- /**
- * 取消配置产品
- */
- public function unbind($ids = NULL)
- {
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $db = new QcodeCompany();
- //查询是否已经添加
- $bool = $db->name($company_id.'_'."qcode_company")
- ->where(['delete_time'=>'','product_id'=>$ids])
- ->update(['delete_time'=>time()]);
- if($bool){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- }
|