Product.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. $data = [];
  43. $data['product_code'] = $params['product_code'];
  44. $data['product_name'] = $params['product_name'];
  45. $data['product_type'] = $params['product_type'];
  46. $data['unit'] = $params['unit'];
  47. $data['Sys_id'] = $params['sys_id'];
  48. $data['status'] = 1;
  49. $data['Sys_rq'] = date('Y-m-d H:i:s');
  50. $result = \db('产品_基本资料')->insert($data);
  51. if ($result) {
  52. $this->success('新增成功');
  53. } else {
  54. $this->error('新增失败');
  55. }
  56. }
  57. /**
  58. * 获取左侧菜单产品分类列表
  59. * 按 product_type 分组 → 分类下显示对应产品
  60. */
  61. public function ProductTypeMenu()
  62. {
  63. if (Request::instance()->isGet() == false){
  64. $this->error('非法请求');
  65. }
  66. $params = Request::instance()->param();
  67. // 获取所有产品
  68. $productList = \db('产品_基本资料')->whereNull('mod_rq')
  69. ->column('id,product_code,product_name,product_type');
  70. $data = [];
  71. if ($productList) {
  72. foreach ($productList as $item) {
  73. $type = $item['product_type'] ?: '未分类';
  74. // 分类
  75. $data[$type]['name'] = $type;
  76. // 分类下的产品
  77. $data[$type]['list'][] = [
  78. 'id' => $item['id'],
  79. 'product_code' => $item['product_code'],
  80. 'product_name' => $item['product_name']
  81. ];
  82. }
  83. }
  84. // 转成索引数组返回
  85. $result = array_values($data);
  86. $this->success('获取成功', $result);
  87. }
  88. /**
  89. * 获取产品资料列表(产品→部件→工艺 三层编号关联)
  90. */
  91. public function ProductList()
  92. {
  93. if (!Request::instance()->isGet()) {
  94. $this->error('非法请求');
  95. }
  96. $params = Request::instance()->param();
  97. $where = [];
  98. if (!empty($params['search'])) {
  99. $where['product_code|product_name|product_type'] = array('like', '%'.$params['search'].'%');
  100. }
  101. $limit = empty($params['limit']) ? 30 : $params['limit'];
  102. $pages = empty($params['page']) ? 1 : $params['page'];
  103. // 1. 查询产品主表
  104. $productList = db('产品_基本资料')
  105. ->whereNull('mod_rq')
  106. ->where($where)
  107. ->page($pages, $limit)
  108. ->order('id desc')
  109. ->select();
  110. if (empty($productList)) {
  111. $this->success('获取成功', ['list' => [], 'count' => 0]);
  112. }
  113. // 提取产品编号
  114. $productCodes = array_column($productList, 'product_code');
  115. // 2. 查询部件(通过 product_code)
  116. $parts = db('产品_部件资料')
  117. ->whereNull('mod_rq')
  118. ->whereIn('product_code', $productCodes)
  119. ->select();
  120. // 提取部件编码 part_code
  121. $partCodes = array_column($parts, 'part_code');
  122. // 3. 查询工艺(通过 part_code)
  123. $process = [];
  124. if (!empty($partCodes)) {
  125. $process = db('产品_工艺资料')
  126. ->whereNull('mod_rq')
  127. ->whereIn('part_code', $partCodes)
  128. ->select();
  129. }
  130. // 部件按 product_code 分组
  131. $partGroup = [];
  132. foreach ($parts as $val) {
  133. $partGroup[$val['product_code']][] = $val;
  134. }
  135. // 工艺按 part_code 分组
  136. $processGroup = [];
  137. foreach ($process as $val) {
  138. $processGroup[$val['part_code']][] = $val;
  139. }
  140. // 组装数据
  141. foreach ($productList as &$product) {
  142. $currentParts = $partGroup[$product['product_code']] ?? [];
  143. foreach ($currentParts as &$item) {
  144. $item['processes'] = $processGroup[$item['part_code']] ?? [];
  145. }
  146. $product['parts'] = $currentParts;
  147. }
  148. // 统计总数
  149. $count = db('产品_基本资料')
  150. ->whereNull('mod_rq')
  151. ->where($where)
  152. ->count();
  153. $this->success('获取成功', [
  154. 'list' => $productList,
  155. 'count' => $count
  156. ]);
  157. }
  158. /**
  159. * 修改产品资料
  160. */
  161. public function ProductEdit()
  162. {
  163. if (!Request::instance()->isPost()) {
  164. $this->error('非法请求');
  165. }
  166. $params = Request::instance()->param();
  167. if (empty($params['id'])) {
  168. $this->error('请选择数据');
  169. }
  170. // 禁止修改编号
  171. unset($params['product_code']);
  172. $data = [];
  173. $data['product_name'] = $params['product_name'];
  174. $data['product_type'] = $params['product_type'];
  175. $data['unit'] = $params['unit'];
  176. $data['Sys_id'] = $params['sys_id'];
  177. $data['updatetime'] = date('Y-m-d H:i:s');
  178. echo "<pre>";
  179. print_r($data);
  180. echo "<pre>";die;
  181. $result = \db('产品_基本资料')
  182. ->where('id', $params['id'])
  183. ->update($data);
  184. if ($result !== false) {
  185. $this->success('修改成功');
  186. } else {
  187. $this->error('修改失败');
  188. }
  189. }
  190. /**
  191. * 删除产品资料(软删除)
  192. */
  193. public function ProductDelete()
  194. {
  195. if (!Request::instance()->isPost()) {
  196. $this->error('非法请求');
  197. }
  198. $id = input('id');
  199. if (empty($id)) {
  200. $this->error('请选择需要删除的数据');
  201. }
  202. $ids = explode(',', $id);
  203. $result = \db('产品_基本资料')
  204. ->where('id', 'in', $ids)
  205. ->update(['mod_rq' => date('Y-m-d H:i:s')]);
  206. if ($result !== false) {
  207. $this->success('删除成功');
  208. } else {
  209. $this->error('删除失败');
  210. }
  211. }
  212. /**
  213. * 获取产品部件列表
  214. */
  215. public function ProductPartList()
  216. {
  217. if (Request::instance()->isGet() == false){
  218. $this->error('非法请求');
  219. }
  220. $params = Request::instance()->param();
  221. $where = [];
  222. if (!empty($params['search'])){
  223. $where['part_name'] = array('like','%'.$params['search'].'%');
  224. }
  225. if (!empty($params['product_code'])){
  226. $where['product_code'] = array('like','%'.$params['product_code'].'%');
  227. }
  228. $limit = $params['limit'];
  229. if (empty($limit)){
  230. $limit = 30;
  231. }
  232. $pages = $params['page'];
  233. if (empty($pages)){
  234. $pages = 1;
  235. }
  236. $list = \db('产品_部件资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
  237. $count = \db('产品_部件资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->count();
  238. $this->success('获取成功', ['list' => $list, 'count' => $count]);
  239. }
  240. /**
  241. * 获取产品工艺列表
  242. */
  243. public function ProductGyList()
  244. {
  245. if (Request::instance()->isGet() == false){
  246. $this->error('非法请求');
  247. }
  248. $params = Request::instance()->param();
  249. $where = [];
  250. if (!empty($params['search'])){
  251. $where['gy_name'] = array('like','%'.$params['search'].'%');
  252. }
  253. if (!empty($params['product_code'])){
  254. $where['product_code'] = $params['product_code'];
  255. }
  256. $limit = $params['limit'];
  257. if (empty($limit)){
  258. $limit = 30;
  259. }
  260. $pages = $params['page'];
  261. if (empty($pages)){
  262. $pages = 1;
  263. }
  264. $list = \db('产品_工艺资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->order('id desc')->select();
  265. $count = \db('产品_工艺资料')->whereNull('mod_rq')->where($where)->page($pages)->limit($limit)->count();
  266. $this->success('获取成功', ['list' => $list, 'count' => $count]);
  267. }
  268. }