QcodeBach.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  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' => (substr($code, 76, 10) == '0000000000') ?
  673. ltrim(substr($code, 66, 10), '0') :
  674. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
  675. ];
  676. // 数量处理
  677. if ($rowData['l_num'] === null) {
  678. $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
  679. } elseif ($rowData['l_num'] == 0) {
  680. $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] /100 : (
  681. $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
  682. );
  683. } else if($main_unit == '万支') {
  684. // if($main_unit == '万支'){
  685. $rows[$key]['num'] = $rowData['l_num']/10000;
  686. // }else{
  687. // $rows[$key]['num'] = $rowData['l_num'];
  688. // }
  689. // if ($main_unit == '万张') {
  690. // $rows[$key]['num'] = $rowData['l_num']/10000;
  691. // }else{
  692. // $rows[$key]['num'] = $rowData['l_num'];
  693. // }
  694. }else if($main_unit == '万张'){
  695. $rows[$key]['num'] = $rowData['l_num']/10000;
  696. }else{
  697. $rows[$key]['num'] = $rowData['l_num'];
  698. }
  699. }
  700. // 查到就停止遍历
  701. break;
  702. } catch (\Exception $e) {
  703. continue;
  704. }
  705. }
  706. } else {
  707. // 普通用户逻辑,只能查看所属公司数据
  708. $userInfo = Session::get('admin');
  709. $company_id = (int)$userInfo['company'];
  710. $tableLarge = $company_id . '_qcode_large';
  711. $tableBach = $company_id . '_qcode_bach';
  712. $tableSmall = $company_id . '_qcode_small';
  713. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  714. $largeArr = json_decode($large, true);
  715. $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
  716. $bachArr = json_decode($bach, true);
  717. $row = $qcodeProduct->where('product_code', $bachArr['matter_no'])->find();
  718. $rowArr = json_decode($row, true);
  719. $company_name = $bachArr['supplier_name'];
  720. $product_name = $bachArr['matter_name'];
  721. $main_unit = $rowArr['main_unit'];
  722. $sec_unit = $rowArr['sec_unit'];
  723. $proportion = $rowArr['proportion'];
  724. foreach ($ids as $key => $value) {
  725. $rowData = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  726. $rowData = json_decode($rowData, true);
  727. $code = $rowData['code'];
  728. $rows[$key] = [
  729. 'company_name' => $company_name,
  730. 'product_name' => $product_name,
  731. 'sqrcd' => $qcodeSmall->name($tableSmall)->where('large_id', $value)->count(),
  732. 'main_unit' => $main_unit,
  733. 'sec_unit' => $sec_unit,
  734. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  735. 'l_flow' => ltrim(substr($code, 53, 6), '0'),
  736. 'qrcode' => $code,
  737. 'pCode' => $this->qrcode($code),
  738. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  739. ltrim(substr($code, 66, 10), '0') :
  740. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
  741. ];
  742. if ($rowData['l_num'] === null) {
  743. $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
  744. } elseif ($rowData['l_num'] == 0) {
  745. $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] /100 : (
  746. $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
  747. );
  748. } else if($main_unit == '万支') {
  749. // if($main_unit == '万支'){
  750. $rows[$key]['num'] = $rowData['l_num']/10000;
  751. // }else{
  752. // $rows[$key]['num'] = $rowData['l_num'];
  753. // }
  754. // if ($main_unit == '万张') {
  755. // $rows[$key]['num'] = $rowData['l_num']/10000;
  756. // }else{
  757. // $rows[$key]['num'] = $rowData['l_num'];
  758. // }
  759. }else if($main_unit == '万张'){
  760. $rows[$key]['num'] = $rowData['l_num']/10000;
  761. }else{
  762. $rows[$key]['num'] = $rowData['l_num'];
  763. }
  764. }
  765. }
  766. // 最终返回数据
  767. $data = [
  768. 'type' => $type,
  769. 'numn' => $numn,
  770. 'rows' => $rows,
  771. 'ids' => $ids,
  772. ];
  773. $this->success('成功', '', $data);
  774. }
  775. /**
  776. * 自动打码(小件)
  777. */
  778. public function print_s()
  779. {
  780. // 过滤请求参数
  781. $this->request->filter(['strip_tags', 'trim']);
  782. $req = $this->request->param();
  783. if (!$this->request->isAjax()) {
  784. $this->view->assign('ids', $req['ids']);
  785. return $this->view->fetch();
  786. }
  787. $ids = $req['ids'];
  788. $type = $req['type'];
  789. $numn = $req['numn'];
  790. $qcodeLarge = new QcodeLarge();
  791. $qcodeSmall = new QcodeSmall();
  792. $rows = [];
  793. $key = 0;
  794. // 超级管理员逻辑
  795. if (isSuperAdmin()) {
  796. $companyList = Db::name('admin')
  797. ->field('company')
  798. ->where('company', '<>', '')
  799. ->where('kes', '<>', '')
  800. ->distinct(true)
  801. ->select();
  802. foreach ($companyList as $company) {
  803. $cid = $company['company'];
  804. $tableLarge = $cid . '_qcode_large';
  805. $tableSmall = $cid . '_qcode_small';
  806. $tableBach = $cid . '_qcode_bach';
  807. try {
  808. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  809. if (empty($large)) continue;
  810. $largeArr = json_decode($large, true);
  811. $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
  812. $bachArr = json_decode($bach, true);
  813. $company_name = $bachArr['supplier_name'];
  814. $product_name = $bachArr['matter_name'];
  815. foreach ($ids as $value) {
  816. $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  817. $large = json_decode($large, true);
  818. $arr = $qcodeSmall->name($tableSmall)->where('large_id', $value)->column('_id');
  819. foreach ($arr as $v) {
  820. $oid = is_object($v) ? $v->jsonSerialize()['$oid'] : $v;
  821. $small = $qcodeSmall->name($tableSmall)->where('_id', $oid)->find();
  822. $small = json_decode($small, true);
  823. $code = $small['code'];
  824. $rows[$key] = [
  825. 'company_name' => $company_name,
  826. 'product_name' => $product_name,
  827. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  828. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  829. 'qrcode' => $code,
  830. // 'pCode' => $this->qrcode($code), // 如需启用二维码图像,取消注释
  831. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  832. ltrim(substr($code, 66, 10), '0') :
  833. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  834. ];
  835. $key++;
  836. }
  837. }
  838. break; // 找到数据即跳出公司循环
  839. } catch (\Exception $e) {
  840. continue;
  841. }
  842. }
  843. } else {
  844. // 普通管理员逻辑
  845. $userInfo = Session::get('admin');
  846. $company_id = (int)$userInfo['company'];
  847. $tableLarge = $company_id . '_qcode_large';
  848. $tableSmall = $company_id . '_qcode_small';
  849. $tableBach = $company_id . '_qcode_bach';
  850. $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
  851. $bach = $this->model->name($tableBach)->where('_id', json_decode($large, true)['bach_id'])->find();
  852. $company_name = json_decode($bach, true)['supplier_name'];
  853. $product_name = json_decode($bach, true)['matter_name'];
  854. foreach ($ids as $value) {
  855. $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
  856. $large = json_decode($large, true);
  857. $arr = $qcodeSmall->name($tableSmall)->where('large_id', $value)->column('_id');
  858. foreach ($arr as $v) {
  859. $oid = is_object($v) ? $v->jsonSerialize()['$oid'] : $v;
  860. $small = $qcodeSmall->name($tableSmall)->where('_id', $oid)->find();
  861. $small = json_decode($small, true);
  862. $code = $small['code'];
  863. $rows[$key] = [
  864. 'company_name' => $company_name,
  865. 'product_name' => $product_name,
  866. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  867. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  868. 'qrcode' => $code,
  869. // 'pCode' => $this->qrcode($code),
  870. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  871. ltrim(substr($code, 66, 10), '0') :
  872. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  873. ];
  874. $key++;
  875. }
  876. }
  877. }
  878. $data = [
  879. 'type' => $type,
  880. 'numn' => $numn,
  881. 'rows' => $rows,
  882. 'ids' => $ids,
  883. ];
  884. $this->success('成功', '', $data);
  885. }
  886. /**
  887. * 手动打码(小件)
  888. */
  889. public function print_ls()
  890. {
  891. // 设置过滤方法
  892. $this->request->filter(['strip_tags', 'trim']);
  893. $req = $this->request->param();
  894. if (!$this->request->isAjax()) {
  895. $this->view->assign('ids', $req['ids']);
  896. return $this->view->fetch();
  897. }
  898. $ids = $req['ids'];
  899. $type = $req['type'];
  900. $numn = $req['numn'];
  901. $qcodeLarge = new QcodeLarge();
  902. $qcodeSmall = new QcodeSmall();
  903. $rows = [];
  904. // 超级管理员逻辑
  905. if (isSuperAdmin()) {
  906. $companyList = Db::name('admin')
  907. ->field('company')
  908. ->where('company', '<>', '')
  909. ->where('kes', '<>', '')
  910. ->distinct(true)
  911. ->select();
  912. foreach ($companyList as $company) {
  913. $cid = $company['company'];
  914. $tableSmall = $cid . '_qcode_small';
  915. $tableLarge = $cid . '_qcode_large';
  916. $tableBach = $cid . '_qcode_bach';
  917. try {
  918. $small = $qcodeSmall->name($tableSmall)->where('_id', $ids)->find();
  919. if (empty($small)) continue;
  920. $small = json_decode($small, true);
  921. $large = $qcodeLarge->name($tableLarge)->where('_id', $small['large_id'])->find();
  922. $large = json_decode($large, true);
  923. $bach = $this->model->name($tableBach)->where('_id', $small['bach_id'])->find();
  924. $bach = json_decode($bach, true);
  925. $code = $small['code'];
  926. $rows[0] = [
  927. 'company_name' => $bach['supplier_name'],
  928. 'product_name' => $bach['matter_name'],
  929. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  930. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  931. 'qrcode' => $code,
  932. // 'pCode' => $this->qrcode($code),
  933. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  934. ltrim(substr($code, 66, 10), '0') :
  935. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  936. ];
  937. break; // 查到就退出循环
  938. } catch (\Exception $e) {
  939. continue;
  940. }
  941. }
  942. } else {
  943. // 普通管理员逻辑
  944. $userInfo = Session::get('admin');
  945. $company_id = (int)$userInfo['company'];
  946. $tableSmall = $company_id . '_qcode_small';
  947. $tableLarge = $company_id . '_qcode_large';
  948. $tableBach = $company_id . '_qcode_bach';
  949. $small = $qcodeSmall->name($tableSmall)->where('_id', $ids)->find();
  950. $small = json_decode($small, true);
  951. $large = $qcodeLarge->name($tableLarge)->where('_id', $small['large_id'])->find();
  952. $large = json_decode($large, true);
  953. $bach = $this->model->name($tableBach)->where('_id', $small['bach_id'])->find();
  954. $bach = json_decode($bach, true);
  955. $code = $small['code'];
  956. $rows[0] = [
  957. 'company_name' => $bach['supplier_name'],
  958. 'product_name' => $bach['matter_name'],
  959. 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
  960. 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
  961. 'qrcode' => $code,
  962. // 'pCode' => $this->qrcode($code),
  963. 'batch' => (substr($code, 76, 10) == '0000000000') ?
  964. ltrim(substr($code, 66, 10), '0') :
  965. ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
  966. ];
  967. }
  968. $data = [
  969. 'type' => $type,
  970. 'numn' => $numn,
  971. 'rows' => $rows,
  972. 'ids' => $ids,
  973. ];
  974. $this->success('成功', '', $data);
  975. }
  976. /**
  977. * 打印数量设置
  978. */
  979. public function set_num()
  980. {
  981. $req = $this->request->param();
  982. //获取数据
  983. $status = $req['status'];
  984. $num = (int)$req['num'];
  985. $ids = $req['ids'];
  986. //获取company_id
  987. $userInfo = Session::get('admin');
  988. $company_id = (int)$userInfo['company'];
  989. //自动打印大件
  990. if($status==1){
  991. $qcodeLarge = new QcodeLarge();
  992. foreach ($ids as $v){
  993. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setInc('p_nums',$num);
  994. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setField('l_print','1');
  995. }
  996. }elseif($status==2){
  997. $qcodeSmall = new QcodeSmall();
  998. foreach ($ids as $v){
  999. $qcodeSmall->name($company_id.'_'."qcode_small")->where('large_id',$v)->setInc('p_nums',$num);
  1000. }
  1001. }elseif($status==3){
  1002. $qcodeSmall = new QcodeSmall();
  1003. $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id',$ids)->setInc('p_nums',$num);
  1004. }
  1005. }
  1006. /**
  1007. * 导出excel跳转
  1008. */
  1009. public function exp()
  1010. {
  1011. //获取产品配置菜单id
  1012. $bool = db('auth_rule')->field('id')->where('title','发货管理')->find();
  1013. if ($bool){
  1014. $this->success('成功','',$bool);
  1015. }else{
  1016. $this->error('未获取到菜单','',$bool);
  1017. }
  1018. }
  1019. /**
  1020. * 修改大件重量数量
  1021. */
  1022. public function edit($ids = NULL)
  1023. {
  1024. $req = $this->request->param();
  1025. if ($this->request->isAjax()){
  1026. parse_str($req['data'],$req);
  1027. if(!isset($req['row']['type']) || empty($req['row']['type'])){
  1028. $this->error('请选择修改类型');
  1029. }
  1030. if(!isset($req['row']['numn']) || empty($req['row']['numn'])){
  1031. $this->error('请填写修改数量');
  1032. }
  1033. $ids = explode(',',$req['row']['ids']);
  1034. $data = [
  1035. 'update_time'=>time(),
  1036. 'sync_flag'=>0
  1037. ];
  1038. $userInfo = Session::get('admin');
  1039. $company_id = (int)$userInfo['company'];
  1040. $qcodeLarge = new QcodeLarge();
  1041. if($req['row']['type']==1){//修改大件重量
  1042. $data['l_weight'] = intval(round($req['row']['numn']*100));
  1043. // $data['l_weight'] = intval($req['row']['numn']*100);
  1044. foreach($ids as $val){
  1045. //查询code
  1046. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('code')->where('_id',$val)->find();
  1047. $code = json_decode($large,true)['code'];
  1048. if($code){
  1049. $code1 = substr($code,0,59);
  1050. $code2 = substr($code,65,85);
  1051. $data['code'] = $code1.str_pad(intval($req['row']['numn'])*100,6, '0', STR_PAD_LEFT).$code2;
  1052. //修改code和大件重量
  1053. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
  1054. }
  1055. }
  1056. $this->success('修改成功');
  1057. }
  1058. if($req['row']['type']==2){//修改大件总张数
  1059. $data['l_num'] = intval(round($req['row']['numn']));
  1060. // $data['l_num'] = intval($req['row']['numn']);
  1061. foreach($ids as $val){
  1062. //查询l_num, 判断是否可以修改总张数
  1063. $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('l_num')->where('_id',$val)->find();
  1064. $l_num = json_decode($large,true)['l_num'];
  1065. if($l_num > 0 || $l_num==null){
  1066. //可以修改
  1067. $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
  1068. }else{
  1069. $this->error('该产品不能修改大件数量');
  1070. }
  1071. }
  1072. $this->success('修改成功');
  1073. }
  1074. }
  1075. $this->view->assign('ids',$req['ids']);
  1076. return $this->view->fetch();
  1077. }
  1078. /**
  1079. * 删除批次
  1080. */
  1081. public function del($ids = NULL)
  1082. {
  1083. $userInfo = Session::get('admin');
  1084. $company_id = (int)$userInfo['company'];
  1085. $qcodeLarge = new QcodeLarge();
  1086. $qcodeSmall = new QcodeSmall();
  1087. $data = [
  1088. 'delete_time'=>time(),
  1089. ];
  1090. $bool1 = $this->model->name($company_id.'_'."qcode_bach")->where('_id',$ids)->update($data);
  1091. $bool2 = $qcodeLarge->name($company_id.'_'."qcode_large")->where('bach_id',$ids)->update($data);
  1092. $bool3 = $qcodeSmall->name($company_id.'_'."qcode_small")->where('bach_id',$ids)->update($data);
  1093. if($bool1!==false && $bool2!==false && $bool3!==false){
  1094. $this->success('删除成功');
  1095. }else{
  1096. $this->error('删除失败');
  1097. }
  1098. }
  1099. /**
  1100. * 补打标签
  1101. */
  1102. public function reprint()
  1103. {
  1104. if ($this->request->isAjax()) {
  1105. $data = $this->request->param();
  1106. parse_str($data['data'],$req);
  1107. if(!isset($req['row']['nickname']) || empty($req['row']['nickname'])){
  1108. $this->error('请填写公司名称');
  1109. }
  1110. if((!isset($req['row']['product_name']) || empty($req['row']['product_name'])) && (!isset($req['row']['product_code']) || empty($req['row']['product_code']))){
  1111. $this->error('请填写辅料名称或辅料编号');
  1112. }
  1113. if(!isset($req['row']['sqrcd']) || empty($req['row']['sqrcd'])){
  1114. $this->error('请填写配盘数');
  1115. }
  1116. if(!isset($req['row']['num']) || empty($req['row']['num'])){
  1117. $this->error('请填写数量');
  1118. }
  1119. //查询公司信息
  1120. $qcodeGsmc = new QcodeGsmc();
  1121. $print_code = $qcodeGsmc
  1122. ->field('id, nickname, print_code')
  1123. ->where('nickname',$req['row']['nickname'])
  1124. ->find();
  1125. if (!$print_code) $this->error('未查到公司信息');
  1126. $print_code = json_decode($print_code,true);
  1127. //查询产品信息
  1128. $qcodeProduct = new QcodeProduct();
  1129. if(!empty($req['row']['product_code'])){
  1130. $sel = $qcodeProduct
  1131. ->field('id, product_name, product_code, temple')
  1132. ->where('product_code',$req['row']['product_code'])
  1133. ->find();
  1134. }else{
  1135. $sel = $qcodeProduct
  1136. ->field('id, product_name, product_code, temple')
  1137. ->where('product_name',$req['row']['product_name'])
  1138. ->find();
  1139. }
  1140. if (!$sel) $this->error('未查到辅料信息');
  1141. $sel = json_decode($sel,true);
  1142. $data = [
  1143. 'company_name'=>$req['row']['nickname'],
  1144. 'product_name'=>$sel['product_name'],
  1145. 'sqrcd'=>$req['row']['sqrcd'],
  1146. 'num'=>$req['row']['num'],
  1147. 'main_unit'=>'盘',
  1148. 'sec_unit'=>'万米',
  1149. 'date'=>date('Y-m-d'),
  1150. 'batch'=>substr(date('Ymd'),2,6),
  1151. 'l_flow'=>1,
  1152. ];
  1153. $num = str_pad($req['row']['num'], 3, '0', STR_PAD_LEFT);
  1154. $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';
  1155. $data['pCode'] = $this->qrcode($qrcode);
  1156. $data['qrcode'] = $qrcode;
  1157. $this->success('成功','',$data);
  1158. }
  1159. return $this->view->fetch();
  1160. }
  1161. /**
  1162. * 公司名称搜索
  1163. */
  1164. public function sel_n()
  1165. {
  1166. $name = $this->request->param()['q_word'][0];
  1167. $where = [
  1168. 'nickname'=>new \MongoDB\BSON\Regex($name),
  1169. 'delete_time'=>'',
  1170. ];
  1171. $qcodeGsmc = new QcodeGsmc();
  1172. $sel = $qcodeGsmc
  1173. ->field('nickname')
  1174. ->where($where)
  1175. ->limit(0,10)
  1176. ->select();
  1177. return json(['list' => $sel, 'total' => count($sel)]);
  1178. }
  1179. /**
  1180. * 辅料名称搜索
  1181. */
  1182. public function sel_p()
  1183. {
  1184. $name = $this->request->param()['q_word'][0];
  1185. $where = [
  1186. 'product_name'=>new \MongoDB\BSON\Regex($name),
  1187. 'delete_time'=>'',
  1188. ];
  1189. $qcodeProduct = new QcodeProduct();
  1190. $sel = $qcodeProduct
  1191. ->field('product_name, product_code')
  1192. ->where($where)
  1193. ->limit(0,10)
  1194. ->select();
  1195. foreach ($sel as $k=>$v) {
  1196. $oid = $v['_id']->jsonSerialize();
  1197. $sel[$k]['id'] = $oid['$oid'];
  1198. }
  1199. return json(['list' => $sel, 'total' => count($sel)]);
  1200. }
  1201. /**
  1202. * 辅料编码搜索
  1203. */
  1204. public function sel_c()
  1205. {
  1206. $name = $this->request->param()['q_word'][0];
  1207. $where = [
  1208. 'product_code'=>new \MongoDB\BSON\Regex($name),
  1209. 'delete_time'=>'',
  1210. ];
  1211. $qcodeProduct = new QcodeProduct();
  1212. $sel = $qcodeProduct
  1213. ->field('id, product_name, product_code')
  1214. ->where($where)
  1215. ->order('id','desc')
  1216. ->limit(0,10)
  1217. ->select();
  1218. foreach ($sel as $k=>$v) {
  1219. $oid = $v['_id']->jsonSerialize();
  1220. $sel[$k]['id'] = $oid['$oid'];
  1221. }
  1222. return json(['list' => $sel, 'total' => count($sel)]);
  1223. }
  1224. /**
  1225. * 二维码生成类
  1226. */
  1227. public function qrcode($url)//二维码生成类
  1228. {
  1229. $url=$url;
  1230. $level=3;
  1231. $size=6;
  1232. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  1233. $errorCorrectionLevel =intval($level) ;//容错级别
  1234. $matrixPointSize = intval($size);//生成图片大小
  1235. //生成二维码图片
  1236. $object = new \QRcode();
  1237. //打开缓冲区
  1238. ob_start();
  1239. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  1240. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  1241. $imageString = base64_encode(ob_get_contents());
  1242. //关闭缓冲区
  1243. ob_end_clean();
  1244. //把生成的base64字符串返回给前端
  1245. // $data = array(
  1246. // 'labelcode'=>$url,
  1247. // 'code'=>200,
  1248. // 'data'=>$imageString,
  1249. // 'product_code'=>$url
  1250. // );
  1251. return 'data:image/png;base64,'.$imageString;
  1252. }
  1253. // public function bacch(){
  1254. // $mongo = \think\Db::connect('mongodb');
  1255. //
  1256. // // 表1数据
  1257. // $qcode_product = $mongo->name('qcode_product')->select();
  1258. // // 表2数据
  1259. // $qr_qcode_product_copy = $mongo->name('qr_qcode_product_copy')->select();
  1260. //
  1261. // // 把表2按 product_name 索引
  1262. // $map2 = [];
  1263. // foreach ($qr_qcode_product_copy as $row) {
  1264. // $map2[$row['product_name']] = $row['product_code'];
  1265. // }
  1266. //
  1267. // $same = []; // 相同 product_name 的记录
  1268. // $diff = []; // 不相同的记录
  1269. //
  1270. // // 先处理表1,找出匹配和不匹配
  1271. // foreach ($qcode_product as $row1) {
  1272. // if (isset($map2[$row1['product_name']])) {
  1273. // $same[] = [
  1274. // 'product_code_1' => $row1['product_code'],
  1275. // 'product_code_2' => $map2[$row1['product_name']],
  1276. // 'product_name' => $row1['product_name']
  1277. // ];
  1278. // unset($map2[$row1['product_name']]); // 已匹配,避免后面重复
  1279. // } else {
  1280. // $diff[] = [
  1281. // 'product_code' => $row1['product_code'],
  1282. // 'product_name' => $row1['product_name']
  1283. // ];
  1284. // }
  1285. // }
  1286. //
  1287. // // 剩下 map2 中的就是表2中独有的
  1288. // foreach ($map2 as $name => $code) {
  1289. // $diff[] = [
  1290. // 'product_code' => $code,
  1291. // 'product_name' => $name
  1292. // ];
  1293. // }
  1294. //
  1295. //// // 存到新表
  1296. // $mongo->name('same_products')->insertAll($same);
  1297. // $mongo->name('diff_products')->insertAll($diff);
  1298. // }
  1299. // public function bacch() {
  1300. // $mongo = \think\Db::connect('mongodb');
  1301. //
  1302. // // 获取三个表的数据
  1303. // $table1 = $mongo->name('qcode_product')->select();
  1304. // $table2 = $mongo->name('qr_qcode_product_copy')->select();
  1305. //
  1306. // // 预加载qr_qcode_unit_copy表数据并建立索引
  1307. // $unitData = $mongo->name('qr_qcode_unit_copy')->select();
  1308. // $unitIndex = [];
  1309. // foreach ($unitData as $unit) {
  1310. // $unitIndex[$unit['code']] = [
  1311. // 'main_unit' => $unit['main_unit'] ?? null,
  1312. // 'sec_unit' => $unit['sec_unit'] ?? null,
  1313. // 'proportion' => $unit['proportion'] ?? null
  1314. // ];
  1315. // }
  1316. //
  1317. // // 索引表2数据,包含product_name和temple
  1318. // $table2Index = [];
  1319. // foreach ($table2 as $row) {
  1320. // $table2Index[$row['product_name']] = [
  1321. // 'product_code' => $row['product_code'],
  1322. // 'temple' => $row['temple'] ?? null
  1323. // ];
  1324. // }
  1325. //
  1326. // $differences = [];
  1327. //
  1328. // // 处理表1中的记录
  1329. // foreach ($table1 as $row) {
  1330. // if (!isset($table2Index[$row['product_name']]) &&
  1331. // strpos($row['product_code'], 'AB') !== 0) {
  1332. //
  1333. // // 获取单位信息
  1334. // $unitInfo = $unitIndex[$row['product_code']] ?? [
  1335. // 'main_unit' => null,
  1336. // 'sec_unit' => null,
  1337. // 'proportion' => null
  1338. // ];
  1339. //
  1340. // $differences[] = [
  1341. // 'product_code' => $row['product_code'],
  1342. // 'product_name' => $row['product_name'],
  1343. // 'temple' => null,
  1344. // 'main_unit' => $unitInfo['main_unit'],
  1345. // 'sec_unit' => $unitInfo['sec_unit'],
  1346. // 'proportion' => $unitInfo['proportion']
  1347. // ];
  1348. // }
  1349. // }
  1350. //
  1351. // // 处理表2中的记录
  1352. // foreach ($table2Index as $name => $data) {
  1353. // $existsInTable1 = false;
  1354. // foreach ($table1 as $row) {
  1355. // if ($row['product_name'] === $name) {
  1356. // $existsInTable1 = true;
  1357. // break;
  1358. // }
  1359. // }
  1360. //
  1361. // if (!$existsInTable1 && strpos($data['product_code'], 'AB') !== 0) {
  1362. // // 获取单位信息
  1363. // $unitInfo = $unitIndex[$data['product_code']] ?? [
  1364. // 'main_unit' => null,
  1365. // 'sec_unit' => null,
  1366. // 'proportion' => null
  1367. // ];
  1368. //
  1369. // $differences[] = [
  1370. // 'product_name' => $name,
  1371. // 'product_code' => $data['product_code'],
  1372. // 'temple' => $data['temple'],
  1373. // 'main_unit' => $unitInfo['main_unit'],
  1374. // 'sec_unit' => $unitInfo['sec_unit'],
  1375. // 'proportion' => $unitInfo['proportion'],
  1376. // 'delete_time' => '',
  1377. // 'sync_flag' => 0,
  1378. // 'code' => substr($data['product_code'], 0, 4)
  1379. // ];
  1380. // }
  1381. // }
  1382. //
  1383. // // 获取当前最大的oid_id值
  1384. // $maxOid = $mongo->name('qcode_product')->order('oid_id','desc')->find();
  1385. //
  1386. // $nextOid = $maxOid['oid_id'] ? $maxOid['oid_id'] + 1 : 1; // 如果没有记录则从1开始
  1387. // // 插入数据到qcode_product表
  1388. // foreach ($differences as $diff) {
  1389. // $insertData = [
  1390. // 'oid_id' => $nextOid++,
  1391. // 'product_code' => $diff['product_code'],
  1392. // 'product_name' => $diff['product_name'],
  1393. // 'temple' => $diff['temple'],
  1394. // 'main_unit' => $diff['main_unit'],
  1395. // 'sec_unit' => $diff['sec_unit'],
  1396. // 'proportion' => $diff['proportion'],
  1397. // 'delete_time' => $diff['delete_time'] ?? '',
  1398. // 'sync_flag' => $diff['sync_flag'] ?? 0,
  1399. // 'code' => $diff['code'] ?? substr($diff['product_code'], 0, 4)
  1400. // ];
  1401. // $mongo->name('qcode_product')->insert($insertData);
  1402. // }
  1403. //
  1404. // echo "成功插入 " . count($differences) . " 条记录";
  1405. // }
  1406. }