| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\QcodeCompany;
- use app\admin\model\QcodeProduct;
- use app\admin\model\QcodeGsmc;
- use app\admin\model\QcodeLarge;
- use app\admin\model\QcodeSmall;
- use app\common\controller\Backend;
- use \think\Session;
- use think\Db;
- use MongoDB\BSON\ObjectId;
- /**
- * 批次列表
- */
- class QcodeBach extends Backend
- {
- /**
- * Product模型对象
- * @var \app\admin\model\QcodeBach
- */
- protected $model = null;
- protected $multiFields = 'status';
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\QcodeBach();
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- return $this->view->fetch();
- }
- /**
- * 主表查询
- */
- public function bach()
- {
- $this->relationSearch = false;
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- // 如果是 Selectpage 请求则跳转
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- // 默认条件:未删除
- $where = ['delete_time' => ''];
- // 获取前端传参
- $req = input();
- // 安全处理排序字段和排序方式
- $sort = $req['sort'] ?? 'id';
- $sort = $sort == 'id' ? '_id' : $sort;
- // 定义允许排序的字段,防止注入
- $allowedSortFields = ['_id', 'create_time', 'update_time', 'name']; // 按实际情况添加
- if (!in_array($sort, $allowedSortFields)) {
- $sort = '_id';
- }
- $orderStr = strtolower($req['order'] ?? 'desc');
- $order = $orderStr === 'asc' ? 1 : -1; // MongoDB 排序必须是 1 或 -1
- $offset = $req['offset'] ?? 0;
- $limit = $req['limit'] ?? 20;
- // 解析 filter 筛选条件
- // $filter = json_decode($req['filter'], true);
- // if (is_array($filter)) {
- // foreach ($filter as $k => $v) {
- // $where[$k] = new \MongoDB\BSON\Regex($v);
- // }
- // }
- $list = [];
- // 超级管理员:查询所有公司
- if (isSuperAdmin()) {
- $companies = Db::name('admin')
- ->field('company')
- ->where('company', '<>', '')
- ->where('kes', '<>', '')
- ->distinct(true)
- ->select();
- foreach ($companies as $row) {
- $cid = $row['company'];
- $rows = $this->model->name($cid . '_qcode_bach')
- ->where($where)
- ->select(); // 不分页,后续统一处理
- foreach ($rows as &$item) {
- $oid = $item['_id']->jsonSerialize();
- $item['id'] = $oid['$oid'];
- }
- $list = array_merge($list, $rows);
- }
- // 全局排序(按 create_time 排序)
- usort($list, function ($a, $b) use ($sort, $order) {
- $valA = $a[$sort] ?? 0;
- $valB = $b[$sort] ?? 0;
- return $order === 1 ? ($valA <=> $valB) : ($valB <=> $valA);
- });
- // 总数与分页
- $total = count($list);
- $list = array_slice($list, $offset, $limit);
- } else {
- // 普通用户:只查本公司
- $total = $this->model->name($company_id . '_qcode_bach')->where($where)->count();
- $list = $this->model->name($company_id . '_qcode_bach')->where($where)
- ->order([$sort => $order])
- ->limit($limit)
- ->skip($offset)
- ->select();
- foreach ($list as &$item) {
- $oid = $item['_id']->jsonSerialize();
- $item['id'] = $oid['$oid'];
- }
- }
- return json(["total" => $total, "rows" => $list]);
- }
- return $this->view->fetch();
- }
- /**
- * 大件列表
- */
- /**
- * 大件列表
- */
- public function large()
- {
- $this->relationSearch = false;
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $where = ['delete_time' => ''];
- $req = input();
- if ($req['filter'] == '{}') {
- return json(["total" => 0, "rows" => []]);
- }
- $sort = ($req['sort'] == 'id') ? '_id' : $req['sort'];
- $order = $req['order'];
- $filter = json_decode($req['filter'], true);
- if (is_array($filter)) {
- foreach ($filter as $k => $v) {
- $where[$k] = new \MongoDB\BSON\Regex($v);
- }
- }
- $list = [];
- $total = 0;
- $db = new QcodeLarge();
- $qcodeSmall = new QcodeSmall();
- // 超级管理员查询所有公司数据
- if (isSuperAdmin()) {
- $companies = Db::name('admin')
- ->field('company')
- ->where('company', '<>', '')
- ->where('kes', '<>', '')
- ->distinct(true)
- ->select();
- foreach ($companies as $row) {
- $cid = $row['company'];
- $table = $cid . '_qcode_large';
- $rows = $db->name($table)->where($where)->order($sort, $order)->select();
- $total += count($rows);
- foreach ($rows as $k => $v) {
- $oid = $v['_id']->jsonSerialize();
- $v['id'] = $oid['$oid'];
- $v['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
- $v['small_num'] = $qcodeSmall->name($cid . '_qcode_small')->where('large_id', $oid['$oid'])->count();
- $v['l_weight'] = floatval($v['l_weight']) / 100;
- $list[] = $v;
- }
- }
- } else {
- // 普通用户只查自己公司表
- $table = $company_id . '_qcode_large';
- $list = $db->name($table)->where($where)->order($sort, $order)->select();
- $total = count($list);
- foreach ($list as $k => $v) {
- $oid = $v['_id']->jsonSerialize();
- $list[$k]['id'] = $oid['$oid'];
- $list[$k]['l_flow'] = ltrim(substr($v['code'], 53, 6), '0');
- $list[$k]['small_num'] = $qcodeSmall->name($company_id . '_qcode_small')->where('large_id', $oid['$oid'])->count();
- $list[$k]['l_weight'] = floatval($v['l_weight']) / 100;
- }
- }
- return json(["total" => $total, "rows" => $list]);
- }
- return $this->view->fetch();
- }
- /**
- * 小件列表
- */
- // public function small()
- // {
- // //设置过滤方法
- // $this->request->filter(['strip_tags', 'trim']);
- // $req = $this->request->param();
- // if ($this->request->isAjax()) {
- // //如果发送的来源是Selectpage,则转发到Selectpage
- // if ($this->request->request('keyField')) {
- // return $this->selectpage();
- // }
- // $userInfo = Session::get('admin');
- // $company_id = (int)$userInfo['company'];
- // $where = [
- // 'delete_time'=> '',
- // 'large_id'=> $req['large_id']
- // ];
- //
- // $req = input();
- // $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
- // $order = $req['order'];
- // $offset = $req['offset'];
- // $limit = $req['limit'];
- //
- // $filter = json_decode($req['filter'], true);
- // foreach ($filter as $k => $v){
- // $where[$k] = new \MongoDB\BSON\Regex($v);
- // }
- //
- // $list = [];
- // $total = 0;
- //
- // $qcodeSmall = new QcodeSmall();
- //
- // // 超级管理员查询所有公司
- // if (isSuperAdmin()) {
- // $companies = Db::name('admin')
- // ->field('company')
- // ->where('company', '<>', '')
- // ->where('kes', '<>', '')
- // ->distinct(true)
- // ->select();
- //
- //
- // foreach ($companies as $row) {
- // $cid = $row['company'];
- // $table = $cid . '_qcode_small';
- //
- // // 获取数据(不分页,后面统一分页)
- // $rows = $qcodeSmall->name($table)->where($where)->select();
- //
- // foreach ($rows as &$v) {
- // $oid = $v['_id']->jsonSerialize();
- // $v['id'] = $oid['$oid'];
- // $v['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
- // if (strpos($v['l_flow'], '-') !== false) {
- // $parts = explode('-', $v['l_flow']);
- // $v['l_flows'] = end($parts);
- // } else {
- // $v['l_flows'] = '';
- // }
- // }
- //
- // $list = array_merge($list, $rows);
- // }
- //
- // // 全部公司数据合并后,统一排序
- // usort($list, function ($a, $b) use ($sort, $order) {
- // $valA = $a[$sort] ?? '';
- // $valB = $b[$sort] ?? '';
- // return $order === 'asc' ? ($valA <=> $valB) : ($valB <=> $valA);
- // });
- //
- // // 总条数
- // $total = count($list);
- // // 分页截取
- // $list = array_slice($list, $offset, $limit);
- // } else {
- // // 普通用户仅查本公司
- // $table = $company_id . '_qcode_small';
- //
- // $total = $qcodeSmall->name($table)->where($where)->count();
- //
- // $list = $qcodeSmall->name($table)->where($where)
- // ->order([$sort => $order])
- // ->limit($limit)
- // ->skip($offset)
- // ->select();
- //
- // foreach ($list as $k => $v) {
- // $oid = $v['_id']->jsonSerialize();
- // $list[$k]['id'] = $oid['$oid'];
- // $list[$k]['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
- // }
- // }
- //
- // return json(["total" => $total, "rows" => $list]);
- // }
- // return $this->view->fetch();
- // }
- public function small()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- $req = $this->request->param();
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $where = [
- 'delete_time'=> '',
- 'large_id'=> $req['large_id']
- ];
- $req = input();
- $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
- $order = $req['order'];
- $offset = $req['offset'];
- $limit = $req['limit'];
- // 构造模糊查询条件
- // $regex = new MongoDB\BSON\Regex('.*abc.*', 'i');
- // $filter = ['field' => $regex];
- $filter = json_decode($req['filter'], true);
- foreach ($filter as $k => $v){
- $where[$k] = new \MongoDB\BSON\Regex($v);
- }
- $qcodeSmall = new QcodeSmall();
- $total = $qcodeSmall->name($company_id.'_'."qcode_small")->where($where)->count();
- $list = $qcodeSmall->name($company_id.'_'."qcode_small")->where($where)
- ->order($sort,$order)
- ->limit($limit)
- ->skip($offset)
- ->select();
- foreach ($list as $k=>$v) {
- $oid = $v['_id']->jsonSerialize();
- $list[$k]['id'] = $oid['$oid'];
- $list[$k]['l_flow'] = $req['l_flow'].'-'.$v['l_flow'];
- if (strpos($v['l_flow'], '-') !== false) {
- $parts = explode('-', $v['l_flow']);
- $v['l_flows'] = end($parts);
- } else {
- $v['l_flows'] = '';
- }
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 小件导出状态修改(支持超级管理员操作单个公司表)
- * 支持处理单个ID和多个ID(逗号分隔)的情况
- */
- public function small_status()
- {
- $req = $this->request->param();
- $ids = $req['ids'];
- // 统一处理ids参数,转换为数组
- if (!is_array($ids)) {
- $ids = explode(',', $ids);
- }
- // 过滤空值
- $ids = array_filter($ids);
- if (empty($ids)) {
- $this->error("参数错误:ids不能为空");
- }
- // 判断是否是超级管理员
- if (isSuperAdmin()) {
- $this->handleSuperAdminMode($ids);
- } else {
- $this->handleNormalUserMode($ids);
- }
- }
- /**
- * 超级管理员处理模式
- */
- protected function handleSuperAdminMode($ids)
- {
- $qcodeSmall = new QcodeSmall();
- $companyTables = Db::name('admin')
- ->field('company')
- ->where('company', '<>', '')
- ->where('kes', '<>', '')
- ->distinct(true)
- ->select();
- $found = false;
- $successIds = [];
- $failedIds = [];
- foreach ($companyTables as $row) {
- $cid = $row['company'];
- $tableName = $cid . '_qcode_small';
- try {
- // 查询该表中存在的记录
- $records = $qcodeSmall->name($tableName)
- ->whereIn('_id', $ids)
- ->select();
- if (!empty($records)) {
- $found = true;
- // 批量更新状态
- foreach ($records as $record) {
- $newStatus = ($record['status'] == 0) ? 1 : 0;
- $result = $qcodeSmall->name($tableName)
- ->where('_id', $record['_id'])
- ->update(['status' => $newStatus]);
- if ($result) {
- $successIds[] = $record['_id'];
- } else {
- $failedIds[] = $record['_id'];
- }
- }
- }
- } catch (\Exception $e) {
- continue; // 表不存在,跳过
- }
- }
- if ($found) {
- $message = "超级管理员批量切换完成";
- if (!empty($successIds)) {
- $message .= ",成功:";
- }
- if (!empty($failedIds)) {
- $message .= ",失败:";
- }
- $this->success($message);
- } else {
- $this->error("在所有公司中未找到这些记录:" . implode(',', $ids));
- }
- }
- /**
- * 普通用户处理模式
- */
- protected function handleNormalUserMode($ids)
- {
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $qcodeSmall = new QcodeSmall();
- $tableName = $company_id . '_qcode_small';
- $successIds = [];
- $failedIds = [];
- try {
- // 查询存在的记录
- $records = $qcodeSmall->name($tableName)
- ->whereIn('_id', $ids)
- ->select();
- if (empty($records)) {
- $this->error("未找到符合条件的记录");
- }
- // 批量更新状态
- foreach ($records as $record) {
- $newStatus = ($record['status'] == 0) ? 1 : 0;
- $result = $qcodeSmall->name($tableName)
- ->where('_id', $record['_id'])
- ->update(['status' => $newStatus]);
- if ($result) {
- $successIds[] = $record['_id'];
- } else {
- $failedIds[] = $record['_id'];
- }
- }
- $message = "批量切换完成";
- if (!empty($successIds)) {
- $message .= ",成功:" ;
- }
- if (!empty($failedIds)) {
- $message .= ",失败:" ;
- }
- $this->success($message);
- } catch (\Exception $e) {
- $this->error("操作失败:" . $e->getMessage());
- }
- }
- /**
- * 自动打码(大件)
- */
- public function print_l()
- {
- $this->request->filter(['strip_tags', 'trim']);
- $req = $this->request->param();
- if (!$this->request->isAjax()) {
- $this->view->assign('ids', $req['ids']);
- return $this->view->fetch();
- }
- $ids = $req['ids'];
- $type = $req['type'];
- $numn = $req['numn'];
- $qcodeLarge = new QcodeLarge();
- $qcodeSmall = new QcodeSmall();
- $qcodeProduct = new QcodeProduct();
- $rows = [];
- // 超级管理员逻辑:遍历所有公司表查找匹配ID
- if (isSuperAdmin()) {
- $companyList = Db::name('admin')
- ->field('company')
- ->where('company', '<>', '')
- ->where('kes', '<>', '')
- ->distinct(true)
- ->select();
- foreach ($companyList as $company) {
- $cid = $company['company'];
- $tableLarge = $cid . '_qcode_large';
- $tableBach = $cid . '_qcode_bach';
- $tableSmall = $cid . '_qcode_small';
- // 检查是否有匹配的大件ID
- try {
- $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
- if (empty($large)) continue;
- $largeArr = json_decode($large, true);
- $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
- $bachArr = json_decode($bach, true);
- $row = $qcodeProduct->where('product_code', $bachArr['matter_no'])->find();
- $rowArr = json_decode($row, true);
- $company_name = $bachArr['supplier_name'];
- $product_name = $bachArr['matter_name'];
- $main_unit = $rowArr['main_unit'];
- $sec_unit = $rowArr['sec_unit'];
- $proportion = $rowArr['proportion'];
- foreach ($ids as $key => $value) {
- $rowData = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
- $rowData = json_decode($rowData, true);
- $code = $rowData['code'];
- $rows[$key] = [
- 'company_name' => $company_name,
- 'product_name' => $product_name,
- 'sqrcd' => $qcodeSmall->name($tableSmall)->where('large_id', $value)->count(),
- 'main_unit' => $main_unit,
- 'sec_unit' => $sec_unit,
- 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
- 'l_flow' => ltrim(substr($code, 53, 6), '0'),
- 'qrcode' => $code,
- 'pCode' => $this->qrcode($code),
- 'batch' => (substr($code, 76, 10) == '0000000000') ?
- ltrim(substr($code, 66, 10), '0') :
- ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
- ];
- // 数量处理
- if ($rowData['l_num'] === null) {
- $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
- } elseif ($rowData['l_num'] == 0) {
- $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] /100 : (
- $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
- );
- } else {
- $rows[$key]['num'] = $rowData['l_num']/10000;
- }
- }
- // 查到就停止遍历
- break;
- } catch (\Exception $e) {
- continue;
- }
- }
- } else {
- // 普通用户逻辑,只能查看所属公司数据
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $tableLarge = $company_id . '_qcode_large';
- $tableBach = $company_id . '_qcode_bach';
- $tableSmall = $company_id . '_qcode_small';
- $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
- $largeArr = json_decode($large, true);
- $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
- $bachArr = json_decode($bach, true);
- $row = $qcodeProduct->where('product_code', $bachArr['matter_no'])->find();
- $rowArr = json_decode($row, true);
- $company_name = $bachArr['supplier_name'];
- $product_name = $bachArr['matter_name'];
- $main_unit = $rowArr['main_unit'];
- $sec_unit = $rowArr['sec_unit'];
- $proportion = $rowArr['proportion'];
- foreach ($ids as $key => $value) {
- $rowData = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
- $rowData = json_decode($rowData, true);
- $code = $rowData['code'];
- $rows[$key] = [
- 'company_name' => $company_name,
- 'product_name' => $product_name,
- 'sqrcd' => $qcodeSmall->name($tableSmall)->where('large_id', $value)->count(),
- 'main_unit' => $main_unit,
- 'sec_unit' => $sec_unit,
- 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
- 'l_flow' => ltrim(substr($code, 53, 6), '0'),
- 'qrcode' => $code,
- 'pCode' => $this->qrcode($code),
- 'batch' => (substr($code, 76, 10) == '0000000000') ?
- ltrim(substr($code, 66, 10), '0') :
- ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0')
- ];
- if ($rowData['l_num'] === null) {
- $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
- } elseif ($rowData['l_num'] == 0) {
- $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] /100 : (
- $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
- );
- } else {
- $rows[$key]['num'] = $rowData['l_num'];
- }
- }
- }
- // 最终返回数据
- $data = [
- 'type' => $type,
- 'numn' => $numn,
- 'rows' => $rows,
- 'ids' => $ids,
- ];
- $this->success('成功', '', $data);
- }
- /**
- * 自动打码(小件)
- */
- public function print_s()
- {
- // 过滤请求参数
- $this->request->filter(['strip_tags', 'trim']);
- $req = $this->request->param();
- if (!$this->request->isAjax()) {
- $this->view->assign('ids', $req['ids']);
- return $this->view->fetch();
- }
- $ids = $req['ids'];
- $type = $req['type'];
- $numn = $req['numn'];
- $qcodeLarge = new QcodeLarge();
- $qcodeSmall = new QcodeSmall();
- $rows = [];
- $key = 0;
- // 超级管理员逻辑
- if (isSuperAdmin()) {
- $companyList = Db::name('admin')
- ->field('company')
- ->where('company', '<>', '')
- ->where('kes', '<>', '')
- ->distinct(true)
- ->select();
- foreach ($companyList as $company) {
- $cid = $company['company'];
- $tableLarge = $cid . '_qcode_large';
- $tableSmall = $cid . '_qcode_small';
- $tableBach = $cid . '_qcode_bach';
- try {
- $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
- if (empty($large)) continue;
- $largeArr = json_decode($large, true);
- $bach = $this->model->name($tableBach)->where('_id', $largeArr['bach_id'])->find();
- $bachArr = json_decode($bach, true);
- $company_name = $bachArr['supplier_name'];
- $product_name = $bachArr['matter_name'];
- foreach ($ids as $value) {
- $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
- $large = json_decode($large, true);
- $arr = $qcodeSmall->name($tableSmall)->where('large_id', $value)->column('_id');
- foreach ($arr as $v) {
- $oid = is_object($v) ? $v->jsonSerialize()['$oid'] : $v;
- $small = $qcodeSmall->name($tableSmall)->where('_id', $oid)->find();
- $small = json_decode($small, true);
- $code = $small['code'];
- $rows[$key] = [
- 'company_name' => $company_name,
- 'product_name' => $product_name,
- 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
- 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
- 'qrcode' => $code,
- // 'pCode' => $this->qrcode($code), // 如需启用二维码图像,取消注释
- 'batch' => (substr($code, 76, 10) == '0000000000') ?
- ltrim(substr($code, 66, 10), '0') :
- ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
- ];
- $key++;
- }
- }
- break; // 找到数据即跳出公司循环
- } catch (\Exception $e) {
- continue;
- }
- }
- } else {
- // 普通管理员逻辑
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $tableLarge = $company_id . '_qcode_large';
- $tableSmall = $company_id . '_qcode_small';
- $tableBach = $company_id . '_qcode_bach';
- $large = $qcodeLarge->name($tableLarge)->where('_id', $ids[0])->find();
- $bach = $this->model->name($tableBach)->where('_id', json_decode($large, true)['bach_id'])->find();
- $company_name = json_decode($bach, true)['supplier_name'];
- $product_name = json_decode($bach, true)['matter_name'];
- foreach ($ids as $value) {
- $large = $qcodeLarge->name($tableLarge)->where('_id', $value)->find();
- $large = json_decode($large, true);
- $arr = $qcodeSmall->name($tableSmall)->where('large_id', $value)->column('_id');
- foreach ($arr as $v) {
- $oid = is_object($v) ? $v->jsonSerialize()['$oid'] : $v;
- $small = $qcodeSmall->name($tableSmall)->where('_id', $oid)->find();
- $small = json_decode($small, true);
- $code = $small['code'];
- $rows[$key] = [
- 'company_name' => $company_name,
- 'product_name' => $product_name,
- 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
- 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
- 'qrcode' => $code,
- // 'pCode' => $this->qrcode($code),
- 'batch' => (substr($code, 76, 10) == '0000000000') ?
- ltrim(substr($code, 66, 10), '0') :
- ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
- ];
- $key++;
- }
- }
- }
- $data = [
- 'type' => $type,
- 'numn' => $numn,
- 'rows' => $rows,
- 'ids' => $ids,
- ];
- $this->success('成功', '', $data);
- }
- /**
- * 手动打码(小件)
- */
- public function print_ls()
- {
- // 设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- $req = $this->request->param();
- if (!$this->request->isAjax()) {
- $this->view->assign('ids', $req['ids']);
- return $this->view->fetch();
- }
- $ids = $req['ids'];
- $type = $req['type'];
- $numn = $req['numn'];
- $qcodeLarge = new QcodeLarge();
- $qcodeSmall = new QcodeSmall();
- $rows = [];
- // 超级管理员逻辑
- if (isSuperAdmin()) {
- $companyList = Db::name('admin')
- ->field('company')
- ->where('company', '<>', '')
- ->where('kes', '<>', '')
- ->distinct(true)
- ->select();
- foreach ($companyList as $company) {
- $cid = $company['company'];
- $tableSmall = $cid . '_qcode_small';
- $tableLarge = $cid . '_qcode_large';
- $tableBach = $cid . '_qcode_bach';
- try {
- $small = $qcodeSmall->name($tableSmall)->where('_id', $ids)->find();
- if (empty($small)) continue;
- $small = json_decode($small, true);
- $large = $qcodeLarge->name($tableLarge)->where('_id', $small['large_id'])->find();
- $large = json_decode($large, true);
- $bach = $this->model->name($tableBach)->where('_id', $small['bach_id'])->find();
- $bach = json_decode($bach, true);
- $code = $small['code'];
- $rows[0] = [
- 'company_name' => $bach['supplier_name'],
- 'product_name' => $bach['matter_name'],
- 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
- 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
- 'qrcode' => $code,
- // 'pCode' => $this->qrcode($code),
- 'batch' => (substr($code, 76, 10) == '0000000000') ?
- ltrim(substr($code, 66, 10), '0') :
- ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
- ];
- break; // 查到就退出循环
- } catch (\Exception $e) {
- continue;
- }
- }
- } else {
- // 普通管理员逻辑
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $tableSmall = $company_id . '_qcode_small';
- $tableLarge = $company_id . '_qcode_large';
- $tableBach = $company_id . '_qcode_bach';
- $small = $qcodeSmall->name($tableSmall)->where('_id', $ids)->find();
- $small = json_decode($small, true);
- $large = $qcodeLarge->name($tableLarge)->where('_id', $small['large_id'])->find();
- $large = json_decode($large, true);
- $bach = $this->model->name($tableBach)->where('_id', $small['bach_id'])->find();
- $bach = json_decode($bach, true);
- $code = $small['code'];
- $rows[0] = [
- 'company_name' => $bach['supplier_name'],
- 'product_name' => $bach['matter_name'],
- 'date' => substr_replace(substr_replace('20' . substr($code, 38, 6), '-', 4, 0), '-', 7, 0),
- 'l_flow' => ltrim(substr($large['code'], 53, 6), '0') . '-' . $small['l_flow'],
- 'qrcode' => $code,
- // 'pCode' => $this->qrcode($code),
- 'batch' => (substr($code, 76, 10) == '0000000000') ?
- ltrim(substr($code, 66, 10), '0') :
- ltrim(substr($code, 66, 10), '0') . '、' . ltrim(substr($code, 76, 10), '0'),
- ];
- }
- $data = [
- 'type' => $type,
- 'numn' => $numn,
- 'rows' => $rows,
- 'ids' => $ids,
- ];
- $this->success('成功', '', $data);
- }
- /**
- * 打印数量设置
- */
- public function set_num()
- {
- $req = $this->request->param();
- //获取数据
- $status = $req['status'];
- $num = (int)$req['num'];
- $ids = $req['ids'];
- //获取company_id
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- //自动打印大件
- if($status==1){
- $qcodeLarge = new QcodeLarge();
- foreach ($ids as $v){
- $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setInc('p_nums',$num);
- $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$v)->setField('l_print','1');
- }
- }elseif($status==2){
- $qcodeSmall = new QcodeSmall();
- foreach ($ids as $v){
- $qcodeSmall->name($company_id.'_'."qcode_small")->where('large_id',$v)->setInc('p_nums',$num);
- }
- }elseif($status==3){
- $qcodeSmall = new QcodeSmall();
- $qcodeSmall->name($company_id.'_'."qcode_small")->where('_id',$ids)->setInc('p_nums',$num);
- }
- }
- /**
- * 导出excel跳转
- */
- public function exp()
- {
- //获取产品配置菜单id
- $bool = db('auth_rule')->field('id')->where('title','发货管理')->find();
- if ($bool){
- $this->success('成功','',$bool);
- }else{
- $this->error('未获取到菜单','',$bool);
- }
- }
- /**
- * 修改大件重量数量
- */
- public function edit($ids = NULL)
- {
- $req = $this->request->param();
- if ($this->request->isAjax()){
- parse_str($req['data'],$req);
- if(!isset($req['row']['type']) || empty($req['row']['type'])){
- $this->error('请选择修改类型');
- }
- if(!isset($req['row']['numn']) || empty($req['row']['numn'])){
- $this->error('请填写修改数量');
- }
- $ids = explode(',',$req['row']['ids']);
- $data = [
- 'update_time'=>time(),
- 'sync_flag'=>0
- ];
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $qcodeLarge = new QcodeLarge();
- if($req['row']['type']==1){//修改大件重量
- $data['l_weight'] = intval($req['row']['numn']*100);
- foreach($ids as $val){
- //查询code
- $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('code')->where('_id',$val)->find();
- $code = json_decode($large,true)['code'];
- if($code){
- $code1 = substr($code,0,59);
- $code2 = substr($code,65,85);
- $data['code'] = $code1.str_pad(intval($req['row']['numn'])*100,6, '0', STR_PAD_LEFT).$code2;
- //修改code和大件重量
- $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
- }
- }
- $this->success('修改成功');
- }
- if($req['row']['type']==2){//修改大件总张数
- $data['l_num'] = intval($req['row']['numn']);
- foreach($ids as $val){
- //查询l_num, 判断是否可以修改总张数
- $large = $qcodeLarge->name($company_id.'_'."qcode_large")->field('l_num')->where('_id',$val)->find();
- $l_num = json_decode($large,true)['l_num'];
- if($l_num > 0 || $l_num==null){
- //可以修改
- $qcodeLarge->name($company_id.'_'."qcode_large")->where('_id',$val)->update($data);
- }else{
- $this->error('该产品不能修改大件数量');
- }
- }
- $this->success('修改成功');
- }
- }
- $this->view->assign('ids',$req['ids']);
- return $this->view->fetch();
- }
- /**
- * 删除批次
- */
- public function del($ids = NULL)
- {
- $userInfo = Session::get('admin');
- $company_id = (int)$userInfo['company'];
- $qcodeLarge = new QcodeLarge();
- $qcodeSmall = new QcodeSmall();
- $data = [
- 'delete_time'=>time(),
- ];
- $bool1 = $this->model->name($company_id.'_'."qcode_bach")->where('_id',$ids)->update($data);
- $bool2 = $qcodeLarge->name($company_id.'_'."qcode_large")->where('bach_id',$ids)->update($data);
- $bool3 = $qcodeSmall->name($company_id.'_'."qcode_small")->where('bach_id',$ids)->update($data);
- if($bool1!==false && $bool2!==false && $bool3!==false){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- /**
- * 补打标签
- */
- public function reprint()
- {
- if ($this->request->isAjax()) {
- $data = $this->request->param();
- parse_str($data['data'],$req);
- if(!isset($req['row']['nickname']) || empty($req['row']['nickname'])){
- $this->error('请填写公司名称');
- }
- if((!isset($req['row']['product_name']) || empty($req['row']['product_name'])) && (!isset($req['row']['product_code']) || empty($req['row']['product_code']))){
- $this->error('请填写辅料名称或辅料编号');
- }
- if(!isset($req['row']['sqrcd']) || empty($req['row']['sqrcd'])){
- $this->error('请填写配盘数');
- }
- if(!isset($req['row']['num']) || empty($req['row']['num'])){
- $this->error('请填写数量');
- }
- //查询公司信息
- $qcodeGsmc = new QcodeGsmc();
- $print_code = $qcodeGsmc
- ->field('id, nickname, print_code')
- ->where('nickname',$req['row']['nickname'])
- ->find();
- if (!$print_code) $this->error('未查到公司信息');
- $print_code = json_decode($print_code,true);
- //查询产品信息
- $qcodeProduct = new QcodeProduct();
- if(!empty($req['row']['product_code'])){
- $sel = $qcodeProduct
- ->field('id, product_name, product_code, temple')
- ->where('product_code',$req['row']['product_code'])
- ->find();
- }else{
- $sel = $qcodeProduct
- ->field('id, product_name, product_code, temple')
- ->where('product_name',$req['row']['product_name'])
- ->find();
- }
- if (!$sel) $this->error('未查到辅料信息');
- $sel = json_decode($sel,true);
- $data = [
- 'company_name'=>$req['row']['nickname'],
- 'product_name'=>$sel['product_name'],
- 'sqrcd'=>$req['row']['sqrcd'],
- 'num'=>$req['row']['num'],
- 'main_unit'=>'盘',
- 'sec_unit'=>'万米',
- 'date'=>date('Y-m-d'),
- 'batch'=>substr(date('Ymd'),2,6),
- 'l_flow'=>1,
- ];
- $num = str_pad($req['row']['num'], 3, '0', STR_PAD_LEFT);
- $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';
- $data['pCode'] = $this->qrcode($qrcode);
- $data['qrcode'] = $qrcode;
- $this->success('成功','',$data);
- }
- return $this->view->fetch();
- }
- /**
- * 公司名称搜索
- */
- public function sel_n()
- {
- $name = $this->request->param()['q_word'][0];
- $where = [
- 'nickname'=>new \MongoDB\BSON\Regex($name),
- 'delete_time'=>'',
- ];
- $qcodeGsmc = new QcodeGsmc();
- $sel = $qcodeGsmc
- ->field('nickname')
- ->where($where)
- ->limit(0,10)
- ->select();
- return json(['list' => $sel, 'total' => count($sel)]);
- }
- /**
- * 辅料名称搜索
- */
- public function sel_p()
- {
- $name = $this->request->param()['q_word'][0];
- $where = [
- 'product_name'=>new \MongoDB\BSON\Regex($name),
- 'delete_time'=>'',
- ];
- $qcodeProduct = new QcodeProduct();
- $sel = $qcodeProduct
- ->field('product_name, product_code')
- ->where($where)
- ->limit(0,10)
- ->select();
- foreach ($sel as $k=>$v) {
- $oid = $v['_id']->jsonSerialize();
- $sel[$k]['id'] = $oid['$oid'];
- }
- return json(['list' => $sel, 'total' => count($sel)]);
- }
- /**
- * 辅料编码搜索
- */
- public function sel_c()
- {
- $name = $this->request->param()['q_word'][0];
- $where = [
- 'product_code'=>new \MongoDB\BSON\Regex($name),
- 'delete_time'=>'',
- ];
- $qcodeProduct = new QcodeProduct();
- $sel = $qcodeProduct
- ->field('id, product_name, product_code')
- ->where($where)
- ->order('id','desc')
- ->limit(0,10)
- ->select();
- foreach ($sel as $k=>$v) {
- $oid = $v['_id']->jsonSerialize();
- $sel[$k]['id'] = $oid['$oid'];
- }
- return json(['list' => $sel, 'total' => count($sel)]);
- }
- /**
- * 二维码生成类
- */
- public function qrcode($url)//二维码生成类
- {
- $url=$url;
- $level=3;
- $size=6;
- Vendor('phpqrcode.phpqrcode');//加载生成二维码的核心类
- $errorCorrectionLevel =intval($level) ;//容错级别
- $matrixPointSize = intval($size);//生成图片大小
- //生成二维码图片
- $object = new \QRcode();
- //打开缓冲区
- ob_start();
- $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
- //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。
- $imageString = base64_encode(ob_get_contents());
- //关闭缓冲区
- ob_end_clean();
- //把生成的base64字符串返回给前端
- // $data = array(
- // 'labelcode'=>$url,
- // 'code'=>200,
- // 'data'=>$imageString,
- // 'product_code'=>$url
- // );
- return 'data:image/png;base64,'.$imageString;
- }
- // public function bacch(){
- // $mongo = \think\Db::connect('mongodb');
- //
- // // 表1数据
- // $qcode_product = $mongo->name('qcode_product')->select();
- // // 表2数据
- // $qr_qcode_product_copy = $mongo->name('qr_qcode_product_copy')->select();
- //
- // // 把表2按 product_name 索引
- // $map2 = [];
- // foreach ($qr_qcode_product_copy as $row) {
- // $map2[$row['product_name']] = $row['product_code'];
- // }
- //
- // $same = []; // 相同 product_name 的记录
- // $diff = []; // 不相同的记录
- //
- // // 先处理表1,找出匹配和不匹配
- // foreach ($qcode_product as $row1) {
- // if (isset($map2[$row1['product_name']])) {
- // $same[] = [
- // 'product_code_1' => $row1['product_code'],
- // 'product_code_2' => $map2[$row1['product_name']],
- // 'product_name' => $row1['product_name']
- // ];
- // unset($map2[$row1['product_name']]); // 已匹配,避免后面重复
- // } else {
- // $diff[] = [
- // 'product_code' => $row1['product_code'],
- // 'product_name' => $row1['product_name']
- // ];
- // }
- // }
- //
- // // 剩下 map2 中的就是表2中独有的
- // foreach ($map2 as $name => $code) {
- // $diff[] = [
- // 'product_code' => $code,
- // 'product_name' => $name
- // ];
- // }
- //
- //// // 存到新表
- // $mongo->name('same_products')->insertAll($same);
- // $mongo->name('diff_products')->insertAll($diff);
- // }
- // public function bacch() {
- // $mongo = \think\Db::connect('mongodb');
- //
- // // 获取三个表的数据
- // $table1 = $mongo->name('qcode_product')->select();
- // $table2 = $mongo->name('qr_qcode_product_copy')->select();
- //
- // // 预加载qr_qcode_unit_copy表数据并建立索引
- // $unitData = $mongo->name('qr_qcode_unit_copy')->select();
- // $unitIndex = [];
- // foreach ($unitData as $unit) {
- // $unitIndex[$unit['code']] = [
- // 'main_unit' => $unit['main_unit'] ?? null,
- // 'sec_unit' => $unit['sec_unit'] ?? null,
- // 'proportion' => $unit['proportion'] ?? null
- // ];
- // }
- //
- // // 索引表2数据,包含product_name和temple
- // $table2Index = [];
- // foreach ($table2 as $row) {
- // $table2Index[$row['product_name']] = [
- // 'product_code' => $row['product_code'],
- // 'temple' => $row['temple'] ?? null
- // ];
- // }
- //
- // $differences = [];
- //
- // // 处理表1中的记录
- // foreach ($table1 as $row) {
- // if (!isset($table2Index[$row['product_name']]) &&
- // strpos($row['product_code'], 'AB') !== 0) {
- //
- // // 获取单位信息
- // $unitInfo = $unitIndex[$row['product_code']] ?? [
- // 'main_unit' => null,
- // 'sec_unit' => null,
- // 'proportion' => null
- // ];
- //
- // $differences[] = [
- // 'product_code' => $row['product_code'],
- // 'product_name' => $row['product_name'],
- // 'temple' => null,
- // 'main_unit' => $unitInfo['main_unit'],
- // 'sec_unit' => $unitInfo['sec_unit'],
- // 'proportion' => $unitInfo['proportion']
- // ];
- // }
- // }
- //
- // // 处理表2中的记录
- // foreach ($table2Index as $name => $data) {
- // $existsInTable1 = false;
- // foreach ($table1 as $row) {
- // if ($row['product_name'] === $name) {
- // $existsInTable1 = true;
- // break;
- // }
- // }
- //
- // if (!$existsInTable1 && strpos($data['product_code'], 'AB') !== 0) {
- // // 获取单位信息
- // $unitInfo = $unitIndex[$data['product_code']] ?? [
- // 'main_unit' => null,
- // 'sec_unit' => null,
- // 'proportion' => null
- // ];
- //
- // $differences[] = [
- // 'product_name' => $name,
- // 'product_code' => $data['product_code'],
- // 'temple' => $data['temple'],
- // 'main_unit' => $unitInfo['main_unit'],
- // 'sec_unit' => $unitInfo['sec_unit'],
- // 'proportion' => $unitInfo['proportion'],
- // 'delete_time' => '',
- // 'sync_flag' => 0,
- // 'code' => substr($data['product_code'], 0, 4)
- // ];
- // }
- // }
- //
- // // 获取当前最大的oid_id值
- // $maxOid = $mongo->name('qcode_product')->order('oid_id','desc')->find();
- //
- // $nextOid = $maxOid['oid_id'] ? $maxOid['oid_id'] + 1 : 1; // 如果没有记录则从1开始
- // // 插入数据到qcode_product表
- // foreach ($differences as $diff) {
- // $insertData = [
- // 'oid_id' => $nextOid++,
- // 'product_code' => $diff['product_code'],
- // 'product_name' => $diff['product_name'],
- // 'temple' => $diff['temple'],
- // 'main_unit' => $diff['main_unit'],
- // 'sec_unit' => $diff['sec_unit'],
- // 'proportion' => $diff['proportion'],
- // 'delete_time' => $diff['delete_time'] ?? '',
- // 'sync_flag' => $diff['sync_flag'] ?? 0,
- // 'code' => $diff['code'] ?? substr($diff['product_code'], 0, 4)
- // ];
- // $mongo->name('qcode_product')->insert($insertData);
- // }
- //
- // echo "成功插入 " . count($differences) . " 条记录";
- // }
- }
|