package com.billion.common.core.controller; import java.beans.PropertyEditorSupport; import java.util.Date; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.billion.common.constant.HttpStatus; import com.billion.common.core.domain.AjaxResult; import com.billion.common.core.domain.model.LoginUser; import com.billion.common.core.page.PageDomain; import com.billion.common.core.page.TableDataInfo; import com.billion.common.core.page.TableSupport; import com.billion.common.utils.DateUtils; import com.billion.common.utils.PageUtils; import com.billion.common.utils.SecurityUtils; import com.billion.common.utils.StringUtils; import com.billion.common.utils.sql.SqlUtil; /** * web层通用数æ®å¤„ç† * * @author ruoyi */ public class BaseController { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); /** * å°†å‰å°ä¼ 递过æ¥çš„æ—¥æœŸæ ¼å¼çš„å—符串,自动转化为Date类型 */ @InitBinder public void initBinder(WebDataBinder binder) { // Date ç±»åž‹è½¬æ¢ binder.registerCustomEditor(Date.class, new PropertyEditorSupport() { @Override public void setAsText(String text) { setValue(DateUtils.parseDate(text)); } }); } /** * è®¾ç½®è¯·æ±‚åˆ†é¡µæ•°æ® */ protected void startPage() { PageUtils.startPage(); } /** * 设置请求排åºæ•°æ® */ protected void startOrderBy() { PageDomain pageDomain = TableSupport.buildPageRequest(); if (StringUtils.isNotEmpty(pageDomain.getOrderBy())) { String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy()); PageHelper.orderBy(orderBy); } } /** * 清ç†åˆ†é¡µçš„线程å˜é‡ */ protected void clearPage() { PageUtils.clearPage(); } /** * å“åº”è¯·æ±‚åˆ†é¡µæ•°æ® */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected TableDataInfo getDataTable(List<?> list) { TableDataInfo rspData = new TableDataInfo(); rspData.setCode(HttpStatus.SUCCESS); rspData.setMsg("查询æˆåŠŸ"); rspData.setRows(list); rspData.setTotal(new PageInfo(list).getTotal()); return rspData; } /** * 返回æˆåŠŸ */ public AjaxResult success() { return AjaxResult.success(); } /** * è¿”å›žå¤±è´¥æ¶ˆæ¯ */ public AjaxResult error() { return AjaxResult.error(); } /** * 返回æˆåŠŸæ¶ˆæ¯ */ public AjaxResult success(String message) { return AjaxResult.success(message); } /** * 返回æˆåŠŸæ¶ˆæ¯ */ public AjaxResult success(Object data) { return AjaxResult.success(data); } /** * è¿”å›žå¤±è´¥æ¶ˆæ¯ */ public AjaxResult error(String message) { return AjaxResult.error(message); } /** * 返回è¦å‘Šæ¶ˆæ¯ */ public AjaxResult warn(String message) { return AjaxResult.warn(message); } /** * å“应返回结果 * * @param rows å½±å“行数 * @return æ“作结果 */ protected AjaxResult toAjax(int rows) { return rows > 0 ? AjaxResult.success() : AjaxResult.error(); } /** * å“应返回结果 * * @param result 结果 * @return æ“作结果 */ protected AjaxResult toAjax(boolean result) { return result ? success() : error(); } /** * 页é¢è·³è½¬ */ public String redirect(String url) { return StringUtils.format("redirect:{}", url); } /** * 获å–用户缓å˜ä¿¡æ¯ */ public LoginUser getLoginUser() { return SecurityUtils.getLoginUser(); } /** * 获å–登录用户id */ public Long getUserId() { return getLoginUser().getUserId(); } /** * 获å–登录部门id */ public Long getDeptId() { return getLoginUser().getDeptId(); } /** * 获å–登录用户å */ public String getUsername() { return getLoginUser().getUsername(); } }