QcodeProduct.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeClassification;
  4. use app\admin\model\QcodeCompany;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use \think\Session;
  8. /**
  9. *
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class QcodeProduct extends Backend
  14. {
  15. /**
  16. * Product模型对象
  17. * @var \app\admin\model\QcodeProduct
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\QcodeProduct();
  24. $qcodeClassification = new QcodeClassification();
  25. $codeList = $qcodeClassification->where(['status'=>1,'delete_time'=>''])->select();
  26. $this->view->assign("codeList", $codeList);
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. public function index()
  34. {
  35. // 设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax()) {
  38. $db3 = \think\Db::connect(config('database.db3'));
  39. // 获取请求参数
  40. $req = $this->request->param();
  41. $sort = isset($req['sort']) ? $req['sort'] : 'Uniqid';
  42. $order = isset($req['order']) ? $req['order'] : 'desc';
  43. $offset = isset($req['offset']) ? $req['offset'] : 0;
  44. $limit = isset($req['limit']) ? $req['limit'] : 10;
  45. $search = isset($req['search']) ? $req['search'] : '';
  46. // 先获取基础数据(不包含Gd_cpdh=成品代号的条件)
  47. $query = $db3->name('工单_基本资料')
  48. ->field('Gd_gdbh, Gd_cpdh, Gd_cpmc, 成品代号, Uniqid')
  49. ->where('Gd_cpdh', '<>', '')
  50. ->where('Gd_cpmc', '<>', '');
  51. // 搜索条件
  52. if ($search) {
  53. $query->where(function($q) use ($search) {
  54. $q->where('Gd_gdbh', 'like', "%{$search}%")
  55. ->whereOr('Gd_cpdh', 'like', "%{$search}%")
  56. ->whereOr('Gd_cpmc', 'like', "%{$search}%");
  57. });
  58. }
  59. // 获取所有符合基础条件的数据
  60. $allData = $query->order($sort, $order)
  61. ->select();
  62. // 用PHP循环过滤出Gd_cpdh等于成品代号的记录
  63. $filteredData = [];
  64. foreach ($allData as $item) {
  65. if ($item['Gd_cpdh'] == $item['成品代号']) {
  66. $filteredData[] = $item;
  67. }
  68. }
  69. // 手动分页处理
  70. $total = count($filteredData);
  71. $rows = array_slice($filteredData, $offset, $limit);
  72. return json([
  73. 'total' => $total,
  74. 'rows' => $rows
  75. ]);
  76. }
  77. return $this->view->fetch();
  78. //当前是否为关联查询
  79. // $this->relationSearch = false;
  80. //设置过滤方法
  81. // $this->request->filter(['strip_tags', 'trim']);
  82. // if ($this->request->isAjax()) {
  83. // //如果发送的来源是Selectpage,则转发到Selectpage
  84. // if ($this->request->request('keyField')) {
  85. // return $this->selectpage();
  86. // }
  87. // $where = [
  88. // 'delete_time'=> ''
  89. // ];
  90. //
  91. // $req = input();
  92. // $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  93. // $order = $req['order'];
  94. // $offset = $req['offset'];
  95. // $limit = $req['limit'];
  96. //
  97. // // 构造模糊查询条件
  98. //// $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  99. //// $filter = ['field' => $regex];
  100. //
  101. // $filter = json_decode($req['filter'], true);
  102. // foreach ($filter as $k => $v){
  103. // $where[$k] = new \MongoDB\BSON\Regex($v);
  104. // }
  105. //
  106. //
  107. // $list = $this->model->where($where)
  108. // ->order($sort,$order)
  109. // ->limit($limit)
  110. // ->skip($offset)
  111. // ->select();
  112. // foreach ($list as $k=>$v) {
  113. // $oid = $v['_id']->jsonSerialize();
  114. // $list[$k]['id'] = $oid['$oid'];
  115. // }
  116. //
  117. // $result = array("total" => count($list), "rows" => $list);
  118. //
  119. // return json($result);
  120. // }
  121. // return $this->view->fetch();
  122. }
  123. /**
  124. * 产品总列表
  125. */
  126. public function products()
  127. {
  128. //当前是否为关联查询
  129. $this->relationSearch = false;
  130. //设置过滤方法
  131. $this->request->filter(['strip_tags', 'trim']);
  132. if ($this->request->isAjax()) {
  133. //如果发送的来源是Selectpage,则转发到Selectpage
  134. if ($this->request->request('keyField')) {
  135. return $this->selectpage();
  136. }
  137. $where = [
  138. 'delete_time'=> ''
  139. ];
  140. $req = input();
  141. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  142. $order = $req['order'];
  143. $offset = $req['offset'];
  144. $limit = $req['limit'];
  145. // 构造模糊查询条件
  146. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  147. // $filter = ['field' => $regex];
  148. $filter = json_decode($req['filter'], true);
  149. foreach ($filter as $k => $v){
  150. $where[$k] = new \MongoDB\BSON\Regex($v);
  151. }
  152. $total = $this->model->where($where)->count();
  153. $list = $this->model->where($where)
  154. ->order($sort,$order)
  155. ->limit($limit)
  156. ->skip($offset)
  157. ->select();
  158. foreach ($list as $k=>$v) {
  159. $oid = $v['_id']->jsonSerialize();
  160. $list[$k]['id'] = $oid['$oid'];
  161. }
  162. $result = array("total" => $total, "rows" => $list);
  163. return json($result);
  164. }
  165. return $this->view->fetch();
  166. }
  167. /**
  168. * 厂商产品列表
  169. */
  170. public function product()
  171. {
  172. // 设置过滤方法
  173. $this->request->filter(['strip_tags', 'trim']);
  174. if ($this->request->isAjax()) {
  175. // 获取用户信息和请求参数
  176. $userInfo = Session::get('admin');
  177. $company_id = (int)$userInfo['company'];
  178. $req = $this->request->param();
  179. // 处理请求参数
  180. $sort = $req['sort'] ?? 'id';
  181. $order = $req['order'] ?? 'desc';
  182. $offset = $req['offset'] ?? 0;
  183. $limit = $req['limit'] ?? 10;
  184. $search = $req['search'] ?? '';
  185. // $filter = json_decode($req['filter'], true);
  186. $filter = json_decode($req['filter'], true);
  187. $where = [];
  188. foreach ($filter as $k => $v){
  189. $where[$k] = $v;
  190. }
  191. // 1. 从qcode_company表获取所有Uniqid
  192. $db = new QcodeCompany();
  193. $tableName = $company_id.'_qcode_company';
  194. $records = $db->name($tableName)
  195. ->where(['delete_time' => ''])
  196. ->select();
  197. // 提取有效的Uniqid值
  198. $uniqIds = [];
  199. foreach ($records as $record) {
  200. if (isset($record['Uniqid'])) {
  201. $uniqIds[] = $record['Uniqid'];
  202. }
  203. }
  204. // 去重并过滤空值
  205. $uniqIds = array_unique(array_filter($uniqIds));
  206. // 如果没有有效的Uniqid,直接返回空结果
  207. if (empty($uniqIds)) {
  208. return json([
  209. 'total' => 0,
  210. 'rows' => []
  211. ]);
  212. }
  213. // 2. 连接工单基本资料数据库
  214. $db3 = \think\Db::connect(config('database.db3'));
  215. // 构建并执行查询(合并版本)
  216. $result = $db3->name('工单_基本资料')
  217. ->field('Gd_gdbh, Gd_cpdh, Gd_cpmc, 成品代号, Uniqid')
  218. ->whereIn('Uniqid', $uniqIds)
  219. ->where($where)
  220. // ->where('Gd_gdbh', 'like', "%{$search}%")
  221. ->limit($offset, $limit)
  222. ->select();
  223. // 获取总数(需要单独查询)
  224. $total = $db3->name('工单_基本资料')
  225. ->field('Gd_gdbh, Gd_cpdh, Gd_cpmc, 成品代号, Uniqid')
  226. ->whereIn('Uniqid', $uniqIds)
  227. ->where('Gd_gdbh', 'like', "%{$search}%")
  228. ->limit($offset, $limit)
  229. ->count();
  230. return json([
  231. 'total' => $total,
  232. 'rows' => $result
  233. ]);
  234. }
  235. return $this->view->fetch();
  236. }
  237. // public function product()
  238. // {
  239. // //当前是否为关联查询
  240. // $this->relationSearch = false;
  241. // //设置过滤方法
  242. // $this->request->filter(['strip_tags', 'trim']);
  243. // if ($this->request->isAjax()) {
  244. // //如果发送的来源是Selectpage,则转发到Selectpage
  245. // if ($this->request->request('keyField')) {
  246. // return $this->selectpage();
  247. // }
  248. // $userInfo = Session::get('admin');
  249. // $company_id = (int)$userInfo['company'];
  250. // $where = [
  251. //// 'company_id'=>(int)$userInfo['company'],
  252. // 'delete_time'=> ''
  253. // ];
  254. //
  255. // $req = input();
  256. // $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  257. // $order = $req['order'];
  258. // $offset = $req['offset'];
  259. // $limit = $req['limit'];
  260. //
  261. // // 构造模糊查询条件
  262. //// $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  263. //// $filter = ['field' => $regex];
  264. //
  265. // $filter = json_decode($req['filter'], true);
  266. // foreach ($filter as $k => $v){
  267. // $where[$k] = new \MongoDB\BSON\Regex($v);
  268. // }
  269. //
  270. // $db = new QcodeCompany();
  271. // $rows = $db->name($company_id.'_'."qcode_company")
  272. // ->where('delete_time','')
  273. // ->column('product_id');
  274. //
  275. // $rows = array_values($rows);
  276. //
  277. // $total = $this->model->where($where)->where('_id','in',$rows)->count();
  278. //
  279. // $list = $this->model->where($where)
  280. // ->where('_id','in',$rows)
  281. // ->order($sort,$order)
  282. // ->limit($limit)
  283. // ->skip($offset)
  284. // ->select();
  285. // foreach ($list as $k=>$v) {
  286. // $oid = $v['_id']->jsonSerialize();
  287. // $list[$k]['id'] = $oid['$oid'];
  288. // }
  289. //
  290. // $result = array("total" => $total, "rows" => $list);
  291. //
  292. // return json($result);
  293. // }
  294. // return $this->view->fetch();
  295. // }
  296. /**
  297. * 新增产品
  298. */
  299. public function add()
  300. {
  301. if ($this->request->isAjax()){
  302. $req = $this->request->post();
  303. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  304. $this->error('请填写编号');
  305. }else{
  306. $data = [
  307. 'product_code'=>$req['row']['product_code'],
  308. 'delete_time'=>'',
  309. ];
  310. if ($this->model->where($data)->find()){
  311. $this->error('编号已存在');
  312. }
  313. }
  314. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  315. $this->error('请填写名称');
  316. }else{
  317. $data = [
  318. 'product_name'=>$req['row']['product_name'],
  319. 'delete_time'=>'',
  320. ];
  321. if ($this->model->where($data)->find()){
  322. $this->error('名称已存在');
  323. }
  324. }
  325. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  326. $this->error('请填写辅料代号');
  327. }
  328. if(!isset($req['row']['main_unit'])){
  329. $this->error('请填写主计量单位');
  330. }
  331. if(!isset($req['row']['sec_unit'])){
  332. $this->error('请填写辅计量单位');
  333. }
  334. if(!isset($req['row']['proportion'])){
  335. $this->error('请填写换算关系');
  336. }
  337. $data = [
  338. 'product_code' =>$req['row']['product_code'],
  339. 'product_name' =>$req['row']['product_name'],
  340. 'temple' =>$req['row']['temple'],
  341. 'main_unit' =>$req['row']['main_unit'],
  342. 'sec_unit' =>$req['row']['sec_unit'],
  343. 'proportion' =>$req['row']['proportion'],
  344. 'code' =>substr($req['row']['product_code'],0,4),
  345. 'create_time' =>time(),
  346. ];
  347. //插入数据
  348. $re = $this->model->save($data);
  349. if($re){
  350. $this->success('成功');
  351. }else{
  352. $this->error('失败');
  353. }
  354. }
  355. return $this->view->fetch();
  356. }
  357. /**
  358. * 编辑产品
  359. */
  360. public function edit($ids = NULL)
  361. {
  362. if ($this->request->isAjax()){
  363. $req = $this->request->post();
  364. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  365. $this->error('请填写编号');
  366. }else{
  367. $data = [
  368. 'product_code'=>$req['row']['product_code'],
  369. 'delete_time'=>'',
  370. ];
  371. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  372. $this->error('编号已存在');
  373. }
  374. }
  375. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  376. $this->error('请填写名称');
  377. }else{
  378. $data = [
  379. 'product_name'=>$req['row']['product_name'],
  380. 'delete_time'=>'',
  381. ];
  382. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  383. $this->error('名称已存在');
  384. }
  385. }
  386. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  387. $this->error('请填写辅料代号');
  388. }
  389. if(!isset($req['row']['main_unit'])){
  390. $this->error('请填写主计量单位');
  391. }
  392. if(!isset($req['row']['sec_unit'])){
  393. $this->error('请填写辅计量单位');
  394. }
  395. if(!isset($req['row']['proportion'])){
  396. $this->error('请填写换算关系');
  397. }
  398. $data = [
  399. 'product_code' =>$req['row']['product_code'],
  400. 'product_name' =>$req['row']['product_name'],
  401. 'temple' =>$req['row']['temple'],
  402. 'main_unit' =>$req['row']['main_unit'],
  403. 'sec_unit' =>$req['row']['sec_unit'],
  404. 'proportion' =>$req['row']['proportion'],
  405. 'update_time' =>time(),
  406. ];
  407. //修改数据
  408. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  409. $re = $department->save($data);
  410. if($re){
  411. $this->success('修改成功');
  412. }else{
  413. $this->error('修改失败');
  414. }
  415. }
  416. $row = $this->model->where('_id',$ids)->find();
  417. $this->view->assign('row',$row);
  418. return $this->view->fetch();
  419. }
  420. /**
  421. * 删除产品
  422. */
  423. public function del($ids = NULL)
  424. {
  425. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  426. $department->delete_time = time();
  427. $re = $department->save();
  428. if($re){
  429. $this->success('删除成功');
  430. }else{
  431. $this->error('删除失败');
  432. }
  433. }
  434. /**
  435. * 配置产品
  436. */
  437. public function bind($ids = NULL)
  438. {
  439. // 连接数据库并查询工单基本资料
  440. $db3 = \think\Db::connect(config('database.db3'));
  441. $results = $db3->name('工单_基本资料')
  442. ->whereIn('Uniqid', $ids)
  443. ->field('Gd_gdbh, Gd_cpdh, Gd_cpmc, 成品代号, Uniqid')
  444. ->select();
  445. // 获取用户信息
  446. $userInfo = Session::get('admin');
  447. $company_id = (int)$userInfo['company'];
  448. $db = new QcodeCompany();
  449. $tableName = $company_id.'_'."qcode_company";
  450. // 循环处理每个查询结果
  451. foreach ($results as $item) {
  452. // 查询是否已经添加
  453. $row = $db->name($tableName)
  454. ->where([
  455. 'delete_time' => '',
  456. 'Uniqid' => $item['Uniqid'] // 将product_id改为Uniqid
  457. ])
  458. ->find();
  459. if ($row) {
  460. continue; // 如果已存在则跳过
  461. }
  462. // 准备插入数据
  463. $data = [
  464. 'Uniqid' => $item['Uniqid'], // 改为Uniqid
  465. 'Gd_gdbh' => $item['Gd_gdbh'], // 添加工单编号
  466. 'Gd_cpdh' => $item['Gd_cpdh'], // 添加产品代号
  467. 'Gd_cpmc' => $item['Gd_cpmc'], // 添加产品名称
  468. 'delete_time' => '',
  469. 'create_time' => time(),
  470. ];
  471. // 插入数据
  472. $bool = $db->name($tableName)->insert($data);
  473. if (!$bool) {
  474. $this->error('添加失败: Uniqid='.$item['Uniqid']);
  475. }
  476. }
  477. $this->success('添加完成');
  478. // $userInfo = Session::get('admin');
  479. // $company_id = (int)$userInfo['company'];
  480. // $db = new QcodeCompany();
  481. // //查询是否已经添加
  482. // $row = $db->name($company_id.'_'."qcode_company")
  483. // ->where(['delete_time'=>'','product_id'=>$ids])
  484. // ->find();
  485. // if($row) $this->success('已添加');
  486. //
  487. // $data = [
  488. // 'product_id'=>$ids,
  489. // 'create_time'=>time(),
  490. // ];
  491. //
  492. // //插入qcode_company数据
  493. // $bool = $db->save($data);
  494. // if($bool){
  495. // $this->success('添加成功');
  496. // }else{
  497. // $this->error('添加失败');
  498. // }
  499. }
  500. /**
  501. * 取消配置产品
  502. */
  503. public function unbind($ids = NULL)
  504. {
  505. $userInfo = Session::get('admin');
  506. $company_id = (int)$userInfo['company'];
  507. $db = new QcodeCompany();
  508. //查询是否已经添加
  509. $bool = $db->name($company_id.'_'."qcode_company")
  510. ->where(['delete_time'=>'','product_id'=>$ids])
  511. ->update(['delete_time'=>time()]);
  512. if($bool){
  513. $this->success('删除成功');
  514. }else{
  515. $this->error('删除失败');
  516. }
  517. }
  518. }