Finishedproduct.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. $params = input('');
  27. $page = input('get.page', 1);
  28. $limit = input('get.limit', 10);
  29. $offset = ($page - 1) * $limit;
  30. // 获取工单编号列表
  31. $db3 = Db::connect(config('database.db3'));
  32. $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']);
  33. $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh');
  34. $gdbhList = array_column($gdRows, 'Gd_cpdh');
  35. if (empty($gdbhList)) {
  36. $this->assign([
  37. 'data' => '',
  38. 'total' =>'',
  39. ]);
  40. }
  41. // Mongo 查询
  42. $mongo = \think\Db::connect('mongodb');
  43. $where = ['jjcp_cpdh' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
  44. if (!empty($params['search'])) {
  45. $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
  46. }
  47. // 1. 查询所有符合条件的成品数据(不分页)
  48. $all_products = $mongo->name('finished_products')
  49. ->where($where)
  50. ->order('UniqId', 'desc')
  51. ->select();
  52. // 2. 按订单编号 + 工单编号分组聚合,并处理多个cpdh与成品编码
  53. $grouped_products = [];
  54. foreach ($all_products as $item) {
  55. $originDateStr = $item['Sys_rq']; // 例如:'20/6/2025 16:01:43'
  56. $dt = \DateTime::createFromFormat('d/m/Y H:i:s', $originDateStr);
  57. $standardDate = $dt ? $dt->format('Y-m-d H:i:s') : null;
  58. $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
  59. if (!isset($grouped_products[$group_key])) {
  60. $grouped_products[$group_key] = [
  61. 'order_ddbh' => $item['订单编号'],
  62. 'gdbh' => $item['jjcp_gdbh'],
  63. 'cpbm' => $item['成品编码'],
  64. 'cpmc' => $item['成品名称'],
  65. 'Sys_rq' => $standardDate,
  66. 'sl' => 0, // 库存总数
  67. 'cpdh_list' => [],
  68. 'cpbm_list' => []
  69. ];
  70. }
  71. $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl']; // 累计库存数量
  72. // 收集不同的cpdh和cpbm(成品编码)
  73. if (!in_array($item['jjcp_cpdh'], $grouped_products[$group_key]['cpdh_list'])) {
  74. $grouped_products[$group_key]['cpdh_list'][] = $item['jjcp_cpdh'];
  75. }
  76. if (!in_array($item['成品编码'], $grouped_products[$group_key]['cpbm_list'])) {
  77. $grouped_products[$group_key]['cpbm_list'][] = $item['成品编码'];
  78. }
  79. }
  80. // 格式化成用逗号连接的字符串
  81. foreach ($grouped_products as &$group) {
  82. $group['cpdh'] = implode(',', $group['cpdh_list']);
  83. $group['cpbm'] = implode(',', $group['cpbm_list']);
  84. unset($group['cpdh_list'], $group['cpbm_list']); // 清理临时字段
  85. }
  86. // 3. 将聚合后结果分页(稳定分页)
  87. $all_grouped = array_values($grouped_products);
  88. $total = count($all_grouped);
  89. $paged_grouped = array_slice($all_grouped, $offset, $limit);
  90. // 4. 获取库存出货数据并计算剩余数
  91. foreach ($paged_grouped as &$prod) {
  92. $inventory = $mongo->name('inventory_summary')->where([
  93. 'order_ddbh' => $prod['order_ddbh'],
  94. 'gdbh' => $prod['gdbh'],
  95. 'cpmc' => $prod['cpmc'],
  96. ])->find();
  97. // 出货数量(若无数据则为 0)
  98. $total_chu_quantity = isset($inventory['total_chu_quantity']) ? (int)$inventory['total_chu_quantity'] : 0;
  99. $prod['total_chu_quantity'] = $total_chu_quantity;
  100. // 计算剩余数 = sl - 出货数
  101. $prod['remaining_quantity'] = $prod['sl'] - $total_chu_quantity;
  102. }
  103. unset($prod);
  104. if (request()->isAjax()) {
  105. return json([
  106. 'code' => 1,
  107. 'data' => $paged_grouped,
  108. 'total' => $total,
  109. 'page' => $page,
  110. 'limit' => $limit
  111. ]);
  112. }
  113. $this->assign('data', $paged_grouped);
  114. return $this->fetch();
  115. }
  116. // public function index()
  117. // {
  118. // $search = input('');
  119. // $page = input('get.page', 1);
  120. // $limit = input('get.limit', 10);
  121. // $mongo = \think\Db::connect('mongodb');
  122. //
  123. // // 初始化查询条件
  124. // $where = [];
  125. //
  126. // if (!empty($search['search'])) {
  127. // // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写
  128. // $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($search['search'], 'i');
  129. // }
  130. //
  131. // if($where){
  132. // $data = $mongo->name('finished_products')
  133. // ->limit($page, $limit)
  134. // ->where($where)
  135. // ->order('UniqId','desc')
  136. // ->where('jjcp_smb','末 板')
  137. // ->select();
  138. // }else{
  139. // $data = $mongo->name('finished_products')
  140. // ->limit($page, $limit)
  141. // ->where($where)
  142. // ->order('UniqId','desc')
  143. // ->where('jjcp_smb','末 板')
  144. // ->select();
  145. // }
  146. //
  147. // $filtered = [];
  148. //
  149. // foreach ($data as $item) {
  150. // if (isset($item['jjcp_cpdh'], $item['成品编码']) && $item['jjcp_cpdh'] === $item['成品编码']) {
  151. // $filtered[] = $item;
  152. // }
  153. // }
  154. //
  155. // $count = $mongo->name('finished_products')
  156. // ->where($where)
  157. // ->order('UniqId','desc')
  158. // ->where('jjcp_smb','末 板')
  159. // ->select();
  160. //
  161. // if (request()->isAjax()) {
  162. // return json([
  163. // 'data' => $filtered,
  164. // 'total' => count($count),
  165. // 'page' => $page,
  166. // 'limit' => $limit
  167. // ]);
  168. // }
  169. //
  170. // $this->assign('data', $data);
  171. // return $this->fetch();
  172. // }
  173. public function finished()
  174. {
  175. // 获取 JSON 格式的请求体
  176. $jsonData = file_get_contents("php://input");
  177. // 解析为 PHP 数组
  178. $data = json_decode($jsonData, true);
  179. if (!isset($data['data']) || !is_array($data['data'])) {
  180. return json(['status' => 'error', 'msg' => '无效数据']);
  181. }
  182. // 连接 MongoDB 数据库(你在 config/database.php 中的 mongodb 配置名)
  183. $mongo = \think\Db::connect('mongodb');
  184. $userinfo = Session::get('admin');
  185. $insertList = [];
  186. foreach ($data['data'] as $item) {
  187. $insertList[] = [
  188. '成品名称' => $item['jjcp_cpmc'],
  189. '成品编码' => $item['jjcp_cpbm'],
  190. '订单编号' => $item['order_ddbh'],
  191. 'jjcp_gdbh' => $item['jjcp_gdbh'],
  192. 'jjcp_sl' => intval($item['jjcp_sl']),
  193. 'jjcp_smb' => '末 板',
  194. 'Mod_rq' => '',
  195. 'company' => $userinfo['company'],
  196. 'jjcp_sj' => date('Y-m-d H:i:s'),
  197. 'Sys_rq' => date('Y-m-d H:i:s')
  198. ];
  199. }
  200. try {
  201. $mongo->name('Finished_products')->insertAll($insertList);
  202. return json(['status' => 'success', 'msg' => '成功插入 MongoDB']);
  203. } catch (\Exception $e) {
  204. return json(['status' => 'error', 'msg' => '插入失败:' . $e->getMessage()]);
  205. }
  206. }
  207. //左侧 右侧页面
  208. public function product_summary()
  209. {
  210. // 连接到 MongoDB 数据库
  211. $mongo = \think\Db::connect('mongodb');
  212. // 获取库存汇总表的数据
  213. $list = $mongo->name('inventory_summary')->select();
  214. // 用于存储分组后的结果
  215. $grouped_data = [];
  216. // 遍历查询到的数据并进行分组
  217. foreach ($list as $item) {
  218. // 获取创建时间的年份和月份
  219. $year = date('Y', strtotime($item['created_at'])); // 获取年份
  220. $month = date('Ym', strtotime($item['created_at'])); // 获取年月(例如 202506)
  221. // 获取产品名称
  222. $product_name = $item['cpmc'];
  223. // 获取工单编号
  224. $gdbh = $item['gdbh'];
  225. $order_ddbh = $item['order_ddbh'];
  226. // 构建多级分组结构
  227. if (!isset($grouped_data[$year])) {
  228. $grouped_data[$year] = [];
  229. }
  230. if (!isset($grouped_data[$year][$month])) {
  231. $grouped_data[$year][$month] = [];
  232. }
  233. if (!isset($grouped_data[$year][$month][$product_name])) {
  234. $grouped_data[$year][$month][$product_name] = [];
  235. }
  236. // 将工单编号添加到对应的位置,保留 'order_ddbh'
  237. $grouped_data[$year][$month][$product_name][$order_ddbh] = [
  238. 'order_ddbh' => $item['order_ddbh'],
  239. ];
  240. }
  241. // 输出结果查看结构
  242. // echo "<pre>";
  243. // print_r($grouped_data); // 或者可以使用 var_export($grouped_data, true)
  244. // echo "</pre>";
  245. return $this->fetch();
  246. }
  247. }