BController_facade.java 6.79 KB
package com.bsth.controller.schedule;

import com.bsth.common.Constants;
import com.bsth.common.ResponseCode;
import com.bsth.control_v2.plan_module.common.dto.PageRequestDto;
import com.bsth.control_v2.plan_module.common.exception.PlanModuleException;
import com.bsth.control_v2.plan_module.common.service.BServiceFacade;
import com.bsth.entity.sys.SysUser;
import com.bsth.service.sys.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BController_facade<Id extends Serializable, DTO> {
    protected abstract BServiceFacade<Id, DTO> getBServiceFacade();

    @Autowired
    private SysUserService sysUserService;

    //---------------- 设置操作用户 ----------------//
    public void setCreateUserInfo(DTO dto, Integer userId, Date createDate) {
        throw new PlanModuleException("子类必须override setCUserInfo方法!");
    }
    public void setUpdateUserInfo(DTO dto, Integer userId, Date updateDate) {
        throw new PlanModuleException("子类必须override setUpdateUserInfo方法!");
    }

    //---------------- 扩展的save,update,delete操作 --------------------//
    protected boolean isSaveExtend() {
        return false;
    }
    protected DTO saveExtend(DTO dto) {
        throw new PlanModuleException("子类如果isSaveExtend()==true,必须覆写saveExtend!");
    }
    protected boolean isUpdateExtend() {
        return false;
    }
    protected DTO updateExtend(DTO dto) {
        throw new PlanModuleException("子类如果isUpdateExtend()==true,必须覆写updateExtend!");
    }
    protected boolean isDeleteExtend() {
        return false;
    }
    protected void deleteExtend(Id id) {
        throw new PlanModuleException("子类如果isDeleteExtend()==true,必须覆写deleteExtend!");
    }

    //---------------- CRUD 操作 ----------------//
    // Create操作
    @RequestMapping(method = RequestMethod.POST)
    public Map<String, Object> save(@RequestBody DTO dto, HttpSession httpSession) {
        String userName = String.valueOf(httpSession.getAttribute(Constants.SESSION_USERNAME));
        SysUser sysUser = sysUserService.findByUserName(userName);
        setCreateUserInfo(dto, sysUser.getId(), new Date());
        setUpdateUserInfo(dto, sysUser.getId(), new Date());

        Map<String, Object> rtn = new HashMap<>();
        try {
            DTO dto_saved;
            if (isSaveExtend()) {
                dto_saved = saveExtend(dto);
            } else {
                dto_saved = getBServiceFacade().save(dto);
            }
            rtn.put("status", ResponseCode.SUCCESS);
            rtn.put("data", dto_saved);
        } catch (Exception exp) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", exp.getMessage());
        }

        return rtn;
    }

    // Update操作
    @RequestMapping(value="/{id}", method = RequestMethod.POST)
    public Map<String, Object> update(@RequestBody DTO dto, HttpSession httpSession) {
        String userName = String.valueOf(httpSession.getAttribute(Constants.SESSION_USERNAME));
        SysUser sysUser = sysUserService.findByUserName(userName);
        setUpdateUserInfo(dto, sysUser.getId(), new Date());

        Map<String, Object> rtn = new HashMap<>();
        try {
            DTO dto_updated;
            if (isUpdateExtend()) {
                dto_updated = updateExtend(dto);
            } else {
                dto_updated = getBServiceFacade().save(dto);
            }
            rtn.put("status", ResponseCode.SUCCESS);
            rtn.put("data", dto_updated);
        } catch (Exception exp) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", exp.getMessage());
        }

        return rtn;
    }

    // Research操作
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Map<String, Object> findById(@PathVariable("id") Id id) {
        Map<String, Object> rtn = new HashMap<>();
        try {
            DTO dto = getBServiceFacade().findById(id);
            rtn.put("status", ResponseCode.SUCCESS);
            rtn.put("data", dto);
        } catch (Exception exp) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", exp.getMessage());
        }

        return rtn;
    }

    // 查询所有操作
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public Map<String, Object> list(@RequestParam Map<String, Object> param) {
        Map<String, Object> rtn = new HashMap<>();
        try {
            List<DTO> tList = getBServiceFacade().list(param);
            rtn.put("status", ResponseCode.SUCCESS);
            rtn.put("data", tList);
        } catch (Exception exp) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", exp.getMessage());
        }
        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) {
        PageRequestDto pageRequestDto = PageRequestDto.getBuilder()
                .setPage(page)
                .setSize(size)
                .setOrder(order)
                .setDirection(direction)
                .build();
        Map<String, Object> rtn = new HashMap<>();
        try {
            rtn.put("data", getBServiceFacade().list(map, pageRequestDto));
            rtn.put("status", ResponseCode.SUCCESS);
        } catch (Exception exp) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", exp.getMessage());
        }
        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 {
            // 由于种种原因,这里不保存用户和操作时间了
            if (isDeleteExtend()) {
                deleteExtend(id);
            } else {
                getBServiceFacade().delete(id);
            }
            rtn.put("status", ResponseCode.SUCCESS);
        } catch (Exception exp) {
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", exp.getMessage());
        }
        return rtn;
    }
}