Finishedproduct.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 = ['成品编码' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
  44. // $where = ['成品编码' => ['in', $gdbhList], 'jjcp_smb' => '末 板'];
  45. if (!empty($params['search'])) {
  46. $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i');
  47. }
  48. // 查询符合条件的成品数据
  49. $al_products = $mongo->name('finished_products')
  50. ->where($where)
  51. ->order('Sys_rq', 'desc')
  52. ->select();
  53. // 查询 texts = 新增产品 的数据
  54. $xz_products = $mongo->name('finished_products')
  55. ->where('texts', '新增产品')
  56. ->order('Sys_rq', 'desc')
  57. ->select();
  58. // 合并数据
  59. $all_products = array_merge($al_products, $xz_products);
  60. // 分组聚合处理
  61. $grouped_products = [];
  62. foreach ($all_products as $item) {
  63. // 转换日期格式(如果是 d/m/Y H:i:s)
  64. $date = \DateTime::createFromFormat('d/m/Y H:i:s', $item['Sys_rq']);
  65. $item['Sys_rq'] = $date ? $date->format('Y-m-d H:i:s') : $item['Sys_rq'];
  66. $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh'];
  67. if (!isset($grouped_products[$group_key])) {
  68. $grouped_products[$group_key] = [
  69. 'Sys_rq' => $item['Sys_rq'],
  70. 'order_ddbh' => $item['订单编号'],
  71. 'gdbh' => $item['jjcp_gdbh'],
  72. 'cpbm' => $item['成品编码'],
  73. 'cpmc' => $item['成品名称'],
  74. 'sl' => 0,
  75. 'cpdh_list' => [],
  76. 'cpbm_list' => []
  77. ];
  78. }
  79. $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl'];
  80. if (!in_array($item['jjcp_cpdh'], $grouped_products[$group_key]['cpdh_list'])) {
  81. $grouped_products[$group_key]['cpdh_list'][] = $item['jjcp_cpdh'];
  82. }
  83. if (!in_array($item['成品编码'], $grouped_products[$group_key]['cpbm_list'])) {
  84. $grouped_products[$group_key]['cpbm_list'][] = $item['成品编码'];
  85. }
  86. }
  87. // 格式化列表字符串
  88. foreach ($grouped_products as &$group) {
  89. $group['cpdh'] = implode(',', $group['cpdh_list']);
  90. $group['cpbm'] = implode(',', $group['cpbm_list']);
  91. unset($group['cpdh_list'], $group['cpbm_list']);
  92. }
  93. // 聚合结果排序(按时间降序)
  94. $all_grouped = array_values($grouped_products);
  95. usort($all_grouped, function ($a, $b) {
  96. $timeA = strtotime($a['Sys_rq']) ?: 0;
  97. $timeB = strtotime($b['Sys_rq']) ?: 0;
  98. return $timeB <=> $timeA;
  99. });
  100. // 再分页
  101. $total = count($all_grouped);
  102. $paged_grouped = array_slice($all_grouped, $offset, $limit);
  103. // 查询库存并计算剩余数
  104. foreach ($paged_grouped as &$prod) {
  105. $inventory = $mongo->name('inventory_summary')->where([
  106. 'order_ddbh' => $prod['order_ddbh'],
  107. 'gdbh' => $prod['gdbh'],
  108. 'cpmc' => $prod['cpmc'],
  109. ])->find();
  110. $total_chu_quantity = isset($inventory['total_chu_quantity']) ? (int)$inventory['total_chu_quantity'] : 0;
  111. $prod['total_chu_quantity'] = $total_chu_quantity;
  112. $prod['remaining_quantity'] = $prod['sl'] - $total_chu_quantity;
  113. }
  114. unset($prod);
  115. if (request()->isAjax()) {
  116. return json([
  117. 'code' => 1,
  118. 'data' => $paged_grouped,
  119. 'total' => $total,
  120. 'page' => $page,
  121. 'limit' => $limit
  122. ]);
  123. }
  124. $this->assign('data', $paged_grouped);
  125. return $this->fetch();
  126. }
  127. // public function index()
  128. // {
  129. // $search = input('');
  130. // $page = input('get.page', 1);
  131. // $limit = input('get.limit', 10);
  132. // $mongo = \think\Db::connect('mongodb');
  133. //
  134. // // 初始化查询条件
  135. // $where = [];
  136. //
  137. // if (!empty($search['search'])) {
  138. // // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写
  139. // $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($search['search'], 'i');
  140. // }
  141. //
  142. // if($where){
  143. // $data = $mongo->name('finished_products')
  144. // ->limit($page, $limit)
  145. // ->where($where)
  146. // ->order('UniqId','desc')
  147. // ->where('jjcp_smb','末 板')
  148. // ->select();
  149. // }else{
  150. // $data = $mongo->name('finished_products')
  151. // ->limit($page, $limit)
  152. // ->where($where)
  153. // ->order('UniqId','desc')
  154. // ->where('jjcp_smb','末 板')
  155. // ->select();
  156. // }
  157. //
  158. // $filtered = [];
  159. //
  160. // foreach ($data as $item) {
  161. // if (isset($item['jjcp_cpdh'], $item['成品编码']) && $item['jjcp_cpdh'] === $item['成品编码']) {
  162. // $filtered[] = $item;
  163. // }
  164. // }
  165. //
  166. // $count = $mongo->name('finished_products')
  167. // ->where($where)
  168. // ->order('UniqId','desc')
  169. // ->where('jjcp_smb','末 板')
  170. // ->select();
  171. //
  172. // if (request()->isAjax()) {
  173. // return json([
  174. // 'data' => $filtered,
  175. // 'total' => count($count),
  176. // 'page' => $page,
  177. // 'limit' => $limit
  178. // ]);
  179. // }
  180. //
  181. // $this->assign('data', $data);
  182. // return $this->fetch();
  183. // }
  184. public function finished()
  185. {
  186. // 获取 JSON 格式的请求体
  187. $jsonData = file_get_contents("php://input");
  188. // 解析为 PHP 数组
  189. $data = json_decode($jsonData, true);
  190. if (!isset($data['data']) || !is_array($data['data'])) {
  191. return json(['status' => 'error', 'msg' => '无效数据']);
  192. }
  193. // 连接 MongoDB 数据库(你在 config/database.php 中的 mongodb 配置名)
  194. $mongo = \think\Db::connect('mongodb');
  195. $userinfo = Session::get('admin');
  196. $insertList = [];
  197. foreach ($data['data'] as $item) {
  198. $insertList[] = [
  199. 'jjcp_num' => '',
  200. 'jjcp_yjno' => '',
  201. 'jjcp_cpmc' => '',
  202. '客户料号' => '',
  203. '仓库编号' => '',
  204. '仓库名称' => '',
  205. 'jjcp_desc' => $item['remark'],
  206. '入仓类型' => '',
  207. '机型备注' => '',
  208. 'Sys_id' => '',
  209. 'UniqId' => '',
  210. '成品名称' => $item['cpmc'],
  211. 'jjcp_cpdh' => $item['cpbm'],
  212. '成品编码' => $item['cpbm'],
  213. '订单编号' => $item['order_ddbh'],
  214. 'jjcp_gdbh' => $item['gdbh'],
  215. 'jjcp_sl' => intval($item['sl']),
  216. 'jjcp_smb' => '末 板',
  217. 'jjcp_dw' => '张',
  218. 'Mod_rq' => '',
  219. 'texts' => '新增产品',
  220. 'company' => $userinfo['company'],
  221. 'jjcp_sj' => date('Y-m-d H:i:s'),
  222. 'Sys_rq' => date('Y-m-d H:i:s')
  223. ];
  224. }
  225. // try {
  226. $mongo->name('Finished_products')->insertAll($insertList);
  227. $this->success('成功插入');
  228. // } catch (\Exception $e) {
  229. // return json(['status' => 'error', 'msg' => '插入失败:' . $e->getMessage()]);
  230. // }
  231. }
  232. //左侧 右侧页面
  233. public function product_summary()
  234. {
  235. // 连接到 MongoDB 数据库
  236. $mongo = \think\Db::connect('mongodb');
  237. // 获取库存汇总表的数据
  238. $list = $mongo->name('inventory_summary')->select();
  239. // 用于存储分组后的结果
  240. $grouped_data = [];
  241. // 遍历查询到的数据并进行分组
  242. foreach ($list as $item) {
  243. // 获取创建时间的年份和月份
  244. $year = date('Y', strtotime($item['created_at'])); // 获取年份
  245. $month = date('Ym', strtotime($item['created_at'])); // 获取年月(例如 202506)
  246. // 获取产品名称
  247. $product_name = $item['cpmc'];
  248. // 获取工单编号
  249. $gdbh = $item['gdbh'];
  250. $order_ddbh = $item['order_ddbh'];
  251. // 构建多级分组结构
  252. if (!isset($grouped_data[$year])) {
  253. $grouped_data[$year] = [];
  254. }
  255. if (!isset($grouped_data[$year][$month])) {
  256. $grouped_data[$year][$month] = [];
  257. }
  258. if (!isset($grouped_data[$year][$month][$product_name])) {
  259. $grouped_data[$year][$month][$product_name] = [];
  260. }
  261. // 将工单编号添加到对应的位置,保留 'order_ddbh'
  262. $grouped_data[$year][$month][$product_name][$order_ddbh] = [
  263. 'order_ddbh' => $item['order_ddbh'],
  264. ];
  265. }
  266. // 输出结果查看结构
  267. // echo "<pre>";
  268. // print_r($grouped_data); // 或者可以使用 var_export($grouped_data, true)
  269. // echo "</pre>";
  270. return $this->fetch();
  271. }
  272. }