QcodeBach.php 57 KB

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