Product.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Request;
  5. use \think\Db;
  6. use think\Cache;
  7. use function fast\e;
  8. /**
  9. * 产品资料
  10. */
  11. class Product extends Api
  12. {
  13. protected $noNeedLogin = ['*'];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 首页
  17. */
  18. public function index()
  19. {
  20. $this->success('产品_基本资料');
  21. }
  22. /**
  23. * 新增产品资料
  24. */
  25. public function ProductAdd()
  26. {
  27. if (!Request::instance()->isPost()) {
  28. $this->error('非法请求');
  29. }
  30. $params = Request::instance()->param();
  31. // 自动生成产品编号:CP000001
  32. $lastCode = \db('产品_基本资料')->order('id desc')->value('product_code');
  33. if ($lastCode) {
  34. $num = intval(str_replace('CP', '', $lastCode)) + 1;
  35. } else {
  36. $num = 1;
  37. }
  38. $params['product_code'] = 'CP' . str_pad($num, 6, '0', STR_PAD_LEFT);
  39. $params['Sys_id'] = session('admin_id'); // 或写死 admin
  40. $params['Sys_rq'] = date('Y-m-d H:i:s');
  41. $params['mod_rq'] = 0;
  42. $result = \db('产品_基本资料')->insert($params);
  43. if ($result) {
  44. $this->success('新增成功');
  45. } else {
  46. $this->error('新增失败');
  47. }
  48. }
  49. /**
  50. * 获取左侧菜单产品分类列表
  51. * 按 product_type 分组 → 分类下显示对应产品
  52. */
  53. public function ProductTypeMenu()
  54. {
  55. if (Request::instance()->isGet() == false){
  56. $this->error('非法请求');
  57. }
  58. $params = Request::instance()->param();
  59. // 获取所有产品
  60. $productList = \db('产品_基本资料')->whereNull('mod_rq')
  61. ->column('id,product_code,product_name,product_type');
  62. $data = [];
  63. if ($productList) {
  64. foreach ($productList as $item) {
  65. $type = $item['product_type'] ?: '未分类';
  66. // 分类
  67. $data[$type]['name'] = $type;
  68. // 分类下的产品
  69. $data[$type]['list'][] = [
  70. 'id' => $item['id'],
  71. 'product_code' => $item['product_code'],
  72. 'product_name' => $item['product_name']
  73. ];
  74. }
  75. }
  76. // 转成索引数组返回
  77. $result = array_values($data);
  78. $this->success('获取成功', $result);
  79. }
  80. /**
  81. * 获取产品资料列表
  82. */
  83. public function ProductList()
  84. {
  85. if (Request::instance()->isGet() == false){
  86. $this->error('非法请求');
  87. }
  88. $params = Request::instance()->param();
  89. $where = [];
  90. if (!empty($params['search'])){
  91. $where['product_code|product_name|product_type'] = array('like','%'.$params['search'].'%');
  92. }
  93. $limit = $params['limit'];
  94. if (empty($limit)){
  95. $limit = 30;
  96. }
  97. $pages = $params['page'];
  98. if (empty($pages)){
  99. $pages = 1;
  100. }
  101. $list = \db('产品_基本资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
  102. $count = \db('产品_基本资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->count();
  103. $this->success('获取成功', ['list' => $list, 'count' => $count]);
  104. }
  105. /**
  106. * 修改产品资料
  107. */
  108. public function ProductEdit()
  109. {
  110. if (!Request::instance()->isPost()) {
  111. $this->error('非法请求');
  112. }
  113. $params = Request::instance()->param();
  114. if (empty($params['id'])) {
  115. $this->error('请选择数据');
  116. }
  117. // 禁止修改编号
  118. unset($params['product_code']);
  119. $result = \db('产品_基本资料')
  120. ->where('id', $params['id'])
  121. ->update($params);
  122. if ($result !== false) {
  123. $this->success('修改成功');
  124. } else {
  125. $this->error('修改失败');
  126. }
  127. }
  128. /**
  129. * 删除产品资料(软删除)
  130. */
  131. public function ProductDelete()
  132. {
  133. if (!Request::instance()->isPost()) {
  134. $this->error('非法请求');
  135. }
  136. $id = input('id');
  137. if (empty($id)) {
  138. $this->error('请选择需要删除的数据');
  139. }
  140. $ids = explode(',', $id);
  141. $result = \db('产品_基本资料')
  142. ->where('id', 'in', $ids)
  143. ->update(['mod_rq' => 1]);
  144. if ($result !== false) {
  145. $this->success('删除成功');
  146. } else {
  147. $this->error('删除失败');
  148. }
  149. }
  150. /**
  151. * 获取产品工艺列表
  152. */
  153. public function ProductGyList()
  154. {
  155. if (Request::instance()->isGet() == false){
  156. $this->error('非法请求');
  157. }
  158. $params = Request::instance()->param();
  159. $where = [];
  160. if (!empty($params['search'])){
  161. $where['gy_name'] = array('like','%'.$params['search'].'%');
  162. }
  163. if (!empty($params['product_code'])){
  164. $where['product_code'] = $params['product_code'];
  165. }
  166. $limit = $params['limit'];
  167. if (empty($limit)){
  168. $limit = 30;
  169. }
  170. $pages = $params['page'];
  171. if (empty($pages)){
  172. $pages = 1;
  173. }
  174. $list = \db('产品_工艺资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
  175. $count = \db('产品_工艺资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->count();
  176. $this->success('获取成功', ['list' => $list, 'count' => $count]);
  177. }
  178. /**
  179. * 获取产品部件列表
  180. */
  181. public function ProductPartList()
  182. {
  183. if (Request::instance()->isGet() == false){
  184. $this->error('非法请求');
  185. }
  186. $params = Request::instance()->param();
  187. $where = [];
  188. if (!empty($params['search'])){
  189. $where['part_name'] = array('like','%'.$params['search'].'%');
  190. }
  191. if (!empty($params['product_code'])){
  192. $where['product_code'] = array('like','%'.$params['product_code'].'%');
  193. }
  194. $limit = $params['limit'];
  195. if (empty($limit)){
  196. $limit = 30;
  197. }
  198. $pages = $params['page'];
  199. if (empty($pages)){
  200. $pages = 1;
  201. }
  202. $list = \db('产品_部件资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
  203. $count = \db('产品_部件资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->count();
  204. $this->success('获取成功', ['list' => $list, 'count' => $count]);
  205. }
  206. }