User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\library\Auth;
  5. use think\Db;
  6. /**
  7. * 会员管理
  8. *
  9. * @icon fa fa-user
  10. */
  11. class User extends Backend
  12. {
  13. protected $relationSearch = true;
  14. protected $searchFields = 'id,username,nickname';
  15. /**
  16. * @var \app\admin\model\User
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\User;
  23. }
  24. /**
  25. * 查看
  26. */
  27. public function index()
  28. {
  29. $mongo = \think\Db::connect('mongodb');
  30. $this->model = $mongo->name('inventory_summary');
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags', 'trim']);
  33. if ($this->request->isAjax()) {
  34. //如果发送的来源是Selectpage,则转发到Selectpage
  35. if ($this->request->request('keyField')) {
  36. return $this->selectpage();
  37. }
  38. //list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  39. // $this->model = $this->model;
  40. //->where($where);
  41. $yearMonth = $this->request->param("yearMonth");
  42. if(!empty($yearMonth)){
  43. // 获取月份的第一天 00:00:00
  44. $startTime = $yearMonth . '-01 00:00:00';
  45. // 获取下个月的第一天,然后减去1秒,得到本月最后一秒
  46. $endTime = date('Y-m-t 23:59:59', strtotime($yearMonth . '-01'));
  47. $this->model = $this->model->whereBetween('created_at',[$startTime,$endTime] );
  48. }
  49. $test2 = $this->request->param("test2");
  50. if(!empty($test2)){
  51. $this->model = $this->model->where('cpmc',$test2 );
  52. }
  53. $test3 = $this->request->param("test3");
  54. if(!empty($test3)){
  55. $this->model = $this->model->where('order_ddbh',$test3 );
  56. }
  57. if(empty($yearMonth)){
  58. $list = [];
  59. $result = array("total" => count($list), "rows" => $list);
  60. return json($result);
  61. }
  62. $list = $this->model
  63. ->select();
  64. $result = array("total" => count($list), "rows" => $list);
  65. return json($result);
  66. }
  67. $tree = $this->getTreeDataOptimized();
  68. $this->assign("tree",$tree);
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 方法二:分级查询(推荐)
  73. */
  74. public function getTreeDataOptimized()
  75. {
  76. $mongo = \think\Db::connect('mongodb');
  77. $this->model = $mongo->name('inventory_summary');
  78. // 1. 查询所有一级分类
  79. $level1 = $this->model
  80. ->field('created_at')
  81. ->order('created_at DESC')
  82. ->select();
  83. $tree = [];
  84. foreach ($level1 as $k =>$v){
  85. $yearMonth = substr($v['created_at'], 0, 7); // 2025-06-20 -> 2025-06
  86. $level1_news[$yearMonth]=$v;
  87. }
  88. $level1 = $level1_news;
  89. unset($level1_news);
  90. foreach ($level1 as $item1) {
  91. $yearMonth = substr($item1['created_at'], 0, 7); // 2025-06-20 -> 2025-06
  92. $node1 = [
  93. 'id' => $yearMonth,
  94. 'name' => $yearMonth,
  95. 'level' => 1,
  96. 'children' => []
  97. ];
  98. // 获取月份的第一天 00:00:00
  99. $startTime = $yearMonth . '-01 00:00:00';
  100. // 获取下个月的第一天,然后减去1秒,得到本月最后一秒
  101. $endTime = date('Y-m-t 23:59:59', strtotime($yearMonth . '-01'));
  102. // 2. 查询当前一级下的所有二级分类
  103. $level2 = $this->model
  104. ->field('cpmc')
  105. ->whereBetween('created_at',[$startTime,$endTime] )
  106. ->where('cpmc', '<>', '')
  107. ->order('cpmc asc')
  108. ->select();
  109. foreach ($level2 as $k=>$v){
  110. $level2_new[$v["cpmc"]] = $v;
  111. }
  112. $level2 = $level2_new;
  113. unset($level2_new);
  114. foreach ($level2 as $item2) {
  115. $node2 = [
  116. 'id' => $yearMonth . '_' . $item2['cpmc'],
  117. 'name' => $item2['cpmc'],
  118. 'level' => 2,
  119. 'parent' => $yearMonth,
  120. 'children' => []
  121. ];
  122. // 3. 查询当前二级下的所有三级分类
  123. $level3 = $this->model
  124. // ->field('*')
  125. ->whereBetween('created_at',[$startTime,$endTime] )
  126. ->where('cpmc', $item2['cpmc'])
  127. ->where('order_ddbh', '<>', '')
  128. ->order('order_ddbh asc')
  129. ->select();
  130. foreach ($level3 as $k=>$v){
  131. $level3_news[$v["order_ddbh"]] = $v;
  132. }
  133. $level3 = $level3_news;
  134. unset($level3_news);
  135. foreach ($level3 as $item3) {
  136. $node2['children'][] = [
  137. 'id' => $yearMonth . '_' . $item2['cpmc'] . '_' . $item3['order_ddbh'],
  138. 'name' => $item3['order_ddbh'],
  139. 'level' => 3,
  140. 'parent' => $yearMonth . '_' . $item2['cpmc']
  141. ];
  142. }
  143. $node1['children'][] = $node2;
  144. }
  145. $tree[$yearMonth] = $node1;
  146. }
  147. return $tree;
  148. }
  149. public function ruku()
  150. {
  151. $order_ddbh = $this->request->get('order_ddbh', '');
  152. $gdbh = $this->request->get('gdbh', '');
  153. $cpmc = $this->request->get('cpmc', '');
  154. $cpbm = $this->request->get('cpbm', '');
  155. $limit = $this->request->get('limit', 20);
  156. $offset = $this->request->get('offset', 0);
  157. $where = [];
  158. if ($order_ddbh) $where['订单编号'] = $order_ddbh;
  159. if ($gdbh) $where['jjcp_gdbh'] = $gdbh;
  160. if ($cpmc) $where['成品名称'] = $cpmc;
  161. if ($cpmc) $where['成品编码'] = $cpbm;
  162. // 获取工单编号列表
  163. $db3 = Db::connect(config('database.db3'));
  164. $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ? AND `Gd_gdbh` = ?", ['J0031', $gdbh]);
  165. $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh');
  166. $gdbhList = array_column($gdRows, 'Gd_cpdh');
  167. if (empty($gdbhList)) {
  168. $this->assign([
  169. 'list' => '',
  170. 'total' =>'',
  171. 'total_sl' => '',
  172. ]);
  173. }
  174. // Mongo 查询
  175. $mongo = \think\Db::connect('mongodb');
  176. $where = ['jjcp_cpdh' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
  177. $total = $mongo->name('finished_products')->where($where)->count();
  178. // 查询原始数据
  179. $rawList = $mongo->name('finished_products')
  180. ->field('订单编号, 成品编码,成品名称, jjcp_gdbh, jjcp_sl, jjcp_sj')
  181. ->where($where)
  182. ->order('jjcp_sj','desc')
  183. ->limit($offset, $limit)
  184. ->select();
  185. // 转换字段名并格式化时间
  186. $list = [];
  187. $total_sl = 0; // 初始化合计值
  188. foreach ($rawList as $item) {
  189. // 处理时间格式转换
  190. $originalDate = $item['jjcp_sj'] ?? null;
  191. $formattedDate = null;
  192. if ($originalDate) {
  193. try {
  194. $date = \DateTime::createFromFormat('d/m/Y H:i:s', $originalDate);
  195. if ($date) {
  196. $formattedDate = $date->format('Y-m-d H:i:s');
  197. } else {
  198. $date = new \DateTime($originalDate);
  199. $formattedDate = $date->format('Y-m-d H:i:s');
  200. }
  201. } catch (Exception $e) {
  202. $formattedDate = $originalDate;
  203. }
  204. }
  205. $sl = $item['jjcp_sl'] ?? 0;
  206. $total_sl += (int)$sl; // 累加数量
  207. $list[] = [
  208. 'order_ddbh' => $item['订单编号'] ?? null,
  209. 'cpbm' => $item['成品编码'] ?? null,
  210. 'cpmc' => $item['成品名称'] ?? null,
  211. 'gdbh' => $item['jjcp_gdbh'] ?? null,
  212. 'sl' => $sl,
  213. 'sj' => $formattedDate
  214. ];
  215. }
  216. $this->assign([
  217. 'list' => $list,
  218. 'total' => $total,
  219. 'total_sl' => $total_sl
  220. ]);
  221. return $this->fetch();
  222. }
  223. public function chuku()
  224. {
  225. $order_ddbh = $this->request->get('order_ddbh', '');
  226. $gdbh = $this->request->get('gdbh', '');
  227. $cpmc = $this->request->get('cpmc', '');
  228. $cpbm = $this->request->get('cpbm', '');
  229. $limit = $this->request->get('limit', 20);
  230. $offset = $this->request->get('offset', 0);
  231. $where = [];
  232. if ($order_ddbh) $where['order_ddbh'] = $order_ddbh;
  233. if ($gdbh) $where['gdbh'] = $gdbh;
  234. if ($cpmc) $where['cpmc'] = $cpmc;
  235. if ($cpmc) $where['cpbm'] = $cpbm;
  236. $mongo = \think\Db::connect('mongodb');
  237. $total = $mongo->name('inventory_records')->where($where)->count();
  238. $list = $mongo->name('inventory_records')
  239. ->where($where)
  240. ->limit($offset, $limit)
  241. ->select();
  242. // 计算出库数量合计
  243. $total_sl = 0;
  244. foreach ($list as &$item) {
  245. $sl = $item['total_chu_quantity'] ?? 0;
  246. $total_sl += (int)$sl;
  247. $item['total_chu_quantity'] = $sl; // 确保sl字段存在
  248. }
  249. $this->assign([
  250. 'list' => $list,
  251. 'total' => $total,
  252. 'total_sl' => $total_sl // 传递合计值到视图
  253. ]);
  254. return $this->fetch();
  255. }
  256. /**
  257. * 添加
  258. */
  259. public function add()
  260. {
  261. if ($this->request->isPost()) {
  262. $this->token();
  263. }
  264. return parent::add();
  265. }
  266. /**
  267. * 编辑
  268. */
  269. public function edit($ids = null)
  270. {
  271. if ($this->request->isPost()) {
  272. $this->token();
  273. }
  274. $row = $this->model->get($ids);
  275. $this->modelValidate = true;
  276. if (!$row) {
  277. $this->error(__('No Results were found'));
  278. }
  279. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  280. return parent::edit($ids);
  281. }
  282. /**
  283. * 删除
  284. */
  285. public function del($ids = "")
  286. {
  287. if (!$this->request->isPost()) {
  288. $this->error(__("Invalid parameters"));
  289. }
  290. $ids = $ids ? $ids : $this->request->post("ids");
  291. $row = $this->model->get($ids);
  292. $this->modelValidate = true;
  293. if (!$row) {
  294. $this->error(__('No Results were found'));
  295. }
  296. Auth::instance()->delete($row['id']);
  297. $this->success();
  298. }
  299. }