QcodeBach.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\QcodeCompany;
  4. use app\admin\model\QcodeProduct;
  5. use app\admin\model\QcodeGsmc;
  6. use app\admin\model\QcodeLarge;
  7. use app\admin\model\QcodeSmall;
  8. use app\common\controller\Backend;
  9. use \think\Session;
  10. use think\Db;
  11. use MongoDB\BSON\ObjectId;
  12. /**
  13. * 批次列表
  14. */
  15. class QcodeBach extends Backend
  16. {
  17. /**
  18. * Product模型对象
  19. * @var \app\admin\model\QcodeBach
  20. */
  21. protected $model = null;
  22. protected $multiFields = 'status';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\QcodeBach();
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = false;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. return $this->view->fetch();
  43. }
  44. /**
  45. * 主表查询
  46. */
  47. public function bach()
  48. {
  49. $this->relationSearch = false;
  50. $this->request->filter(['strip_tags', 'trim']);
  51. if ($this->request->isAjax()) {
  52. // 如果是 Selectpage 请求则跳转
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. $userInfo = Session::get('admin');
  57. $company_id = (int)$userInfo['company'];
  58. // 默认条件:未删除
  59. $where = ['delete_time' => ''];
  60. // 获取前端传参
  61. $req = input();
  62. // 安全处理排序字段和排序方式
  63. $sort = $req['sort'] ?? 'id';
  64. $sort = $sort == 'id' ? '_id' : $sort;
  65. // 定义允许排序的字段,防止注入
  66. $allowedSortFields = ['_id', 'create_time', 'update_time', 'name']; // 按实际情况添加
  67. if (!in_array($sort, $allowedSortFields)) {
  68. $sort = '_id';
  69. }
  70. $orderStr = strtolower($req['order'] ?? 'desc');
  71. $order = $orderStr === 'asc' ? 1 : -1; // MongoDB 排序必须是 1 或 -1
  72. $offset = $req['offset'] ?? 0;
  73. $limit = $req['limit'] ?? 20;
  74. // 解析 filter 筛选条件
  75. // $filter = json_decode($req['filter'], true);
  76. // if (is_array($filter)) {
  77. // foreach ($filter as $k => $v) {
  78. // $where[$k] = new \MongoDB\BSON\Regex($v);
  79. // }
  80. // }
  81. $list = [];
  82. // 超级管理员:查询所有公司
  83. if (isSuperAdmin()) {
  84. $companies = Db::name('admin')
  85. ->field('company')
  86. ->where('company', '<>', '')
  87. ->where('kes', '<>', '')
  88. ->distinct(true)
  89. ->select();
  90. foreach ($companies as $row) {
  91. $cid = $row['company'];
  92. $rows = $this->model->name($cid . '_qcode_bach')
  93. ->where($where)
  94. ->select(); // 不分页,后续统一处理
  95. foreach ($rows as &$item) {
  96. $oid = $item['_id']->jsonSerialize();
  97. $item['id'] = $oid['$oid'];
  98. }
  99. $list = array_merge($list, $rows);
  100. }
  101. // 全局排序(按 create_time 排序)
  102. usort($list, function ($a, $b) use ($sort, $order) {
  103. $valA = $a[$sort] ?? 0;
  104. $valB = $b[$sort] ?? 0;
  105. return $order === 1 ? ($valA <=> $valB) : ($valB <=> $valA);
  106. });
  107. // 总数与分页
  108. $total = count($list);
  109. $list = array_slice($list, $offset, $limit);
  110. } else {
  111. // 普通用户:只查本公司
  112. $total = $this->model->name($company_id . '_qcode_bach')->where($where)->count();
  113. $list = $this->model->name($company_id . '_qcode_bach')->where($where)
  114. ->order([$sort => $order])
  115. ->limit($limit)
  116. ->skip($offset)
  117. ->select();
  118. foreach ($list as &$item) {
  119. $oid = $item['_id']->jsonSerialize();
  120. $item['id'] = $oid['$oid'];
  121. }
  122. }
  123. return json(["total" => $total, "rows" => $list]);
  124. }
  125. return $this->view->fetch();
  126. }
  127. /**
  128. * 大件列表
  129. */
  130. /**
  131. * 大件列表
  132. */
  133. public function large()
  134. {
  135. $this->relationSearch = false;
  136. $this->request->filter(['strip_tags', 'trim']);
  137. if ($this->request->isAjax()) {
  138. if ($this->request->request('keyField')) {
  139. return $this->selectpage();
  140. }
  141. $userInfo = Session::get('admin');
  142. $company_id = (int)$userInfo['company'];
  143. $where = ['delete_time' => ''];
  144. $req = input();
  145. if ($req['filter'] == '{}') {
  146. return json(["total" => 0, "rows" => []]);
  147. }
  148. $sort = ($req['sort'] == 'id') ? '_id' : $req['sort'];
  149. $order = $req['order'];
  150. $filter = json_decode($req['filter'], true);
  151. if (is_array($filter)) {
  152. foreach ($filter as $k => $v) {
  153. $where[$k] = new \MongoDB\BSON\Regex($v);
  154. }
  155. }
  156. $list = [];
  157. $total = 0;
  158. $db = new QcodeLarge();
  159. $qcodeSmall = new QcodeSmall();
  160. // 超级管理员查询所有公司数据
  161. if (isSuperAdmin()) {
  162. $companies = Db::name('admin')
  163. ->field('company')
  164. ->where('company', '<>', '')
  165. ->where('kes', '<>', '')
  166. ->distinct(true)
  167. ->select();
  168. foreach ($companies as $row) {
  169. $cid = $row['company'];
  170. $table = $cid . '_qcode_large';
  171. $rows = $db->name($table)->where($where)->order($sort, $order)->select();
  172. $total += count($rows);
  173. foreach ($rows as $k => $v) {
  174. $oid = $v['_id']->jsonSerialize();
  175. $v['id'] = $oid['$oid'];
  176. $v['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
  177. $v['small_num'] = $qcodeSmall->name($cid . '_qcode_small')->where('large_id', $oid['$oid'])->count();
  178. $v['l_weight'] = floatval($v['l_weight']) / 100;
  179. $list[] = $v;
  180. }
  181. }
  182. } else {
  183. // 普通用户只查自己公司表
  184. $table = $company_id . '_qcode_large';
  185. $list = $db->name($table)->where($where)->order($sort, $order)->select();
  186. $total = count($list);
  187. foreach ($list as $k => $v) {
  188. $oid = $v['_id']->jsonSerialize();
  189. $list[$k]['id'] = $oid['$oid'];
  190. $list[$k]['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
  191. $list[$k]['small_num'] = $qcodeSmall->name($company_id . '_qcode_small')->where('large_id', $oid['$oid'])->count();
  192. $list[$k]['l_weight'] = floatval($v['l_weight']) / 100;
  193. }
  194. }
  195. return json(["total" => $total, "rows" => $list]);
  196. }
  197. return $this->view->fetch();
  198. }
  199. /**
  200. * 小件列表
  201. */
  202. // public function small()
  203. // {
  204. // //设置过滤方法
  205. // $this->request->filter(['strip_tags', 'trim']);
  206. // $req = $this->request->param();
  207. // if ($this->request->isAjax()) {
  208. // //如果发送的来源是Selectpage,则转发到Selectpage
  209. // if ($this->request->request('keyField')) {
  210. // return $this->selectpage();
  211. // }
  212. // $userInfo = Session::get('admin');
  213. // $company_id = (int)$userInfo['company'];
  214. // $where = [
  215. // 'delete_time'=> '',
  216. // 'large_id'=> $req['large_id']
  217. // ];
  218. //
  219. // $req = input();
  220. // $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  221. // $order = $req['order'];
  222. // $offset = $req['offset'];
  223. // $limit = $req['limit'];
  224. //
  225. // $filter = json_decode($req['filter'], true);
  226. // foreach ($filter as $k => $v){
  227. // $where[$k] = new \MongoDB\BSON\Regex($v);
  228. // }
  229. //
  230. // $list = [];
  231. // $total = 0;
  232. //
  233. // $qcodeSmall = new QcodeSmall();
  234. //
  235. // // 超级管理员查询所有公司
  236. // if (isSuperAdmin()) {
  237. // $companies = Db::name('admin')
  238. // ->field('company')
  239. // ->where('company', '<>', '')
  240. // ->where('kes', '<>', '')
  241. // ->distinct(true)
  242. // ->select();
  243. //
  244. //
  245. // foreach ($companies as $row) {
  246. // $cid = $row['company'];
  247. // $table = $cid . '_qcode_small';
  248. //
  249. // // 获取数据(不分页,后面统一分页)
  250. // $rows = $qcodeSmall->name($table)->where($where)->select();
  251. //
  252. // foreach ($rows as &$v) {
  253. // $oid = $v['_id']->jsonSerialize();
  254. // $v['id'] = $oid['$oid'];
  255. // $v['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
  256. // if (strpos($v['l_flow'], '-') !== false) {
  257. // $parts = explode('-', $v['l_flow']);
  258. // $v['l_flows'] = end($parts);
  259. // } else {
  260. // $v['l_flows'] = '';
  261. // }
  262. // }
  263. //
  264. // $list = array_merge($list, $rows);
  265. // }
  266. //
  267. // // 全部公司数据合并后,统一排序
  268. // usort($list, function ($a, $b) use ($sort, $order) {
  269. // $valA = $a[$sort] ?? '';
  270. // $valB = $b[$sort] ?? '';
  271. // return $order === 'asc' ? ($valA <=> $valB) : ($valB <=> $valA);
  272. // });
  273. //
  274. // // 总条数
  275. // $total = count($list);
  276. // // 分页截取
  277. // $list = array_slice($list, $offset, $limit);
  278. // } else {
  279. // // 普通用户仅查本公司
  280. // $table = $company_id . '_qcode_small';
  281. //
  282. // $total = $qcodeSmall->name($table)->where($where)->count();
  283. //
  284. // $list = $qcodeSmall->name($table)->where($where)
  285. // ->order([$sort => $order])
  286. // ->limit($limit)
  287. // ->skip($offset)
  288. // ->select();
  289. //
  290. // foreach ($list as $k => $v) {
  291. // $oid = $v['_id']->jsonSerialize();
  292. // $list[$k]['id'] = $oid['$oid'];
  293. // $list[$k]['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
  294. // }
  295. // }
  296. //
  297. // return json(["total" => $total, "rows" => $list]);
  298. // }
  299. // return $this->view->fetch();
  300. // }
  301. public function small()
  302. {
  303. //设置过滤方法
  304. $this->request->filter(['strip_tags', 'trim']);
  305. $req = $this->request->param();
  306. if ($this->request->isAjax()) {
  307. //如果发送的来源是Selectpage,则转发到Selectpage
  308. if ($this->request->request('keyField')) {
  309. return $this->selectpage();
  310. }
  311. $userInfo = Session::get('admin');
  312. $company_id = (int)$userInfo['company'];
  313. $where = [
  314. 'delete_time'=> '',
  315. 'large_id'=> $req['large_id']
  316. ];
  317. $req = input();
  318. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  319. $order = $req['order'];
  320. $offset = $req['offset'];
  321. $limit = $req['limit'];
  322. // 构造模糊查询条件
  323. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  324. // $filter = ['field' => $regex];
  325. $filter = json_decode($req['filter'], true);
  326. foreach ($filter as $k => $v){
  327. $where[$k] = new \MongoDB\BSON\Regex($v);
  328. }
  329. $qcodeSmall = new QcodeSmall();
  330. $total = $qcodeSmall->name($company_id.'_'."qcode_small")->where($where)->count();
  331. $list = $qcodeSmall->name($company_id.'_'."qcode_small")->where($where)
  332. ->order($sort,$order)
  333. ->limit($limit)
  334. ->skip($offset)
  335. ->select();
  336. foreach ($list as $k=>$v) {
  337. $oid = $v['_id']->jsonSerialize();
  338. $list[$k]['id'] = $oid['$oid'];
  339. $list[$k]['l_flow'] = $req['l_flow'].'-'.$v['l_flow'];
  340. if (strpos($v['l_flow'], '-') !== false) {
  341. $parts = explode('-', $v['l_flow']);
  342. $v['l_flows'] = end($parts);
  343. } else {
  344. $v['l_flows'] = '';
  345. }
  346. }
  347. $result = array("total" => $total, "rows" => $list);
  348. return json($result);
  349. }
  350. return $this->view->fetch();
  351. }
  352. /**
  353. * 小件导出状态修改(支持超级管理员操作单个公司表)
  354. * 支持处理单个ID和多个ID(逗号分隔)的情况
  355. */
  356. public function small_status()
  357. {
  358. $req = $this->request->param();
  359. $ids = $req['ids'];
  360. // 统一处理ids参数,转换为数组
  361. if (!is_array($ids)) {
  362. $ids = explode(',', $ids);
  363. }
  364. // 过滤空值
  365. $ids = array_filter($ids);
  366. if (empty($ids)) {
  367. $this->error("参数错误:ids不能为空");
  368. }
  369. // 判断是否是超级管理员
  370. if (isSuperAdmin()) {
  371. $this->handleSuperAdminMode($ids);
  372. } else {
  373. $this->handleNormalUserMode($ids);
  374. }
  375. }
  376. /**
  377. * 超级管理员处理模式
  378. */
  379. protected function handleSuperAdminMode($ids)
  380. {
  381. $qcodeSmall = new QcodeSmall();
  382. $companyTables = Db::name('admin')
  383. ->field('company')
  384. ->where('company', '<>', '')
  385. ->where('kes', '<>', '')
  386. ->distinct(true)
  387. ->select();
  388. $found = false;
  389. $successIds = [];
  390. $failedIds = [];
  391. foreach ($companyTables as $row) {
  392. $cid = $row['company'];
  393. $tableName = $cid . '_qcode_small';
  394. try {
  395. // 查询该表中存在的记录
  396. $records = $qcodeSmall->name($tableName)
  397. ->whereIn('_id', $ids)
  398. ->select();
  399. if (!empty($records)) {
  400. $found = true;
  401. // 批量更新状态
  402. foreach ($records as $record) {
  403. $newStatus = ($record['status'] == 0) ? 1 : 0;
  404. $result = $qcodeSmall->name($tableName)
  405. ->where('_id', $record['_id'])
  406. ->update(['status' => $newStatus]);
  407. if ($result) {
  408. $successIds[] = $record['_id'];
  409. } else {
  410. $failedIds[] = $record['_id'];
  411. }
  412. }
  413. }
  414. } catch (\Exception $e) {
  415. continue; // 表不存在,跳过
  416. }
  417. }
  418. if ($found) {
  419. $message = "超级管理员批量切换完成";
  420. if (!empty($successIds)) {
  421. $message .= ",成功:";
  422. }
  423. if (!empty($failedIds)) {
  424. $message .= ",失败:";
  425. }
  426. $this->success($message);
  427. } else {
  428. $this->error("在所有公司中未找到这些记录:" . implode(',', $ids));
  429. }
  430. }
  431. /**
  432. * 普通用户处理模式
  433. */
  434. protected function handleNormalUserMode($ids)
  435. {
  436. $userInfo = Session::get('admin');
  437. $company_id = (int)$userInfo['company'];
  438. $qcodeSmall = new QcodeSmall();
  439. $tableName = $company_id . '_qcode_small';
  440. $successIds = [];
  441. $failedIds = [];
  442. try {
  443. // 查询存在的记录
  444. $records = $qcodeSmall->name($tableName)
  445. ->whereIn('_id', $ids)
  446. ->select();
  447. if (empty($records)) {
  448. $this->error("未找到符合条件的记录");
  449. }
  450. // 批量更新状态
  451. foreach ($records as $record) {
  452. $newStatus = ($record['status'] == 0) ? 1 : 0;
  453. $result = $qcodeSmall->name($tableName)
  454. ->where('_id', $record['_id'])
  455. ->update(['status' => $newStatus]);
  456. if ($result) {
  457. $successIds[] = $record['_id'];
  458. } else {
  459. $failedIds[] = $record['_id'];
  460. }
  461. }
  462. $message = "批量切换完成";
  463. if (!empty($successIds)) {
  464. $message .= ",成功:" ;
  465. }
  466. if (!empty($failedIds)) {
  467. $message .= ",失败:" ;
  468. }
  469. $this->success($message);
  470. } catch (\Exception $e) {
  471. $this->error("操作失败:" . $e->getMessage());
  472. }
  473. }
  474. /**
  475. * 自动打码(大件)
  476. */
  477. public function print_l()
  478. {
  479. $this->request->filter(['strip_tags', 'trim']);
  480. $req = $this->request->param();
  481. if (!$this->request->isAjax()) {
  482. $this->view->assign('ids', $req['ids']);
  483. return $this->view->fetch();
  484. }
  485. $ids = $req['ids'];
  486. $type = $req['type'];
  487. $numn = $req['numn'];
  488. $qcodeLarge = new QcodeLarge();
  489. $qcodeSmall = new QcodeSmall();
  490. $qcodeProduct = new QcodeProduct();
  491. $rows = [];
  492. // 超级管理员逻辑:遍历所有公司表查找匹配ID
  493. if (isSuperAdmin()) {
  494. $companyList = Db::name('admin')
  495. ->field('company')
  496. ->where('company', '<>', '')
  497. ->where('kes', '<>', '')
  498. ->distinct(true)
  499. ->select();
  500. foreach ($companyList as $company) {
  501. $cid = $company['company'];
  502. $tableLarge = $cid . '_qcode_large';
  503. $tableBach = $cid . '_qcode_bach';
  504. $tableSmall = $cid . '_qcode_small';
  505. // 检查是否有匹配的大件ID
  506. try {
  507. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  508. if (empty($large)) continue;
  509. $largeArr = json_decode($large, true);
  510. $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
  511. $bachArr = json_decode($bach, true);
  512. $row = $qcodeProduct->where('product_code', $bachArr['matter_no'])->find();
  513. $rowArr = json_decode($row, true);
  514. $company_name = $bachArr['supplier_name'];
  515. $product_name = $bachArr['matter_name'];
  516. $main_unit = $rowArr['main_unit'];
  517. $sec_unit = $rowArr['sec_unit'];
  518. $proportion = $rowArr['proportion'];
  519. foreach ($ids as $key => $value) {
  520. $rowData = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  521. $rowData = json_decode($rowData, true);
  522. $code = $rowData['code'];
  523. $rows[$key] = [
  524. 'company_name' => $company_name,
  525. 'product_name' => $product_name,
  526. 'sqrcd' => $qcodeSmall->name($tableSmall)->where('large_id', $value)->count(),
  527. 'main_unit' => $main_unit,
  528. 'sec_unit' => $sec_unit,
  529. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  530. 'l_flow' => ltrim(substr($code, 53, 6), '0'),
  531. 'qrcode' => $code,
  532. 'pCode' => $this->qrcode($code),
  533. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  534. ltrim(substr($code, 66, 10), '0') :
  535. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
  536. ];
  537. // 数量处理
  538. if ($rowData['l_num'] === null) {
  539. $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
  540. } elseif ($rowData['l_num'] == 0) {
  541. $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] /100 : (
  542. $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
  543. );
  544. } else {
  545. $rows[$key]['num'] = $rowData['l_num']/10000;
  546. }
  547. }
  548. // 查到就停止遍历
  549. break;
  550. } catch (\Exception $e) {
  551. continue;
  552. }
  553. }
  554. } else {
  555. // 普通用户逻辑,只能查看所属公司数据
  556. $userInfo = Session::get('admin');
  557. $company_id = (int)$userInfo['company'];
  558. $tableLarge = $company_id . '_qcode_large';
  559. $tableBach = $company_id . '_qcode_bach';
  560. $tableSmall = $company_id . '_qcode_small';
  561. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  562. $largeArr = json_decode($large, true);
  563. $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
  564. $bachArr = json_decode($bach, true);
  565. $row = $qcodeProduct->where('product_code', $bachArr['matter_no'])->find();
  566. $rowArr = json_decode($row, true);
  567. $company_name = $bachArr['supplier_name'];
  568. $product_name = $bachArr['matter_name'];
  569. $main_unit = $rowArr['main_unit'];
  570. $sec_unit = $rowArr['sec_unit'];
  571. $proportion = $rowArr['proportion'];
  572. foreach ($ids as $key => $value) {
  573. $rowData = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  574. $rowData = json_decode($rowData, true);
  575. $code = $rowData['code'];
  576. $rows[$key] = [
  577. 'company_name' => $company_name,
  578. 'product_name' => $product_name,
  579. 'sqrcd' => $qcodeSmall->name($tableSmall)->where('large_id', $value)->count(),
  580. 'main_unit' => $main_unit,
  581. 'sec_unit' => $sec_unit,
  582. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  583. 'l_flow' => ltrim(substr($code, 53, 6), '0'),
  584. 'qrcode' => $code,
  585. 'pCode' => $this->qrcode($code),
  586. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  587. ltrim(substr($code, 66, 10), '0') :
  588. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
  589. ];
  590. if ($rowData['l_num'] === null) {
  591. $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
  592. } elseif ($rowData['l_num'] == 0) {
  593. $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] /100 : (
  594. $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
  595. );
  596. } else {
  597. $rows[$key]['num'] = $rowData['l_num'];
  598. }
  599. }
  600. }
  601. // 最终返回数据
  602. $data = [
  603. 'type' => $type,
  604. 'numn' => $numn,
  605. 'rows' => $rows,
  606. 'ids' => $ids,
  607. ];
  608. $this->success('成功', '', $data);
  609. }
  610. /**
  611. * 自动打码(小件)
  612. */
  613. public function print_s()
  614. {
  615. // 过滤请求参数
  616. $this->request->filter(['strip_tags', 'trim']);
  617. $req = $this->request->param();
  618. if (!$this->request->isAjax()) {
  619. $this->view->assign('ids', $req['ids']);
  620. return $this->view->fetch();
  621. }
  622. $ids = $req['ids'];
  623. $type = $req['type'];
  624. $numn = $req['numn'];
  625. $qcodeLarge = new QcodeLarge();
  626. $qcodeSmall = new QcodeSmall();
  627. $rows = [];
  628. $key = 0;
  629. // 超级管理员逻辑
  630. if (isSuperAdmin()) {
  631. $companyList = Db::name('admin')
  632. ->field('company')
  633. ->where('company', '<>', '')
  634. ->where('kes', '<>', '')
  635. ->distinct(true)
  636. ->select();
  637. foreach ($companyList as $company) {
  638. $cid = $company['company'];
  639. $tableLarge = $cid . '_qcode_large';
  640. $tableSmall = $cid . '_qcode_small';
  641. $tableBach = $cid . '_qcode_bach';
  642. try {
  643. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  644. if (empty($large)) continue;
  645. $largeArr = json_decode($large, true);
  646. $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
  647. $bachArr = json_decode($bach, true);
  648. $company_name = $bachArr['supplier_name'];
  649. $product_name = $bachArr['matter_name'];
  650. foreach ($ids as $value) {
  651. $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  652. $large = json_decode($large, true);
  653. $arr = $qcodeSmall->name($tableSmall)->where('large_id', $value)->column('_id');
  654. foreach ($arr as $v) {
  655. $oid = is_object($v) ? $v->jsonSerialize()['$oid'] : $v;
  656. $small = $qcodeSmall->name($tableSmall)->where('_id', $oid)->find();
  657. $small = json_decode($small, true);
  658. $code = $small['code'];
  659. $rows[$key] = [
  660. 'company_name' => $company_name,
  661. 'product_name' => $product_name,
  662. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  663. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  664. 'qrcode' => $code,
  665. // 'pCode' => $this->qrcode($code), // 如需启用二维码图像,取消注释
  666. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  667. ltrim(substr($code, 66, 10), '0') :
  668. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  669. ];
  670. $key++;
  671. }
  672. }
  673. break; // 找到数据即跳出公司循环
  674. } catch (\Exception $e) {
  675. continue;
  676. }
  677. }
  678. } else {
  679. // 普通管理员逻辑
  680. $userInfo = Session::get('admin');
  681. $company_id = (int)$userInfo['company'];
  682. $tableLarge = $company_id . '_qcode_large';
  683. $tableSmall = $company_id . '_qcode_small';
  684. $tableBach = $company_id . '_qcode_bach';
  685. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  686. $bach = $this->model->name($tableBach)->where('_id', json_decode($large, true)['bach_id'])->find();
  687. $company_name = json_decode($bach, true)['supplier_name'];
  688. $product_name = json_decode($bach, true)['matter_name'];
  689. foreach ($ids as $value) {
  690. $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  691. $large = json_decode($large, true);
  692. $arr = $qcodeSmall->name($tableSmall)->where('large_id', $value)->column('_id');
  693. foreach ($arr as $v) {
  694. $oid = is_object($v) ? $v->jsonSerialize()['$oid'] : $v;
  695. $small = $qcodeSmall->name($tableSmall)->where('_id', $oid)->find();
  696. $small = json_decode($small, true);
  697. $code = $small['code'];
  698. $rows[$key] = [
  699. 'company_name' => $company_name,
  700. 'product_name' => $product_name,
  701. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  702. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  703. 'qrcode' => $code,
  704. // 'pCode' => $this->qrcode($code),
  705. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  706. ltrim(substr($code, 66, 10), '0') :
  707. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  708. ];
  709. $key++;
  710. }
  711. }
  712. }
  713. $data = [
  714. 'type' => $type,
  715. 'numn' => $numn,
  716. 'rows' => $rows,
  717. 'ids' => $ids,
  718. ];
  719. $this->success('成功', '', $data);
  720. }
  721. /**
  722. * 手动打码(小件)
  723. */
  724. public function print_ls()
  725. {
  726. // 设置过滤方法
  727. $this->request->filter(['strip_tags', 'trim']);
  728. $req = $this->request->param();
  729. if (!$this->request->isAjax()) {
  730. $this->view->assign('ids', $req['ids']);
  731. return $this->view->fetch();
  732. }
  733. $ids = $req['ids'];
  734. $type = $req['type'];
  735. $numn = $req['numn'];
  736. $qcodeLarge = new QcodeLarge();
  737. $qcodeSmall = new QcodeSmall();
  738. $rows = [];
  739. // 超级管理员逻辑
  740. if (isSuperAdmin()) {
  741. $companyList = Db::name('admin')
  742. ->field('company')
  743. ->where('company', '<>', '')
  744. ->where('kes', '<>', '')
  745. ->distinct(true)
  746. ->select();
  747. foreach ($companyList as $company) {
  748. $cid = $company['company'];
  749. $tableSmall = $cid . '_qcode_small';
  750. $tableLarge = $cid . '_qcode_large';
  751. $tableBach = $cid . '_qcode_bach';
  752. try {
  753. $small = $qcodeSmall->name($tableSmall)->where('_id', $ids)->find();
  754. if (empty($small)) continue;
  755. $small = json_decode($small, true);
  756. $large = $qcodeLarge->name($tableLarge)->where('_id', $small['large_id'])->find();
  757. $large = json_decode($large, true);
  758. $bach = $this->model->name($tableBach)->where('_id', $small['bach_id'])->find();
  759. $bach = json_decode($bach, true);
  760. $code = $small['code'];
  761. $rows[0] = [
  762. 'company_name' => $bach['supplier_name'],
  763. 'product_name' => $bach['matter_name'],
  764. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  765. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  766. 'qrcode' => $code,
  767. // 'pCode' => $this->qrcode($code),
  768. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  769. ltrim(substr($code, 66, 10), '0') :
  770. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  771. ];
  772. break; // 查到就退出循环
  773. } catch (\Exception $e) {
  774. continue;
  775. }
  776. }
  777. } else {
  778. // 普通管理员逻辑
  779. $userInfo = Session::get('admin');
  780. $company_id = (int)$userInfo['company'];
  781. $tableSmall = $company_id . '_qcode_small';
  782. $tableLarge = $company_id . '_qcode_large';
  783. $tableBach = $company_id . '_qcode_bach';
  784. $small = $qcodeSmall->name($tableSmall)->where('_id', $ids)->find();
  785. $small = json_decode($small, true);
  786. $large = $qcodeLarge->name($tableLarge)->where('_id', $small['large_id'])->find();
  787. $large = json_decode($large, true);
  788. $bach = $this->model->name($tableBach)->where('_id', $small['bach_id'])->find();
  789. $bach = json_decode($bach, true);
  790. $code = $small['code'];
  791. $rows[0] = [
  792. 'company_name' => $bach['supplier_name'],
  793. 'product_name' => $bach['matter_name'],
  794. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  795. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  796. 'qrcode' => $code,
  797. // 'pCode' => $this->qrcode($code),
  798. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  799. ltrim(substr($code, 66, 10), '0') :
  800. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  801. ];
  802. }
  803. $data = [
  804. 'type' => $type,
  805. 'numn' => $numn,
  806. 'rows' => $rows,
  807. 'ids' => $ids,
  808. ];
  809. $this->success('成功', '', $data);
  810. }
  811. /**
  812. * 打印数量设置
  813. */
  814. public function set_num()
  815. {
  816. $req = $this->request->param();
  817. //获取数据
  818. $status = $req['status'];
  819. $num = (int)$req['num'];
  820. $ids = $req['ids'];
  821. //获取company_id
  822. $userInfo = Session::get('admin');
  823. $company_id = (int)$userInfo['company'];
  824. //自动打印大件
  825. if($status==1){
  826. $qcodeLarge = new QcodeLarge();
  827. foreach ($ids as $v){
  828. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setInc('p_nums',$num);
  829. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setField('l_print','1');
  830. }
  831. }elseif($status==2){
  832. $qcodeSmall = new QcodeSmall();
  833. foreach ($ids as $v){
  834. $qcodeSmall->name($company_id.'_'."qcode_small")->where('large_id',$v)->setInc('p_nums',$num);
  835. }
  836. }elseif($status==3){
  837. $qcodeSmall = new QcodeSmall();
  838. $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id',$ids)->setInc('p_nums',$num);
  839. }
  840. }
  841. /**
  842. * 导出excel跳转
  843. */
  844. public function exp()
  845. {
  846. //获取产品配置菜单id
  847. $bool = db('auth_rule')->field('id')->where('title','发货管理')->find();
  848. if ($bool){
  849. $this->success('成功','',$bool);
  850. }else{
  851. $this->error('未获取到菜单','',$bool);
  852. }
  853. }
  854. /**
  855. * 修改大件重量数量
  856. */
  857. public function edit($ids = NULL)
  858. {
  859. $req = $this->request->param();
  860. if ($this->request->isAjax()){
  861. parse_str($req['data'],$req);
  862. if(!isset($req['row']['type']) || empty($req['row']['type'])){
  863. $this->error('请选择修改类型');
  864. }
  865. if(!isset($req['row']['numn']) || empty($req['row']['numn'])){
  866. $this->error('请填写修改数量');
  867. }
  868. $ids = explode(',',$req['row']['ids']);
  869. $data = [
  870. 'update_time'=>time(),
  871. 'sync_flag'=>0
  872. ];
  873. $userInfo = Session::get('admin');
  874. $company_id = (int)$userInfo['company'];
  875. $qcodeLarge = new QcodeLarge();
  876. if($req['row']['type']==1){//修改大件重量
  877. $data['l_weight'] = intval($req['row']['numn']*100);
  878. foreach($ids as $val){
  879. //查询code
  880. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('code')->where('_id',$val)->find();
  881. $code = json_decode($large,true)['code'];
  882. if($code){
  883. $code1 = substr($code,0,59);
  884. $code2 = substr($code,65,85);
  885. $data['code'] = $code1.str_pad(intval($req['row']['numn'])*100,6, '0', STR_PAD_LEFT).$code2;
  886. //修改code和大件重量
  887. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
  888. }
  889. }
  890. $this->success('修改成功');
  891. }
  892. if($req['row']['type']==2){//修改大件总张数
  893. $data['l_num'] = intval($req['row']['numn']);
  894. foreach($ids as $val){
  895. //查询l_num, 判断是否可以修改总张数
  896. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('l_num')->where('_id',$val)->find();
  897. $l_num = json_decode($large,true)['l_num'];
  898. if($l_num > 0 || $l_num==null){
  899. //可以修改
  900. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
  901. }else{
  902. $this->error('该产品不能修改大件数量');
  903. }
  904. }
  905. $this->success('修改成功');
  906. }
  907. }
  908. $this->view->assign('ids',$req['ids']);
  909. return $this->view->fetch();
  910. }
  911. /**
  912. * 删除批次
  913. */
  914. public function del($ids = NULL)
  915. {
  916. $userInfo = Session::get('admin');
  917. $company_id = (int)$userInfo['company'];
  918. $qcodeLarge = new QcodeLarge();
  919. $qcodeSmall = new QcodeSmall();
  920. $data = [
  921. 'delete_time'=>time(),
  922. ];
  923. $bool1 = $this->model->name($company_id.'_'."qcode_bach")->where('_id',$ids)->update($data);
  924. $bool2 = $qcodeLarge->name($company_id.'_'."qcode_large")->where('bach_id',$ids)->update($data);
  925. $bool3 = $qcodeSmall->name($company_id.'_'."qcode_small")->where('bach_id',$ids)->update($data);
  926. if($bool1!==false && $bool2!==false && $bool3!==false){
  927. $this->success('删除成功');
  928. }else{
  929. $this->error('删除失败');
  930. }
  931. }
  932. /**
  933. * 补打标签
  934. */
  935. public function reprint()
  936. {
  937. if ($this->request->isAjax()) {
  938. $data = $this->request->param();
  939. parse_str($data['data'],$req);
  940. if(!isset($req['row']['nickname']) || empty($req['row']['nickname'])){
  941. $this->error('请填写公司名称');
  942. }
  943. if((!isset($req['row']['product_name']) || empty($req['row']['product_name'])) && (!isset($req['row']['product_code']) || empty($req['row']['product_code']))){
  944. $this->error('请填写辅料名称或辅料编号');
  945. }
  946. if(!isset($req['row']['sqrcd']) || empty($req['row']['sqrcd'])){
  947. $this->error('请填写配盘数');
  948. }
  949. if(!isset($req['row']['num']) || empty($req['row']['num'])){
  950. $this->error('请填写数量');
  951. }
  952. //查询公司信息
  953. $qcodeGsmc = new QcodeGsmc();
  954. $print_code = $qcodeGsmc
  955. ->field('id, nickname, print_code')
  956. ->where('nickname',$req['row']['nickname'])
  957. ->find();
  958. if (!$print_code) $this->error('未查到公司信息');
  959. $print_code = json_decode($print_code,true);
  960. //查询产品信息
  961. $qcodeProduct = new QcodeProduct();
  962. if(!empty($req['row']['product_code'])){
  963. $sel = $qcodeProduct
  964. ->field('id, product_name, product_code, temple')
  965. ->where('product_code',$req['row']['product_code'])
  966. ->find();
  967. }else{
  968. $sel = $qcodeProduct
  969. ->field('id, product_name, product_code, temple')
  970. ->where('product_name',$req['row']['product_name'])
  971. ->find();
  972. }
  973. if (!$sel) $this->error('未查到辅料信息');
  974. $sel = json_decode($sel,true);
  975. $data = [
  976. 'company_name'=>$req['row']['nickname'],
  977. 'product_name'=>$sel['product_name'],
  978. 'sqrcd'=>$req['row']['sqrcd'],
  979. 'num'=>$req['row']['num'],
  980. 'main_unit'=>'盘',
  981. 'sec_unit'=>'万米',
  982. 'date'=>date('Y-m-d'),
  983. 'batch'=>substr(date('Ymd'),2,6),
  984. 'l_flow'=>1,
  985. ];
  986. $num = str_pad($req['row']['num'], 3, '0', STR_PAD_LEFT);
  987. $qrcode = 'AB'.'92'.$sel['temple'].'0'.$print_code['print_code'].('000'.$sel['product_code']).$data['batch'].$num.$data['batch'].'000001'.'000000'.'2'.'0000'.$data['batch'].'0000000000';
  988. $data['pCode'] = $this->qrcode($qrcode);
  989. $data['qrcode'] = $qrcode;
  990. $this->success('成功','',$data);
  991. }
  992. return $this->view->fetch();
  993. }
  994. /**
  995. * 公司名称搜索
  996. */
  997. public function sel_n()
  998. {
  999. $name = $this->request->param()['q_word'][0];
  1000. $where = [
  1001. 'nickname'=>new \MongoDB\BSON\Regex($name),
  1002. 'delete_time'=>'',
  1003. ];
  1004. $qcodeGsmc = new QcodeGsmc();
  1005. $sel = $qcodeGsmc
  1006. ->field('nickname')
  1007. ->where($where)
  1008. ->limit(0,10)
  1009. ->select();
  1010. return json(['list' => $sel, 'total' => count($sel)]);
  1011. }
  1012. /**
  1013. * 辅料名称搜索
  1014. */
  1015. public function sel_p()
  1016. {
  1017. $name = $this->request->param()['q_word'][0];
  1018. $where = [
  1019. 'product_name'=>new \MongoDB\BSON\Regex($name),
  1020. 'delete_time'=>'',
  1021. ];
  1022. $qcodeProduct = new QcodeProduct();
  1023. $sel = $qcodeProduct
  1024. ->field('product_name, product_code')
  1025. ->where($where)
  1026. ->limit(0,10)
  1027. ->select();
  1028. foreach ($sel as $k=>$v) {
  1029. $oid = $v['_id']->jsonSerialize();
  1030. $sel[$k]['id'] = $oid['$oid'];
  1031. }
  1032. return json(['list' => $sel, 'total' => count($sel)]);
  1033. }
  1034. /**
  1035. * 辅料编码搜索
  1036. */
  1037. public function sel_c()
  1038. {
  1039. $name = $this->request->param()['q_word'][0];
  1040. $where = [
  1041. 'product_code'=>new \MongoDB\BSON\Regex($name),
  1042. 'delete_time'=>'',
  1043. ];
  1044. $qcodeProduct = new QcodeProduct();
  1045. $sel = $qcodeProduct
  1046. ->field('id, product_name, product_code')
  1047. ->where($where)
  1048. ->order('id','desc')
  1049. ->limit(0,10)
  1050. ->select();
  1051. foreach ($sel as $k=>$v) {
  1052. $oid = $v['_id']->jsonSerialize();
  1053. $sel[$k]['id'] = $oid['$oid'];
  1054. }
  1055. return json(['list' => $sel, 'total' => count($sel)]);
  1056. }
  1057. /**
  1058. * 二维码生成类
  1059. */
  1060. public function qrcode($url)//二维码生成类
  1061. {
  1062. $url=$url;
  1063. $level=3;
  1064. $size=6;
  1065. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  1066. $errorCorrectionLevel =intval($level) ;//容错级别
  1067. $matrixPointSize = intval($size);//生成图片大小
  1068. //生成二维码图片
  1069. $object = new \QRcode();
  1070. //打开缓冲区
  1071. ob_start();
  1072. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  1073. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  1074. $imageString = base64_encode(ob_get_contents());
  1075. //关闭缓冲区
  1076. ob_end_clean();
  1077. //把生成的base64字符串返回给前端
  1078. // $data = array(
  1079. // 'labelcode'=>$url,
  1080. // 'code'=>200,
  1081. // 'data'=>$imageString,
  1082. // 'product_code'=>$url
  1083. // );
  1084. return 'data:image/png;base64,'.$imageString;
  1085. }
  1086. // public function bacch(){
  1087. // $mongo = \think\Db::connect('mongodb');
  1088. //
  1089. // // 表1数据
  1090. // $qcode_product = $mongo->name('qcode_product')->select();
  1091. // // 表2数据
  1092. // $qr_qcode_product_copy = $mongo->name('qr_qcode_product_copy')->select();
  1093. //
  1094. // // 把表2按 product_name 索引
  1095. // $map2 = [];
  1096. // foreach ($qr_qcode_product_copy as $row) {
  1097. // $map2[$row['product_name']] = $row['product_code'];
  1098. // }
  1099. //
  1100. // $same = []; // 相同 product_name 的记录
  1101. // $diff = []; // 不相同的记录
  1102. //
  1103. // // 先处理表1,找出匹配和不匹配
  1104. // foreach ($qcode_product as $row1) {
  1105. // if (isset($map2[$row1['product_name']])) {
  1106. // $same[] = [
  1107. // 'product_code_1' => $row1['product_code'],
  1108. // 'product_code_2' => $map2[$row1['product_name']],
  1109. // 'product_name' => $row1['product_name']
  1110. // ];
  1111. // unset($map2[$row1['product_name']]); // 已匹配,避免后面重复
  1112. // } else {
  1113. // $diff[] = [
  1114. // 'product_code' => $row1['product_code'],
  1115. // 'product_name' => $row1['product_name']
  1116. // ];
  1117. // }
  1118. // }
  1119. //
  1120. // // 剩下 map2 中的就是表2中独有的
  1121. // foreach ($map2 as $name => $code) {
  1122. // $diff[] = [
  1123. // 'product_code' => $code,
  1124. // 'product_name' => $name
  1125. // ];
  1126. // }
  1127. //
  1128. //// // 存到新表
  1129. // $mongo->name('same_products')->insertAll($same);
  1130. // $mongo->name('diff_products')->insertAll($diff);
  1131. // }
  1132. // public function bacch() {
  1133. // $mongo = \think\Db::connect('mongodb');
  1134. //
  1135. // // 获取三个表的数据
  1136. // $table1 = $mongo->name('qcode_product')->select();
  1137. // $table2 = $mongo->name('qr_qcode_product_copy')->select();
  1138. //
  1139. // // 预加载qr_qcode_unit_copy表数据并建立索引
  1140. // $unitData = $mongo->name('qr_qcode_unit_copy')->select();
  1141. // $unitIndex = [];
  1142. // foreach ($unitData as $unit) {
  1143. // $unitIndex[$unit['code']] = [
  1144. // 'main_unit' => $unit['main_unit'] ?? null,
  1145. // 'sec_unit' => $unit['sec_unit'] ?? null,
  1146. // 'proportion' => $unit['proportion'] ?? null
  1147. // ];
  1148. // }
  1149. //
  1150. // // 索引表2数据,包含product_name和temple
  1151. // $table2Index = [];
  1152. // foreach ($table2 as $row) {
  1153. // $table2Index[$row['product_name']] = [
  1154. // 'product_code' => $row['product_code'],
  1155. // 'temple' => $row['temple'] ?? null
  1156. // ];
  1157. // }
  1158. //
  1159. // $differences = [];
  1160. //
  1161. // // 处理表1中的记录
  1162. // foreach ($table1 as $row) {
  1163. // if (!isset($table2Index[$row['product_name']]) &&
  1164. // strpos($row['product_code'], 'AB') !== 0) {
  1165. //
  1166. // // 获取单位信息
  1167. // $unitInfo = $unitIndex[$row['product_code']] ?? [
  1168. // 'main_unit' => null,
  1169. // 'sec_unit' => null,
  1170. // 'proportion' => null
  1171. // ];
  1172. //
  1173. // $differences[] = [
  1174. // 'product_code' => $row['product_code'],
  1175. // 'product_name' => $row['product_name'],
  1176. // 'temple' => null,
  1177. // 'main_unit' => $unitInfo['main_unit'],
  1178. // 'sec_unit' => $unitInfo['sec_unit'],
  1179. // 'proportion' => $unitInfo['proportion']
  1180. // ];
  1181. // }
  1182. // }
  1183. //
  1184. // // 处理表2中的记录
  1185. // foreach ($table2Index as $name => $data) {
  1186. // $existsInTable1 = false;
  1187. // foreach ($table1 as $row) {
  1188. // if ($row['product_name'] === $name) {
  1189. // $existsInTable1 = true;
  1190. // break;
  1191. // }
  1192. // }
  1193. //
  1194. // if (!$existsInTable1 && strpos($data['product_code'], 'AB') !== 0) {
  1195. // // 获取单位信息
  1196. // $unitInfo = $unitIndex[$data['product_code']] ?? [
  1197. // 'main_unit' => null,
  1198. // 'sec_unit' => null,
  1199. // 'proportion' => null
  1200. // ];
  1201. //
  1202. // $differences[] = [
  1203. // 'product_name' => $name,
  1204. // 'product_code' => $data['product_code'],
  1205. // 'temple' => $data['temple'],
  1206. // 'main_unit' => $unitInfo['main_unit'],
  1207. // 'sec_unit' => $unitInfo['sec_unit'],
  1208. // 'proportion' => $unitInfo['proportion'],
  1209. // 'delete_time' => '',
  1210. // 'sync_flag' => 0,
  1211. // 'code' => substr($data['product_code'], 0, 4)
  1212. // ];
  1213. // }
  1214. // }
  1215. //
  1216. // // 获取当前最大的oid_id值
  1217. // $maxOid = $mongo->name('qcode_product')->order('oid_id','desc')->find();
  1218. //
  1219. // $nextOid = $maxOid['oid_id'] ? $maxOid['oid_id'] + 1 : 1; // 如果没有记录则从1开始
  1220. // // 插入数据到qcode_product表
  1221. // foreach ($differences as $diff) {
  1222. // $insertData = [
  1223. // 'oid_id' => $nextOid++,
  1224. // 'product_code' => $diff['product_code'],
  1225. // 'product_name' => $diff['product_name'],
  1226. // 'temple' => $diff['temple'],
  1227. // 'main_unit' => $diff['main_unit'],
  1228. // 'sec_unit' => $diff['sec_unit'],
  1229. // 'proportion' => $diff['proportion'],
  1230. // 'delete_time' => $diff['delete_time'] ?? '',
  1231. // 'sync_flag' => $diff['sync_flag'] ?? 0,
  1232. // 'code' => $diff['code'] ?? substr($diff['product_code'], 0, 4)
  1233. // ];
  1234. // $mongo->name('qcode_product')->insert($insertData);
  1235. // }
  1236. //
  1237. // echo "成功插入 " . count($differences) . " 条记录";
  1238. // }
  1239. }