Product.php 6.7 KB

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