QcodeBach.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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\common\controller\Backend;
  8. use \think\Session;
  9. /**
  10. *
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class QcodeBach extends Backend
  15. {
  16. /**
  17. * Product模型对象
  18. * @var \app\admin\model\QcodeBach
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\QcodeBach();
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = false;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. return $this->view->fetch();
  41. }
  42. /**
  43. * 查看
  44. */
  45. public function table1()
  46. {
  47. //当前是否为关联查询
  48. $this->relationSearch = false;
  49. //设置过滤方法
  50. $this->request->filter(['strip_tags', 'trim']);
  51. if ($this->request->isAjax()) {
  52. //如果发送的来源是Selectpage,则转发到Selectpage
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. $where = [
  57. 'delete_time'=> ''
  58. ];
  59. $req = input();
  60. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  61. $order = $req['order'];
  62. $offset = $req['offset'];
  63. $limit = $req['limit'];
  64. // 构造模糊查询条件
  65. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  66. // $filter = ['field' => $regex];
  67. $filter = json_decode($req['filter'], true);
  68. foreach ($filter as $k => $v){
  69. $where[$k] = new \MongoDB\BSON\Regex($v);
  70. }
  71. $userInfo = Session::get('admin');
  72. $company_id = (int)$userInfo['company'];
  73. $total = $this->model->name($company_id.'_'."qcode_bach")->where($where)
  74. ->order($sort,$order)
  75. ->skip($offset)
  76. ->select();
  77. $list = $this->model->name($company_id.'_'."qcode_bach")->where($where)
  78. ->order($sort,$order)
  79. ->limit($limit)
  80. ->skip($offset)
  81. ->select();
  82. foreach ($list as $k=>$v) {
  83. $oid = $v['_id']->jsonSerialize();
  84. $list[$k]['id'] = $oid['$oid'];
  85. }
  86. $result = array("total" => count($total), "rows" => $list);
  87. return json($result);
  88. }
  89. return $this->view->fetch();
  90. }
  91. /**
  92. * 查看
  93. */
  94. public function table2()
  95. {
  96. //当前是否为关联查询
  97. $this->relationSearch = false;
  98. //设置过滤方法
  99. $this->request->filter(['strip_tags', 'trim']);
  100. if ($this->request->isAjax()) {
  101. //如果发送的来源是Selectpage,则转发到Selectpage
  102. if ($this->request->request('keyField')) {
  103. return $this->selectpage();
  104. }
  105. $userInfo = Session::get('admin');
  106. $company_id = (int)$userInfo['company'];
  107. $where = [
  108. // 'company_id'=>(int)$userInfo['company'],
  109. 'delete_time'=> ''
  110. ];
  111. $req = input();
  112. $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
  113. $order = $req['order'];
  114. $offset = $req['offset'];
  115. $limit = $req['limit'];
  116. // 构造模糊查询条件
  117. // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
  118. // $filter = ['field' => $regex];
  119. $filter = json_decode($req['filter'], true);
  120. foreach ($filter as $k => $v){
  121. $where[$k] = new \MongoDB\BSON\Regex($v);
  122. }
  123. $db = new QcodeLarge();
  124. $rows = $db->name($company_id.'_'."qcode_large")
  125. ->where('delete_time','')
  126. ->column('product_id');
  127. $rows = array_values($rows);
  128. $total = $this->model->where($where)
  129. ->where('_id','in',$rows)
  130. ->order($sort,$order)
  131. ->skip($offset)
  132. ->select();
  133. $list = $this->model->where($where)
  134. ->where('_id','in',$rows)
  135. ->order($sort,$order)
  136. ->limit($limit)
  137. ->skip($offset)
  138. ->select();
  139. foreach ($list as $k=>$v) {
  140. $oid = $v['_id']->jsonSerialize();
  141. $list[$k]['id'] = $oid['$oid'];
  142. }
  143. $result = array("total" => count($total), "rows" => $list);
  144. return json($result);
  145. }
  146. return $this->view->fetch();
  147. }
  148. public function vo2()
  149. {
  150. $num1 = 0;
  151. $num2 = 5;
  152. if($num2<=$num1){
  153. $this->error('同步num1到num2之间的数据, 要求num2大于num1');
  154. }
  155. $qcodeGsmc = new QcodeGsmc();
  156. // 连接到其他数据库
  157. $config = [
  158. 'type' => 'mysql',
  159. 'hostname' => '127.0.0.1',
  160. 'database' => 'dm_7in6_com',
  161. 'username' => 'root',
  162. 'password' => 'root',
  163. 'charset' => 'utf8mb4',
  164. 'prefix' => 'qr_',
  165. ];
  166. $db = Db::connect($config);
  167. //查询主表记录
  168. $rows1 = $db->name('qcode_gsmc')
  169. ->limit($num1,$num2-$num1)
  170. ->select();
  171. foreach($rows1 as $v){
  172. //1. 查询mongodb中是否存在该条记录
  173. $bool = $qcodeGsmc->where('oid_id',$v['id'])->find();
  174. if ($bool) continue;
  175. //2. 获取设置主表数据
  176. $row = $v;
  177. unset($row['id']);
  178. $row = array_merge(['oid_id'=>$v['id']],$row);
  179. //3. 插入主表记录到mongodb中
  180. $qcodeGsmc = new QcodeGsmc();
  181. $qcodeGsmc->save($row);
  182. }
  183. $this->success('成功');
  184. }
  185. /**
  186. * 新增
  187. */
  188. public function add()
  189. {
  190. if ($this->request->isAjax()){
  191. $req = $this->request->post();
  192. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  193. $this->error('请填写编号');
  194. }else{
  195. $data = [
  196. 'product_code'=>$req['row']['product_code'],
  197. 'delete_time'=>'',
  198. ];
  199. if ($this->model->where($data)->find()){
  200. $this->error('编号已存在');
  201. }
  202. }
  203. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  204. $this->error('请填写名称');
  205. }else{
  206. $data = [
  207. 'product_name'=>$req['row']['product_name'],
  208. 'delete_time'=>'',
  209. ];
  210. if ($this->model->where($data)->find()){
  211. $this->error('名称已存在');
  212. }
  213. }
  214. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  215. $this->error('请填写辅料代号');
  216. }
  217. if(!isset($req['row']['main_unit'])){
  218. $this->error('请填写主计量单位');
  219. }
  220. if(!isset($req['row']['sec_unit'])){
  221. $this->error('请填写辅计量单位');
  222. }
  223. if(!isset($req['row']['proportion'])){
  224. $this->error('请填写换算关系');
  225. }
  226. $data = [
  227. 'product_code' =>$req['row']['product_code'],
  228. 'product_name' =>$req['row']['product_name'],
  229. 'temple' =>$req['row']['temple'],
  230. 'main_unit' =>$req['row']['main_unit'],
  231. 'sec_unit' =>$req['row']['sec_unit'],
  232. 'proportion' =>$req['row']['proportion'],
  233. 'create_time' =>date('Y-m-d H:i:s'),
  234. ];
  235. //插入数据
  236. $re = $this->model->save($data);
  237. if($re){
  238. $this->success('成功');
  239. }else{
  240. $this->error('失败');
  241. }
  242. }
  243. return $this->view->fetch();
  244. }
  245. /**
  246. * 编辑
  247. */
  248. public function edit($ids = NULL)
  249. {
  250. if ($this->request->isAjax()){
  251. $req = $this->request->post();
  252. if(!isset($req['row']['product_code']) || empty($req['row']['product_code'])){
  253. $this->error('请填写编号');
  254. }else{
  255. $data = [
  256. 'product_code'=>$req['row']['product_code'],
  257. 'delete_time'=>'',
  258. ];
  259. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  260. $this->error('编号已存在');
  261. }
  262. }
  263. if(!isset($req['row']['product_name']) || empty($req['row']['product_name'])){
  264. $this->error('请填写名称');
  265. }else{
  266. $data = [
  267. 'product_name'=>$req['row']['product_name'],
  268. 'delete_time'=>'',
  269. ];
  270. if ($this->model->where($data)->where('_id','neq',$ids)->find()){
  271. $this->error('名称已存在');
  272. }
  273. }
  274. if(!isset($req['row']['temple']) || empty($req['row']['temple'])){
  275. $this->error('请填写辅料代号');
  276. }
  277. if(!isset($req['row']['main_unit'])){
  278. $this->error('请填写主计量单位');
  279. }
  280. if(!isset($req['row']['sec_unit'])){
  281. $this->error('请填写辅计量单位');
  282. }
  283. if(!isset($req['row']['proportion'])){
  284. $this->error('请填写换算关系');
  285. }
  286. $data = [
  287. 'product_code' =>$req['row']['product_code'],
  288. 'product_name' =>$req['row']['product_name'],
  289. 'temple' =>$req['row']['temple'],
  290. 'main_unit' =>$req['row']['main_unit'],
  291. 'sec_unit' =>$req['row']['sec_unit'],
  292. 'proportion' =>$req['row']['proportion'],
  293. 'update_time' =>date('Y-m-d H:i:s'),
  294. ];
  295. //修改数据
  296. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  297. $re = $department->save($data);
  298. if($re){
  299. $this->success('修改成功');
  300. }else{
  301. $this->error('修改失败');
  302. }
  303. }
  304. $row = $this->model->where('_id',$ids)->find();
  305. $this->view->assign('row',$row);
  306. return $this->view->fetch();
  307. }
  308. /**
  309. * 删除
  310. */
  311. public function del($ids = NULL)
  312. {
  313. $department = \app\admin\model\QcodeProduct::get(['_id' => new \MongoDB\BSON\ObjectID($ids)]);
  314. $department->delete_time = date('Y-m-d H:i:s');
  315. $re = $department->save();
  316. if($re){
  317. $this->success('删除成功');
  318. }else{
  319. $this->error('删除失败');
  320. }
  321. }
  322. /**
  323. * 绑定
  324. */
  325. public function bind($ids = NULL)
  326. {
  327. $userInfo = Session::get('admin');
  328. $company_id = (int)$userInfo['company'];
  329. $db = new QcodeCompany();
  330. //查询是否已经添加
  331. $row = $db->name($company_id.'_'."qcode_company")
  332. ->where(['delete_time'=>'','product_id'=>$ids])
  333. ->find();
  334. if($row) $this->success('已添加');
  335. $data = [
  336. 'product_id'=>$ids,
  337. 'create_time'=>date('Y-m-d H:i:s'),
  338. ];
  339. //插入qcode_company数据
  340. $bool = $db->save($data);
  341. if($bool){
  342. $this->success('添加成功');
  343. }else{
  344. $this->error('添加失败');
  345. }
  346. }
  347. /**
  348. * 解绑
  349. */
  350. public function unbind($ids = NULL)
  351. {
  352. $userInfo = Session::get('admin');
  353. $company_id = (int)$userInfo['company'];
  354. $db = new QcodeCompany();
  355. //查询是否已经添加
  356. $bool = $db->name($company_id.'_'."qcode_company")
  357. ->where(['delete_time'=>'','product_id'=>$ids])
  358. ->update(['delete_time'=>date('Y-m-d H:i:s')]);
  359. if($bool){
  360. $this->success('删除成功');
  361. }else{
  362. $this->error('删除失败');
  363. }
  364. }
  365. /**
  366. * 补打标签
  367. */
  368. public function reprint()
  369. {
  370. if ($this->request->isAjax()) {
  371. $data = $this->request->param();
  372. parse_str($data['data'],$req);
  373. if(!isset($req['row']['nickname']) || empty($req['row']['nickname'])){
  374. $this->error('请填写公司名称');
  375. }
  376. if((!isset($req['row']['product_name']) || empty($req['row']['product_name'])) && (!isset($req['row']['product_code']) || empty($req['row']['product_code']))){
  377. $this->error('请填写辅料名称或辅料编号');
  378. }
  379. if(!isset($req['row']['sqrcd']) || empty($req['row']['sqrcd'])){
  380. $this->error('请填写配盘数');
  381. }
  382. if(!isset($req['row']['num']) || empty($req['row']['num'])){
  383. $this->error('请填写数量');
  384. }
  385. //查询公司信息
  386. $qcodeGsmc = new QcodeGsmc();
  387. $print_code = $qcodeGsmc
  388. ->field('id, nickname, print_code')
  389. ->where('nickname',$req['row']['nickname'])
  390. ->find();
  391. if (!$print_code) $this->error('未查到公司信息');
  392. $print_code = json_decode($print_code,true);
  393. //查询产品信息
  394. $qcodeProduct = new QcodeProduct();
  395. if(!empty($req['row']['product_code'])){
  396. $sel = $qcodeProduct
  397. ->field('id, product_name, product_code, temple')
  398. ->where('product_code',$req['row']['product_code'])
  399. ->find();
  400. }else{
  401. $sel = $qcodeProduct
  402. ->field('id, product_name, product_code, temple')
  403. ->where('product_name',$req['row']['product_name'])
  404. ->find();
  405. }
  406. if (!$sel) $this->error('未查到辅料信息');
  407. $sel = json_decode($sel,true);
  408. $data = [
  409. 'company_name'=>$req['row']['nickname'],
  410. 'product_name'=>$sel['product_name'],
  411. 'sqrcd'=>$req['row']['sqrcd'],
  412. 'num'=>$req['row']['num'],
  413. 'main_unit'=>'盘',
  414. 'sec_unit'=>'万米',
  415. 'date'=>date('Y-m-d'),
  416. 'batch'=>substr(date('Ymd'),2,6),
  417. 'l_flow'=>1,
  418. ];
  419. $num = str_pad($req['row']['num'], 3, '0', STR_PAD_LEFT);
  420. $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';
  421. $data['pCode'] = $this->qrcode($qrcode);
  422. $data['qrcode'] = $qrcode;
  423. $this->success('成功','',$data);
  424. }
  425. return $this->view->fetch();
  426. }
  427. //公司搜索
  428. public function sel_n()
  429. {
  430. $name = $this->request->param()['q_word'][0];
  431. $where = [
  432. 'nickname'=>new \MongoDB\BSON\Regex($name),
  433. 'delete_time'=>'',
  434. ];
  435. $qcodeGsmc = new QcodeGsmc();
  436. $sel = $qcodeGsmc
  437. ->field('nickname')
  438. ->where($where)
  439. ->limit(0,10)
  440. ->select();
  441. return json(['list' => $sel, 'total' => count($sel)]);
  442. }
  443. //辅料名称搜索
  444. public function sel_p()
  445. {
  446. $name = $this->request->param()['q_word'][0];
  447. $where = [
  448. 'product_name'=>new \MongoDB\BSON\Regex($name),
  449. 'delete_time'=>'',
  450. ];
  451. $qcodeProduct = new QcodeProduct();
  452. $sel = $qcodeProduct
  453. ->field('product_name, product_code')
  454. ->where($where)
  455. ->limit(0,10)
  456. ->select();
  457. foreach ($sel as $k=>$v) {
  458. $oid = $v['_id']->jsonSerialize();
  459. $sel[$k]['id'] = $oid['$oid'];
  460. }
  461. return json(['list' => $sel, 'total' => count($sel)]);
  462. }
  463. //辅料编码搜索
  464. public function sel_c()
  465. {
  466. $name = $this->request->param()['q_word'][0];
  467. $where = [
  468. 'product_code'=>new \MongoDB\BSON\Regex($name),
  469. 'delete_time'=>'',
  470. ];
  471. $qcodeProduct = new QcodeProduct();
  472. $sel = $qcodeProduct
  473. ->field('id, product_name, product_code')
  474. ->where($where)
  475. ->limit(0,10)
  476. ->select();
  477. foreach ($sel as $k=>$v) {
  478. $oid = $v['_id']->jsonSerialize();
  479. $sel[$k]['id'] = $oid['$oid'];
  480. }
  481. return json(['list' => $sel, 'total' => count($sel)]);
  482. }
  483. /**
  484. * 二维码生成类
  485. */
  486. public function qrcode($url)//二维码生成类
  487. {
  488. $url=$url;
  489. $level=3;
  490. $size=6;
  491. Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
  492. $errorCorrectionLevel =intval($level) ;//容错级别
  493. $matrixPointSize = intval($size);//生成图片大小
  494. //生成二维码图片
  495. $object = new \QRcode();
  496. //打开缓冲区
  497. ob_start();
  498. $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
  499. //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
  500. $imageString = base64_encode(ob_get_contents());
  501. //关闭缓冲区
  502. ob_end_clean();
  503. //把生成的base64字符串返回给前端
  504. // $data = array(
  505. // 'labelcode'=>$url,
  506. // 'code'=>200,
  507. // 'data'=>$imageString,
  508. // 'product_code'=>$url
  509. // );
  510. return 'data:image/png;base64,'.$imageString;
  511. }
  512. }