| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?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);
- $data = [];
- $data['product_code'] = $params['product_code'];
- $data['product_name'] = $params['product_name'];
- $data['product_type'] = $params['product_type'];
- $data['unit'] = $params['unit'];
- $data['Sys_id'] = $params['sys_id'];
- $data['status'] = 1;
- $data['Sys_rq'] = date('Y-m-d H:i:s');
- $result = \db('产品_基本资料')->insert($data);
- 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()) {
- $this->error('非法请求');
- }
- $params = Request::instance()->param();
- $where = [];
- if (!empty($params['search'])) {
- $where['product_code|product_name|product_type'] = array('like', '%'.$params['search'].'%');
- }
- $limit = empty($params['limit']) ? 30 : $params['limit'];
- $pages = empty($params['page']) ? 1 : $params['page'];
- // 1. 查询产品主表
- $productList = db('产品_基本资料')
- ->whereNull('mod_rq')
- ->where($where)
- ->page($pages, $limit)
- ->order('id desc')
- ->select();
- if (empty($productList)) {
- $this->success('获取成功', ['list' => [], 'count' => 0]);
- }
- // 提取产品编号
- $productCodes = array_column($productList, 'product_code');
- // 2. 查询部件(通过 product_code)
- $parts = db('产品_部件资料')
- ->whereNull('mod_rq')
- ->whereIn('product_code', $productCodes)
- ->select();
- // 提取部件编码 part_code
- $partCodes = array_column($parts, 'part_code');
- // 3. 查询工艺(通过 part_code)
- $process = [];
- if (!empty($partCodes)) {
- $process = db('产品_工艺资料')
- ->whereNull('mod_rq')
- ->whereIn('part_code', $partCodes)
- ->select();
- }
- // 部件按 product_code 分组
- $partGroup = [];
- foreach ($parts as $val) {
- $partGroup[$val['product_code']][] = $val;
- }
- // 工艺按 part_code 分组
- $processGroup = [];
- foreach ($process as $val) {
- $processGroup[$val['part_code']][] = $val;
- }
- // 组装数据
- foreach ($productList as &$product) {
- $currentParts = $partGroup[$product['product_code']] ?? [];
- foreach ($currentParts as &$item) {
- $item['processes'] = $processGroup[$item['part_code']] ?? [];
- }
- $product['parts'] = $currentParts;
- }
- // 统计总数
- $count = db('产品_基本资料')
- ->whereNull('mod_rq')
- ->where($where)
- ->count();
- $this->success('获取成功', [
- 'list' => $productList,
- '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']);
- $data = [];
- $data['product_name'] = $params['product_name'];
- $data['product_type'] = $params['product_type'];
- $data['unit'] = $params['unit'];
- $data['Sys_id'] = $params['sys_id'];
- $data['updatetime'] = date('Y-m-d H:i:s');
- echo "<pre>";
- print_r($data);
- echo "<pre>";die;
- $result = \db('产品_基本资料')
- ->where('id', $params['id'])
- ->update($data);
- 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' => date('Y-m-d H:i:s')]);
- 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]);
- }
- }
|