Finishedproduct.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. public function index()
  31. {
  32. $search = input('get.search', '');
  33. $page = input('get.page', 1);
  34. $limit = input('get.limit', 10);
  35. $db3 = \db()->connect(config('database.db3'));
  36. $where = [];
  37. if (!empty($search)) {
  38. $where = function($query) use ($search) {
  39. $query->where('jjcp_gdbh', 'like', "%{$search}%")
  40. ->whereOr('jjcp_cpdh', 'like', "%{$search}%")
  41. ->whereOr('jjcp_cpmc', 'like', "%{$search}%");
  42. };
  43. }
  44. $data =$db3->name('成品入仓')
  45. ->where($where)
  46. ->field('
  47. jjcp_gdbh,
  48. jjcp_cpdh,
  49. jjcp_cpmc,
  50. jjcp_sl,
  51. UniqId,
  52. 订单编号 AS order_ddbh,
  53. jjcp_smb
  54. ')
  55. ->order('Sys_rq desc')
  56. ->where('jjcp_smb', '=', '末 板')
  57. ->whereNull('Mod_rq')
  58. ->limit(($page - 1) * $limit, $limit)
  59. ->select();
  60. // 查询总数(必须使用和分页前相同条件)
  61. $total = $db3->name('成品入仓')->where($where)->count();
  62. // 字段值格式化
  63. foreach ($data as &$row) {
  64. $row['jjcp_smb'] = ($row['jjcp_smb'] === '末 板') ? '完工入库' : '未完工入库';
  65. }
  66. // 返回 JSON 或渲染视图
  67. if (request()->isAjax()) {
  68. return json([
  69. 'data' => $data,
  70. 'total' => $total,
  71. 'page' => $page,
  72. 'limit' => $limit
  73. ]);
  74. }
  75. $this->assign('data', $data);
  76. return $this->fetch();
  77. }
  78. public function finished()
  79. {
  80. // 获取 JSON 格式的请求体
  81. $jsonData = file_get_contents("php://input");
  82. // 解析为 PHP 数组
  83. $data = json_decode($jsonData, true);
  84. if (!isset($data['data']) || !is_array($data['data'])) {
  85. return json(['status' => 'error', 'msg' => '无效数据']);
  86. }
  87. // 连接 MongoDB 数据库(你在 config/database.php 中的 mongodb 配置名)
  88. $mongo = \think\Db::connect('mongodb');
  89. $userinfo = Session::get('admin');
  90. $insertList = [];
  91. foreach ($data['data'] as $item) {
  92. $insertList[] = [
  93. 'jjcp_cpmc' => $item['jjcp_cpmc'],
  94. 'jjcp_cpdh' => $item['jjcp_cpdh'],
  95. 'order_ddbh' => $item['order_ddbh'],
  96. 'jjcp_gdbh' => $item['jjcp_gdbh'],
  97. 'jjcp_sl' => intval($item['jjcp_sl']),
  98. 'jjcp_smb' => '已入库',
  99. 'company' => $userinfo['company'],
  100. 'created_at' => date('Y-m-d H:i:s')
  101. ];
  102. }
  103. try {
  104. $mongo->name('Finished_products')->insertAll($insertList);
  105. return json(['status' => 'success', 'msg' => '成功插入 MongoDB']);
  106. } catch (\Exception $e) {
  107. return json(['status' => 'error', 'msg' => '插入失败:' . $e->getMessage()]);
  108. }
  109. }
  110. }