| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Db;
- use think\Session;
- use function EasyWeChat\Kernel\Support\rsa_public_encrypt;
- /**
- * 版本管理
- *
- * @icon fa fa-circle-o
- */
- class Finishedproduct extends Backend
- {
- /**
- * Finishedproduct模型对象
- * @var \app\admin\model\Finishedproduct
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Finishedproduct;
- }
- public function index()
- {
- $params = input('');
- $page = input('get.page', 1);
- $limit = input('get.limit', 10);
- $offset = ($page - 1) * $limit;
- // 获取工单编号列表
- $db3 = Db::connect(config('database.db3'));
- $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']);
- $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh');
- $gdbhList = array_column($gdRows, 'Gd_cpdh');
- if (empty($gdbhList)) {
- $this->assign([
- 'data' => '',
- 'total' => '',
- ]);
- }
- // Mongo 查询
- $mongo = \think\Db::connect('mongodb');
- $where = ['成品编码' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
- // $where = ['成品编码' => ['in', $gdbhList], 'jjcp_smb' => '末 板'];
- if (!empty($params['search'])) {
- $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
- }
- // 查询符合条件的成品数据
- $al_products = $mongo->name('finished_products')
- ->where($where)
- ->order('Sys_rq', 'desc')
- ->select();
- // 查询 texts = 新增产品 的数据
- $xz_products = $mongo->name('finished_products')
- ->where('texts', '新增产品')
- ->order('Sys_rq', 'desc')
- ->select();
- // 合并数据
- $all_products = array_merge($al_products, $xz_products);
- // 分组聚合处理
- $grouped_products = [];
- foreach ($all_products as $item) {
- // 转换日期格式(如果是 d/m/Y H:i:s)
- $date = \DateTime::createFromFormat('d/m/Y H:i:s', $item['Sys_rq']);
- $item['Sys_rq'] = $date ? $date->format('Y-m-d H:i:s') : $item['Sys_rq'];
- $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
- if (!isset($grouped_products[$group_key])) {
- $grouped_products[$group_key] = [
- 'Sys_rq' => $item['Sys_rq'],
- 'order_ddbh' => $item['订单编号'],
- 'gdbh' => $item['jjcp_gdbh'],
- 'cpbm' => $item['成品编码'],
- 'cpmc' => $item['成品名称'],
- 'sl' => 0,
- 'cpdh_list' => [],
- 'cpbm_list' => []
- ];
- }
- $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl'];
- if (!in_array($item['jjcp_cpdh'], $grouped_products[$group_key]['cpdh_list'])) {
- $grouped_products[$group_key]['cpdh_list'][] = $item['jjcp_cpdh'];
- }
- if (!in_array($item['成品编码'], $grouped_products[$group_key]['cpbm_list'])) {
- $grouped_products[$group_key]['cpbm_list'][] = $item['成品编码'];
- }
- }
- // 格式化列表字符串
- foreach ($grouped_products as &$group) {
- $group['cpdh'] = implode(',', $group['cpdh_list']);
- $group['cpbm'] = implode(',', $group['cpbm_list']);
- unset($group['cpdh_list'], $group['cpbm_list']);
- }
- // 聚合结果排序(按时间降序)
- $all_grouped = array_values($grouped_products);
- usort($all_grouped, function ($a, $b) {
- $timeA = strtotime($a['Sys_rq']) ?: 0;
- $timeB = strtotime($b['Sys_rq']) ?: 0;
- return $timeB <=> $timeA;
- });
- // 再分页
- $total = count($all_grouped);
- $paged_grouped = array_slice($all_grouped, $offset, $limit);
- // 查询库存并计算剩余数
- foreach ($paged_grouped as &$prod) {
- $inventory = $mongo->name('inventory_summary')->where([
- 'order_ddbh' => $prod['order_ddbh'],
- 'gdbh' => $prod['gdbh'],
- 'cpmc' => $prod['cpmc'],
- ])->find();
- $total_chu_quantity = isset($inventory['total_chu_quantity']) ? (int)$inventory['total_chu_quantity'] : 0;
- $prod['total_chu_quantity'] = $total_chu_quantity;
- $prod['remaining_quantity'] = $prod['sl'] - $total_chu_quantity;
- }
- unset($prod);
- if (request()->isAjax()) {
- return json([
- 'code' => 1,
- 'data' => $paged_grouped,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit
- ]);
- }
- $this->assign('data', $paged_grouped);
- return $this->fetch();
- }
- // public function index()
- // {
- // $search = input('');
- // $page = input('get.page', 1);
- // $limit = input('get.limit', 10);
- // $mongo = \think\Db::connect('mongodb');
- //
- // // 初始化查询条件
- // $where = [];
- //
- // if (!empty($search['search'])) {
- // // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写
- // $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($search['search'], 'i');
- // }
- //
- // if($where){
- // $data = $mongo->name('finished_products')
- // ->limit($page, $limit)
- // ->where($where)
- // ->order('UniqId','desc')
- // ->where('jjcp_smb','末 板')
- // ->select();
- // }else{
- // $data = $mongo->name('finished_products')
- // ->limit($page, $limit)
- // ->where($where)
- // ->order('UniqId','desc')
- // ->where('jjcp_smb','末 板')
- // ->select();
- // }
- //
- // $filtered = [];
- //
- // foreach ($data as $item) {
- // if (isset($item['jjcp_cpdh'], $item['成品编码']) && $item['jjcp_cpdh'] === $item['成品编码']) {
- // $filtered[] = $item;
- // }
- // }
- //
- // $count = $mongo->name('finished_products')
- // ->where($where)
- // ->order('UniqId','desc')
- // ->where('jjcp_smb','末 板')
- // ->select();
- //
- // if (request()->isAjax()) {
- // return json([
- // 'data' => $filtered,
- // 'total' => count($count),
- // 'page' => $page,
- // 'limit' => $limit
- // ]);
- // }
- //
- // $this->assign('data', $data);
- // return $this->fetch();
- // }
- public function finished()
- {
- // 获取 JSON 格式的请求体
- $jsonData = file_get_contents("php://input");
- // 解析为 PHP 数组
- $data = json_decode($jsonData, true);
- if (!isset($data['data']) || !is_array($data['data'])) {
- return json(['status' => 'error', 'msg' => '无效数据']);
- }
- // 连接 MongoDB 数据库(你在 config/database.php 中的 mongodb 配置名)
- $mongo = \think\Db::connect('mongodb');
- $userinfo = Session::get('admin');
- $insertList = [];
- foreach ($data['data'] as $item) {
- $insertList[] = [
- 'jjcp_num' => '',
- 'jjcp_yjno' => '',
- 'jjcp_cpmc' => '',
- '客户料号' => '',
- '仓库编号' => '',
- '仓库名称' => '',
- 'jjcp_desc' => $item['remark'],
- '入仓类型' => '',
- '机型备注' => '',
- 'Sys_id' => '',
- 'UniqId' => '',
- '成品名称' => $item['cpmc'],
- 'jjcp_cpdh' => $item['cpbm'],
- '成品编码' => $item['cpbm'],
- '订单编号' => $item['order_ddbh'],
- 'jjcp_gdbh' => $item['gdbh'],
- 'jjcp_sl' => intval($item['sl']),
- 'jjcp_smb' => '末 板',
- 'jjcp_dw' => '张',
- 'Mod_rq' => '',
- 'texts' => '新增产品',
- 'company' => $userinfo['company'],
- 'jjcp_sj' => date('Y-m-d H:i:s'),
- 'Sys_rq' => date('Y-m-d H:i:s')
- ];
- }
- // try {
- $mongo->name('Finished_products')->insertAll($insertList);
- $this->success('成功插入');
- // } catch (\Exception $e) {
- // return json(['status' => 'error', 'msg' => '插入失败:' . $e->getMessage()]);
- // }
- }
- //左侧 右侧页面
- public function product_summary()
- {
- // 连接到 MongoDB 数据库
- $mongo = \think\Db::connect('mongodb');
- // 获取库存汇总表的数据
- $list = $mongo->name('inventory_summary')->select();
- // 用于存储分组后的结果
- $grouped_data = [];
- // 遍历查询到的数据并进行分组
- foreach ($list as $item) {
- // 获取创建时间的年份和月份
- $year = date('Y', strtotime($item['created_at'])); // 获取年份
- $month = date('Ym', strtotime($item['created_at'])); // 获取年月(例如 202506)
- // 获取产品名称
- $product_name = $item['cpmc'];
- // 获取工单编号
- $gdbh = $item['gdbh'];
- $order_ddbh = $item['order_ddbh'];
- // 构建多级分组结构
- if (!isset($grouped_data[$year])) {
- $grouped_data[$year] = [];
- }
- if (!isset($grouped_data[$year][$month])) {
- $grouped_data[$year][$month] = [];
- }
- if (!isset($grouped_data[$year][$month][$product_name])) {
- $grouped_data[$year][$month][$product_name] = [];
- }
- // 将工单编号添加到对应的位置,保留 'order_ddbh'
- $grouped_data[$year][$month][$product_name][$order_ddbh] = [
- 'order_ddbh' => $item['order_ddbh'],
- ];
- }
- // 输出结果查看结构
- // echo "<pre>";
- // print_r($grouped_data); // 或者可以使用 var_export($grouped_data, true)
- // echo "</pre>";
- return $this->fetch();
- }
- }
|