package com.github.wxiaoqi.security.common.rest;

import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.context.BaseContextHandler;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.Query;
import com.github.wxiaoqi.security.common.util.ReflectionUtils;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-15 8:48
 */
@Slf4j
public class BaseController<Biz extends BaseBiz,Entity> extends CommonBaseController{
    @Autowired
    protected Biz baseBiz;

    public Biz getBaseBiz() {
        return baseBiz;
    }

    @ApiOperation("添加")
    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Entity> add(@RequestBody Entity entity) throws Exception {
        baseBiz.insertSelective(entity);
        return new ObjectRestResponse<Entity>();
    }

    @ApiOperation("查询")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Entity> get(@PathVariable int id){
        ObjectRestResponse<Entity> entityObjectRestResponse = new ObjectRestResponse<>();
        Object o = baseBiz.selectById(id);
        entityObjectRestResponse.data((Entity)o);
        return entityObjectRestResponse;
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<Entity> update(@RequestBody Entity entity) throws Exception {
        baseBiz.updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>();
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<Entity> remove(@PathVariable int id){
        baseBiz.deleteById(id);
        return new ObjectRestResponse<Entity>();
    }

    @ApiOperation("查询所有")
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    public List<Entity> all() throws Exception {
        return baseBiz.selectListAll();
    }

    @ApiOperation("查询分页")
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Entity> list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return baseBiz.selectByQuery(query);
    }

    @ApiOperation("查询分页-ObjectRestResponse")
    @RequestMapping(value = "/pages",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<PageDataVO<Entity>> pages(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return ObjectRestResponse.succ(baseBiz.selectByQueryPage(query));
    }

    @ApiOperation("查询分页-ObjectRestResponse")
    @RequestMapping(value = "/baseLogicPages",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<PageDataVO<Entity>> baseLogicPages(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        return ObjectRestResponse.succ(baseBiz.selectByQueryLogicPage(query));
    }

    @ApiOperation("根据参数查询，等于")
    @RequestMapping(value = "/entityList",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<List<Entity>> entityList(Entity entity){
        //查询列表数据
        return ObjectRestResponse.succ(baseBiz.selectList(entity));
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/del",method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse del( Entity entity){
        Field field = ReflectionUtils.getAccessibleField(entity, "isDel");
        Field field2 = ReflectionUtils.getAccessibleField(entity, "delete");

        if(null != field) {
            ReflectionUtils.setFieldValue(entity,"isDel",1);
        }
        if(null != field2) {
            ReflectionUtils.setFieldValue(entity,"delete",1);
        }

        baseBiz.updateSelectiveById(entity);
        return ObjectRestResponse.succ();
    }


    @RequestMapping(value = "baseDetail",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<Entity> baseDetail(Entity entity){
        Object o = baseBiz.selectOne(entity);
        return ObjectRestResponse.succ(o);
    }

    @RequestMapping(value = "baseAdd",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Entity> baseAdd(@RequestBody Entity entity){
        baseBiz.insertSelective(entity);
        return new ObjectRestResponse<Entity>();
    }

    @RequestMapping(value = "baseUpdate",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Entity> baseUpdate(@RequestBody Entity entity){
        baseBiz.updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>();
    }


    @Data
    public static class BaseDetailDTO {
        Integer id;
    }
}
