User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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($test2)){
  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. $limit = $this->request->get('limit', 20);
  155. $offset = $this->request->get('offset', 0);
  156. $where = [];
  157. if ($order_ddbh) $where['订单编号'] = $order_ddbh;
  158. if ($gdbh) $where['jjcp_gdbh'] = $gdbh;
  159. if ($cpmc) $where['成品名称'] = $cpmc;
  160. // 获取工单编号列表
  161. $db3 = Db::connect(config('database.db3'));
  162. $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ? AND `Gd_gdbh` = ?", ['J0031', $gdbh]);
  163. $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh');
  164. $gdbhList = array_column($gdRows, 'Gd_cpdh');
  165. if (empty($gdbhList)) {
  166. $this->assign([
  167. 'list' => '',
  168. 'total' =>'',
  169. 'total_sl' => '',
  170. ]);
  171. }
  172. // Mongo 查询
  173. $mongo = \think\Db::connect('mongodb');
  174. $where = ['jjcp_cpdh' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板'];
  175. $total = $mongo->name('finished_products')->where($where)->count();
  176. // 查询原始数据
  177. $rawList = $mongo->name('finished_products')
  178. ->field('订单编号, 成品名称, jjcp_gdbh, jjcp_sl, jjcp_sj')
  179. ->where($where)
  180. ->order('jjcp_sj','desc')
  181. ->limit($offset, $limit)
  182. ->select();
  183. // 转换字段名并格式化时间
  184. $list = [];
  185. $total_sl = 0; // 初始化合计值
  186. foreach ($rawList as $item) {
  187. // 处理时间格式转换
  188. $originalDate = $item['jjcp_sj'] ?? null;
  189. $formattedDate = null;
  190. if ($originalDate) {
  191. try {
  192. $date = \DateTime::createFromFormat('d/m/Y H:i:s', $originalDate);
  193. if ($date) {
  194. $formattedDate = $date->format('Y-m-d H:i:s');
  195. } else {
  196. $date = new \DateTime($originalDate);
  197. $formattedDate = $date->format('Y-m-d H:i:s');
  198. }
  199. } catch (Exception $e) {
  200. $formattedDate = $originalDate;
  201. }
  202. }
  203. $sl = $item['jjcp_sl'] ?? 0;
  204. $total_sl += (int)$sl; // 累加数量
  205. $list[] = [
  206. 'order_ddbh' => $item['订单编号'] ?? null,
  207. 'cpmc' => $item['成品名称'] ?? null,
  208. 'gdbh' => $item['jjcp_gdbh'] ?? null,
  209. 'sl' => $sl,
  210. 'sj' => $formattedDate
  211. ];
  212. }
  213. $this->assign([
  214. 'list' => $list,
  215. 'total' => $total,
  216. 'total_sl' => $total_sl
  217. ]);
  218. return $this->fetch();
  219. }
  220. public function chuku()
  221. {
  222. $order_ddbh = $this->request->get('order_ddbh', '');
  223. $gdbh = $this->request->get('gdbh', '');
  224. $cpmc = $this->request->get('cpmc', '');
  225. $limit = $this->request->get('limit', 20);
  226. $offset = $this->request->get('offset', 0);
  227. $where = [];
  228. if ($order_ddbh) $where['order_ddbh'] = $order_ddbh;
  229. if ($gdbh) $where['gdbh'] = $gdbh;
  230. if ($cpmc) $where['cpmc'] = $cpmc;
  231. $mongo = \think\Db::connect('mongodb');
  232. $total = $mongo->name('inventory_records')->where($where)->count();
  233. $list = $mongo->name('inventory_records')
  234. ->where($where)
  235. ->limit($offset, $limit)
  236. ->select();
  237. // 计算出库数量合计
  238. $total_sl = 0;
  239. foreach ($list as &$item) {
  240. $sl = $item['total_chu_quantity'] ?? 0;
  241. $total_sl += (int)$sl;
  242. $item['total_chu_quantity'] = $sl; // 确保sl字段存在
  243. }
  244. $this->assign([
  245. 'list' => $list,
  246. 'total' => $total,
  247. 'total_sl' => $total_sl // 传递合计值到视图
  248. ]);
  249. return $this->fetch();
  250. }
  251. /**
  252. * 添加
  253. */
  254. public function add()
  255. {
  256. if ($this->request->isPost()) {
  257. $this->token();
  258. }
  259. return parent::add();
  260. }
  261. /**
  262. * 编辑
  263. */
  264. public function edit($ids = null)
  265. {
  266. if ($this->request->isPost()) {
  267. $this->token();
  268. }
  269. $row = $this->model->get($ids);
  270. $this->modelValidate = true;
  271. if (!$row) {
  272. $this->error(__('No Results were found'));
  273. }
  274. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  275. return parent::edit($ids);
  276. }
  277. /**
  278. * 删除
  279. */
  280. public function del($ids = "")
  281. {
  282. if (!$this->request->isPost()) {
  283. $this->error(__("Invalid parameters"));
  284. }
  285. $ids = $ids ? $ids : $this->request->post("ids");
  286. $row = $this->model->get($ids);
  287. $this->modelValidate = true;
  288. if (!$row) {
  289. $this->error(__('No Results were found'));
  290. }
  291. Auth::instance()->delete($row['id']);
  292. $this->success();
  293. }
  294. }