| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use \think\Request;
- use \think\Db;
- use think\Cache;
- use function fast\e;
- /**
- * 产品资料
- */
- class Product extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 首页
- */
- public function index()
- {
- $this->success('产品_基本资料');
- }
- /**
- * 产品资料
- */
- /**
- * 新增产品资料
- */
- public function ProductAdd()
- {
- if (!Request::instance()->isPost()) {
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- // 自动生成产品编号:CP000001
- $lastCode = \db('产品_基本资料')->order('id desc')->value('product_code');
- if ($lastCode) {
- $num = intval(str_replace('CP', '', $lastCode)) + 1;
- } else {
- $num = 1;
- }
- $params['product_code'] = 'CP' . str_pad($num, 6, '0', STR_PAD_LEFT);
- $params['Sys_id'] = session('admin_id'); // 或写死 admin
- $params['Sys_rq'] = date('Y-m-d H:i:s');
- $params['mod_rq'] = 0;
- $result = \db('产品_基本资料')->insert($params);
- if ($result) {
- $this->success('新增成功');
- } else {
- $this->error('新增失败');
- }
- }
- /**
- * 获取左侧菜单产品分类列表
- * 按 product_type 分组 → 分类下显示对应产品
- */
- public function ProductTypeMenu()
- {
- if (Request::instance()->isGet() == false){
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- // 获取所有产品
- $productList = \db('产品_基本资料')->whereNull('mod_rq')
- ->column('id,product_code,product_name,product_type');
- $data = [];
- if ($productList) {
- foreach ($productList as $item) {
- $type = $item['product_type'] ?: '未分类';
- // 分类
- $data[$type]['name'] = $type;
- // 分类下的产品
- $data[$type]['list'][] = [
- 'id' => $item['id'],
- 'product_code' => $item['product_code'],
- 'product_name' => $item['product_name']
- ];
- }
- }
- // 转成索引数组返回
- $result = array_values($data);
- $this->success('获取成功', $result);
- }
- /**
- * 获取产品资料列表
- */
- public function ProductList()
- {
- if (Request::instance()->isGet() == false){
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- $where = [];
- if (!empty($params['search'])){
- $where['product_code|product_name|product_type'] = array('like','%'.$params['search'].'%');
- }
- $limit = $params['limit'];
- if (empty($limit)){
- $limit = 30;
- }
- $pages = $params['page'];
- if (empty($pages)){
- $pages = 1;
- }
- $list = \db('产品_基本资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
- $count = \db('产品_基本资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->count();
- $this->success('获取成功', ['list' => $list, 'count' => $count]);
- }
- /**
- * 修改产品资料
- */
- public function ProductEdit()
- {
- if (!Request::instance()->isPost()) {
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- if (empty($params['id'])) {
- $this->error('请选择数据');
- }
- // 禁止修改编号
- unset($params['product_code']);
- $result = \db('产品_基本资料')
- ->where('id', $params['id'])
- ->update($params);
- if ($result !== false) {
- $this->success('修改成功');
- } else {
- $this->error('修改失败');
- }
- }
- /**
- * 删除产品资料(软删除)
- */
- public function ProductDelete()
- {
- if (!Request::instance()->isPost()) {
- $this->error('非法请求');
- }
- $id = input('id');
- if (empty($id)) {
- $this->error('请选择需要删除的数据');
- }
- $ids = explode(',', $id);
- $result = \db('产品_基本资料')
- ->where('id', 'in', $ids)
- ->update(['mod_rq' => 1]);
- if ($result !== false) {
- $this->success('删除成功');
- } else {
- $this->error('删除失败');
- }
- }
- /**
- * 获取产品部件列表
- */
- public function ProductPartList()
- {
- if (Request::instance()->isGet() == false){
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- $where = [];
- if (!empty($params['search'])){
- $where['part_name'] = array('like','%'.$params['search'].'%');
- }
- if (!empty($params['product_code'])){
- $where['product_code'] = array('like','%'.$params['product_code'].'%');
- }
- $limit = $params['limit'];
- if (empty($limit)){
- $limit = 30;
- }
- $pages = $params['page'];
- if (empty($pages)){
- $pages = 1;
- }
- $list = \db('产品_部件资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
- $count = \db('产品_部件资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->count();
- $this->success('获取成功', ['list' => $list, 'count' => $count]);
- }
- /**
- * 获取产品工艺列表
- */
- public function ProductGyList()
- {
- if (Request::instance()->isGet() == false){
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- $where = [];
- if (!empty($params['search'])){
- $where['gy_name'] = array('like','%'.$params['search'].'%');
- }
- if (!empty($params['product_code'])){
- $where['product_code'] = $params['product_code'];
- }
- $limit = $params['limit'];
- if (empty($limit)){
- $limit = 30;
- }
- $pages = $params['page'];
- if (empty($pages)){
- $pages = 1;
- }
- $list = \db('产品_工艺资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
- $count = \db('产品_工艺资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->count();
- $this->success('获取成功', ['list' => $list, 'count' => $count]);
- }
- }
|