懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 package com.jcdm.common.core.controller;
2
3 import java.beans.PropertyEditorSupport;
4 import java.util.Date;
5 import java.util.List;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.springframework.web.bind.WebDataBinder;
9 import org.springframework.web.bind.annotation.InitBinder;
10 import com.github.pagehelper.PageHelper;
11 import com.github.pagehelper.PageInfo;
12 import com.jcdm.common.constant.HttpStatus;
13 import com.jcdm.common.core.domain.AjaxResult;
14 import com.jcdm.common.core.domain.model.LoginUser;
15 import com.jcdm.common.core.page.PageDomain;
16 import com.jcdm.common.core.page.TableDataInfo;
17 import com.jcdm.common.core.page.TableSupport;
18 import com.jcdm.common.utils.DateUtils;
19 import com.jcdm.common.utils.PageUtils;
20 import com.jcdm.common.utils.SecurityUtils;
21 import com.jcdm.common.utils.StringUtils;
22 import com.jcdm.common.utils.sql.SqlUtil;
23
24 /**
25  * web层通用数据处理
26  * 
27  * @author jc
28  */
29 public class BaseController
30 {
31     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
32
33     /**
34      * 将前台传递过来的日期格式的字符串,自动转化为Date类型
35      */
36     @InitBinder
37     public void initBinder(WebDataBinder binder)
38     {
39         // Date 类型转换
40         binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
41         {
42             @Override
43             public void setAsText(String text)
44             {
45                 setValue(DateUtils.parseDate(text));
46             }
47         });
48     }
49
50     /**
51      * 设置请求分页数据
52      */
53     protected void startPage()
54     {
55         PageUtils.startPage();
56     }
57
58     /**
59      * 设置请求排序数据
60      */
61     protected void startOrderBy()
62     {
63         PageDomain pageDomain = TableSupport.buildPageRequest();
64         if (StringUtils.isNotEmpty(pageDomain.getOrderBy()))
65         {
66             String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
67             PageHelper.orderBy(orderBy);
68         }
69     }
70
71     /**
72      * 清理分页的线程变量
73      */
74     protected void clearPage()
75     {
76         PageUtils.clearPage();
77     }
78
79     /**
80      * 响应请求分页数据
81      */
82     @SuppressWarnings({ "rawtypes", "unchecked" })
83     protected TableDataInfo getDataTable(List<?> list)
84     {
85         TableDataInfo rspData = new TableDataInfo();
86         rspData.setCode(HttpStatus.SUCCESS);
87         rspData.setMsg("查询成功");
88         rspData.setRows(list);
89         rspData.setTotal(new PageInfo(list).getTotal());
90         return rspData;
91     }
92
93     /**
94      * 返回成功
95      */
96     public AjaxResult success()
97     {
98         return AjaxResult.success();
99     }
100
101     /**
102      * 返回失败消息
103      */
104     public AjaxResult error()
105     {
106         return AjaxResult.error();
107     }
108
109     /**
110      * 返回成功消息
111      */
112     public AjaxResult success(String message)
113     {
114         return AjaxResult.success(message);
115     }
116     
117     /**
118      * 返回成功消息
119      */
120     public AjaxResult success(Object data)
121     {
122         return AjaxResult.success(data);
123     }
124
125     /**
126      * 返回失败消息
127      */
128     public AjaxResult error(String message)
129     {
130         return AjaxResult.error(message);
131     }
132
133     /**
134      * 返回警告消息
135      */
136     public AjaxResult warn(String message)
137     {
138         return AjaxResult.warn(message);
139     }
140
141     /**
142      * 响应返回结果
143      * 
144      * @param rows 影响行数
145      * @return 操作结果
146      */
147     protected AjaxResult toAjax(int rows)
148     {
149         return rows > 0 ? AjaxResult.success() : AjaxResult.error();
150     }
151
152     /**
153      * 响应返回结果
154      * 
155      * @param result 结果
156      * @return 操作结果
157      */
158     protected AjaxResult toAjax(boolean result)
159     {
160         return result ? success() : error();
161     }
162
163     /**
164      * 页面跳转
165      */
166     public String redirect(String url)
167     {
168         return StringUtils.format("redirect:{}", url);
169     }
170
171     /**
172      * 获取用户缓存信息
173      */
174     public LoginUser getLoginUser()
175     {
176         return SecurityUtils.getLoginUser();
177     }
178
179     /**
180      * 获取登录用户id
181      */
182     public Long getUserId()
183     {
184         return getLoginUser().getUserId();
185     }
186
187     /**
188      * 获取登录部门id
189      */
190     public Long getDeptId()
191     {
192         return getLoginUser().getDeptId();
193     }
194
195     /**
196      * 获取登录用户名
197      */
198     public String getUsername()
199     {
200         return getLoginUser().getUsername();
201     }
202 }