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.entity.SysMenu;
import com.yunzhi.marketing.service.ISysMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
    * 菜单表 不同系统,菜单不一致 前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-09-11
 */
@RestController
@RequestMapping("/api")
public class SysMenuController extends BaseController {

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

    @Autowired
    public ISysMenuService iSysMenuService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/sysMenu",method= RequestMethod.GET)
    public ResponseBean sysMenuList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                    @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<SysMenu> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

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

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

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

    /**
     * 修改对象
     * @param id  实体ID
     * @param sysMenu 实体对象
     * @return
     */
    @RequestMapping(value="/sysMenu/{id}",method= RequestMethod.PUT)
    public ResponseBean sysMenuUpdate(@PathVariable Integer id,
                                        @RequestBody SysMenu sysMenu){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iSysMenuService.updateById(sysMenu)){
                responseBean.addSuccess(sysMenu);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("sysMenuUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/sysMenu/{id}",method= RequestMethod.GET)
    public ResponseBean sysMenuGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iSysMenuService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("sysMenuDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 获取所有模块
     * @return
     */
    @GetMapping(value = "/center/sysmodules")
    public ResponseBean allModules(@RequestParam Integer userId) {
        List<SysMenu> sysMenuList = iSysMenuService.getAllModules(userId);
        return ResponseBean.success(sysMenuList);
    }

    @PostMapping(value = "/center/sysmodules")
    public ResponseBean authorizeModules(@RequestBody Map<String, Object> data) {

        Integer userId = (Integer) data.get("userId");
        List<Integer> menuIds = (List<Integer>) data.get("menus");

        try {
            iSysMenuService.authorizeModules(userId, menuIds);
        } catch (Exception e) {
            return ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
        }

        return ResponseBean.success("ok");
    }
}
