BController.java 5.8 KB
package com.bsth.controller.schedule;

import com.bsth.common.Constants;
import com.bsth.common.ResponseCode;
import com.bsth.entity.schedule.BEntity;
import com.bsth.entity.sys.SysUser;
import com.bsth.service.schedule.BService;
import com.bsth.service.schedule.ScheduleException;
import com.bsth.service.sys.SysUserService;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.*;

/**
 * 基础控制器。
 */
public class BController<T, ID extends Serializable> {
    @Autowired
    protected BService<T, ID> bService;
    @Autowired
    private SysUserService sysUserService;

    // CRUD 操作
    // Create操作
    @RequestMapping(method = RequestMethod.POST)
    public Map<String, Object> save(@RequestBody T t, HttpSession httpSession) {
        // 判定T是否是BEntity的子类,增加新的字段
        String userName = String.valueOf(httpSession.getAttribute(Constants.SESSION_USERNAME));
        SysUser sysUser = sysUserService.findByUserName(userName);
        BEntity t_b = null;
        if (t instanceof BEntity) {
            t_b = (BEntity) t;
            t_b.setCreateBy(sysUser);
            t_b.setCreateDate(new Date());
        }

        T t_saved = bService.save(t_b == null ? t : (T) t_b);
        Map<String, Object> rtn = new HashMap<>();
        rtn.put("status", ResponseCode.SUCCESS);
        rtn.put("data", t_saved);
        return rtn;
    }
    // Update操作
    @RequestMapping(value="/{id}", method = RequestMethod.POST)
    public Map<String, Object> update(@RequestBody T t, HttpSession httpSession) {
        String userName = String.valueOf(httpSession.getAttribute(Constants.SESSION_USERNAME));
        SysUser sysUser = sysUserService.findByUserName(userName);
        BEntity t_b = null;
        if (t instanceof BEntity) {
            t_b = (BEntity) t;
            t_b.setUpdateBy(sysUser);
            t_b.setUpdateDate(new Date());
        }

        T t_updated = bService.save(t_b == null ? t : (T) t_b);
        Map<String, Object> rtn = new HashMap<>();
        rtn.put("status", ResponseCode.SUCCESS);
        rtn.put("data", t_updated);
        return rtn;
    }
    // Research操作
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Map<String, Object> findById(@PathVariable("id") ID id) {
        T t = bService.findById(id);
        Map<String, Object> rtn = new HashMap<>();
        rtn.put("status", ResponseCode.SUCCESS);
        rtn.put("data", t);
        return rtn;
    }
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public Map<String, Object> list(@RequestParam Map<String, Object> param) {
        List<T> tList = bService.list(param);
        Map<String, Object> rtn = new HashMap<>();
        rtn.put("status", ResponseCode.SUCCESS);
        rtn.put("data", tList);
        return rtn;
    }
    @RequestMapping(method = RequestMethod.GET)
    public Map<String, Object> list(
            @RequestParam Map<String, Object> map,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "id") String order,
            @RequestParam(defaultValue = "DESC") String direction) {
        // 允许多个字段排序,order可以写单个字段,也可以写多个字段
        // 多个字段格式:{col1},{col2},{col3},....,{coln}
        List<String> order_columns = Splitter.on(",").trimResults().splitToList(order);
        // 多字段排序:DESC,ASC...
        List<String> order_dirs = Splitter.on(",").trimResults().splitToList(direction);

        Map<String, Object> rtn = new HashMap<>();

        if (order_dirs.size() == 1) { // 所有字段采用一种排序
            rtn.put("status", ResponseCode.SUCCESS);
            if (order_dirs.get(0).equals("ASC")) {
                rtn.put("data", bService.list(map, new PageRequest(page, size, new Sort(Sort.Direction.ASC, order_columns))));
            } else {
                rtn.put("data", bService.list(map, new PageRequest(page, size, new Sort(Sort.Direction.DESC, order_columns))));
            }
        } else if (order_columns.size() == order_dirs.size()) {
            List<Sort.Order> orderList = new ArrayList<>();
            for (int i = 0; i < order_columns.size(); i++) {
                if (null != order_dirs.get(i) && order_dirs.get(i).equals("ASC")) {
                    orderList.add(new Sort.Order(Sort.Direction.ASC, order_columns.get(i)));
                } else {
                    orderList.add(new Sort.Order(Sort.Direction.DESC, order_columns.get(i)));
                }
            }
            rtn.put("status", ResponseCode.SUCCESS);
            rtn.put("data", bService.list(map, new PageRequest(page, size, new Sort(orderList))));
        } else {
            throw new RuntimeException("多字段排序参数格式问题,排序顺序和字段数不一致");
        }

        return rtn;

    }

    // Delete操作
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> delete(@PathVariable("id") ID id, HttpSession httpSession) {
        Map<String, Object> rtn = new HashMap<>();
        try {
            // 由于种种原因,这里不保存用户和操作时间了
            bService.delete(id);
            rtn.put("status", ResponseCode.SUCCESS);
        } catch (ScheduleException exp) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", exp.getMessage());
        }

        return rtn;
    }

}