| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?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()
- {
- $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_cpdh|成品名称|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_cpmc' => $item['jjcp_cpmc'],
- 'jjcp_cpdh' => $item['jjcp_cpdh'],
- 'order_ddbh' => $item['order_ddbh'],
- 'jjcp_gdbh' => $item['jjcp_gdbh'],
- 'jjcp_sl' => intval($item['jjcp_sl']),
- 'jjcp_smb' => '已入库',
- 'company' => $userinfo['company'],
- 'created_at' => date('Y-m-d H:i:s')
- ];
- }
- try {
- $mongo->name('Finished_products')->insertAll($insertList);
- return json(['status' => 'success', 'msg' => '成功插入 MongoDB']);
- } 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();
- }
- }
|