Finishedproduct.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Session;
  6. use function EasyWeChat\Kernel\Support\rsa_public_encrypt;
  7. /**
  8. * 版本管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Finishedproduct extends Backend
  13. {
  14. /**
  15. * Finishedproduct模型对象
  16. * @var \app\admin\model\Finishedproduct
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Finishedproduct;
  23. }
  24. public function index()
  25. {
  26. $search = input('');
  27. $page = input('get.page', 1);
  28. $limit = input('get.limit', 10);
  29. $mongo = \think\Db::connect('mongodb');
  30. // 初始化查询条件
  31. $where = [];
  32. if (!empty($search['search'])) {
  33. // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写
  34. $where['jjcp_cpdh|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($search['search'], 'i');
  35. }
  36. if($where){
  37. $data = $mongo->name('finished_products')
  38. ->limit($page, $limit)
  39. ->where($where)
  40. ->order('UniqId','desc')
  41. ->where('jjcp_smb','末 板')
  42. ->select();
  43. }else{
  44. $data = $mongo->name('finished_products')
  45. ->limit($page, $limit)
  46. ->where($where)
  47. ->order('UniqId','desc')
  48. ->where('jjcp_smb','末 板')
  49. ->select();
  50. }
  51. $filtered = [];
  52. foreach ($data as $item) {
  53. if (isset($item['jjcp_cpdh'], $item['成品编码']) && $item['jjcp_cpdh'] === $item['成品编码']) {
  54. $filtered[] = $item;
  55. }
  56. }
  57. $count = $mongo->name('finished_products')
  58. ->where($where)
  59. ->order('UniqId','desc')
  60. ->where('jjcp_smb','末 板')
  61. ->select();
  62. if (request()->isAjax()) {
  63. return json([
  64. 'data' => $filtered,
  65. 'total' => count($count),
  66. 'page' => $page,
  67. 'limit' => $limit
  68. ]);
  69. }
  70. $this->assign('data', $data);
  71. return $this->fetch();
  72. }
  73. public function finished()
  74. {
  75. // 获取 JSON 格式的请求体
  76. $jsonData = file_get_contents("php://input");
  77. // 解析为 PHP 数组
  78. $data = json_decode($jsonData, true);
  79. if (!isset($data['data']) || !is_array($data['data'])) {
  80. return json(['status' => 'error', 'msg' => '无效数据']);
  81. }
  82. // 连接 MongoDB 数据库(你在 config/database.php 中的 mongodb 配置名)
  83. $mongo = \think\Db::connect('mongodb');
  84. $userinfo = Session::get('admin');
  85. $insertList = [];
  86. foreach ($data['data'] as $item) {
  87. $insertList[] = [
  88. 'jjcp_cpmc' => $item['jjcp_cpmc'],
  89. 'jjcp_cpdh' => $item['jjcp_cpdh'],
  90. 'order_ddbh' => $item['order_ddbh'],
  91. 'jjcp_gdbh' => $item['jjcp_gdbh'],
  92. 'jjcp_sl' => intval($item['jjcp_sl']),
  93. 'jjcp_smb' => '已入库',
  94. 'company' => $userinfo['company'],
  95. 'created_at' => date('Y-m-d H:i:s')
  96. ];
  97. }
  98. try {
  99. $mongo->name('Finished_products')->insertAll($insertList);
  100. return json(['status' => 'success', 'msg' => '成功插入 MongoDB']);
  101. } catch (\Exception $e) {
  102. return json(['status' => 'error', 'msg' => '插入失败:' . $e->getMessage()]);
  103. }
  104. }
  105. //左侧 右侧页面
  106. public function product_summary()
  107. {
  108. // 连接到 MongoDB 数据库
  109. $mongo = \think\Db::connect('mongodb');
  110. // 获取库存汇总表的数据
  111. $list = $mongo->name('inventory_summary')->select();
  112. // 用于存储分组后的结果
  113. $grouped_data = [];
  114. // 遍历查询到的数据并进行分组
  115. foreach ($list as $item) {
  116. // 获取创建时间的年份和月份
  117. $year = date('Y', strtotime($item['created_at'])); // 获取年份
  118. $month = date('Ym', strtotime($item['created_at'])); // 获取年月(例如 202506)
  119. // 获取产品名称
  120. $product_name = $item['cpmc'];
  121. // 获取工单编号
  122. $gdbh = $item['gdbh'];
  123. $order_ddbh = $item['order_ddbh'];
  124. // 构建多级分组结构
  125. if (!isset($grouped_data[$year])) {
  126. $grouped_data[$year] = [];
  127. }
  128. if (!isset($grouped_data[$year][$month])) {
  129. $grouped_data[$year][$month] = [];
  130. }
  131. if (!isset($grouped_data[$year][$month][$product_name])) {
  132. $grouped_data[$year][$month][$product_name] = [];
  133. }
  134. // 将工单编号添加到对应的位置,保留 'order_ddbh'
  135. $grouped_data[$year][$month][$product_name][$order_ddbh] = [
  136. 'order_ddbh' => $item['order_ddbh'],
  137. ];
  138. }
  139. // 输出结果查看结构
  140. // echo "<pre>";
  141. // print_r($grouped_data); // 或者可以使用 var_export($grouped_data, true)
  142. // echo "</pre>";
  143. return $this->fetch();
  144. }
  145. }