package com.yunzhi.marketing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.entity.TaRole;
import com.yunzhi.marketing.service.ITaRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
    * 角色表 角色表，每个org的角色可以单独设置。但是都共有一个admin角色。这个角色在创建用户的时候创建好了 前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-09-11
 */
@RestController
@RequestMapping("/api")
public class TaRoleController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaRoleController.class);

    @Autowired
    public ITaRoleService iTaRoleService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taRole",method= RequestMethod.GET)
    public ResponseBean taRoleList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                   @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                   HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        Integer orgId = getOrgId(request);
        try {
            //使用分页插件
		    IPage<TaRole> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id",orgId);
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            queryWrapper.orderByDesc("create_date");

            IPage<TaRole> result = iTaRoleService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taRoleList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taRole 实体对象
     * @return
     */
    @RequestMapping(value="/admin/taRole",method= RequestMethod.POST)
    public ResponseBean taRoleAdd(@RequestBody TaRole taRole){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaRoleService.save(taRole)){
                responseBean.addSuccess(taRole);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taRoleAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/taRole/{id}", method= RequestMethod.DELETE)
    public ResponseBean taRoleDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTaRoleService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taRoleDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taRole 实体对象
     * @return
     */
    @RequestMapping(value="/admin/editAndAddRole/{id}",method= RequestMethod.POST)
    public ResponseBean taRoleUpdate(@PathVariable Integer id,
                                        @RequestBody TaRole taRole) {
        ResponseBean responseBean = new ResponseBean();
        if (null==id){
            iTaRoleService.save(taRole);
        }else{
            iTaRoleService.updateById(taRole);
        }
        responseBean.addSuccess("成果");
        return responseBean;
    }
    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taRole/{id}",method= RequestMethod.GET)
    public ResponseBean taRoleGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaRoleService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taRoleDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改角色状态--删除
     * @param id
     * @return
     */
    @RequestMapping(value="/admin/taRole/stop/{id}",method= RequestMethod.PUT)
    public ResponseBean taRoleStop(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        TaRole taRole = new TaRole();
        taRole = iTaRoleService.getById(id);
        if (null != taRole){
            iTaRoleService.removeById(id);
        }
        return responseBean;
    }

    /**
     * 查询当前角色信息
     */
    @RequestMapping(value="/admin/taRole/details/{id}",method= RequestMethod.GET)
    public ResponseBean taRoleDetails(@PathVariable Integer id,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        TaRole taRole = iTaRoleService.taRoleDetails(id,getOrgId(request));
        responseBean.addSuccess(taRole);
        return responseBean;
    }
}
