 package cn.estsh.i3plus.core.apiservice.controller.busi;

 import cn.estsh.i3plus.core.api.iservice.busi.ISysMenuService;
 import cn.estsh.i3plus.platform.common.convert.ConvertBean;
 import cn.estsh.i3plus.platform.common.tool.StringTool;
 import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
 import cn.estsh.i3plus.pojo.base.bean.ListPager;
 import cn.estsh.i3plus.pojo.base.common.Pager;
 import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
 import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
 import cn.estsh.i3plus.pojo.platform.bean.SysMenu;
 import cn.estsh.impp.framework.base.controller.CoreBaseController;
 import cn.estsh.impp.framework.boot.exception.ImppBusiException;
 import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
 import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
 import cn.estsh.impp.framework.boot.util.ResultBean;
 import cn.estsh.impp.framework.boot.util.ValidatorBean;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 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;

/**
 * @Description : 系统功能对外接口
 * @Reference :
 * @Author : wei.peng
 * @Date : 2018-10-22 15:18:14.982
 * @Modify :
 **/
@RestController
@Api(description = "系统功能服务")
@RequestMapping(PlatformConstWords.BASE_URL + "/sys-menu")
public class SysMenuController extends CoreBaseController {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysMenuController.class);

    @Autowired
    private ISysMenuService sysMenuService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增系统功能", notes = "系统功能")
    public ResultBean insertSysMenu(SysMenu menu) {
        try {
            //条件验证
            ValidatorBean.beginValid(menu)
                    .notNull("name", menu.getName())
                    .notNull("menuCode", menu.getMenuCode())
                    .notNull("menuType", menu.getMenuType());

            //新增初始化
            ConvertBean.modelInitialize(menu, getSessionUser());
            menu.setNameZhShortening(StringTool.getAllFirstLetter(menu.getName()));
            if(menu.getParentId() == null || menu.getParentId().intValue() == 0){
                menu.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
            }
            if(menu.getMenuSort() == null){
                menu.setMenuSort(0);
            }

            menu.setMenuStatus(CommonEnumUtil.DATA_STATUS.ENABLE.getValue());

            menu = sysMenuService.insertSysMenu(menu);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(menu);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep).build();
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "修改系统功能", notes = "系统功能")
    public ResultBean updateSysMenu(SysMenu menu) {
        try {
            // 数据校验
            ValidatorBean.beginValid(menu)
                    .notNull("id", menu.getId())
                    .notNull("menuType", menu.getMenuType())
                    .notNull("menuCode", menu.getMenuCode())
                    .notNull("name", menu.getName());

            ConvertBean.modelUpdate(menu, getSessionUser());
            menu.setNameZhShortening(StringTool.getAllFirstLetter(menu.getName()));
            if(menu.getParentId() == null || menu.getParentId().intValue() == 0){
                menu.setParentId(CommonEnumUtil.PARENT.DEFAULT.getValue());
            }
            if(menu.getMenuSort() == null){
                menu.setMenuSort(0);
            }

            sysMenuService.updateSysMenu(menu);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep).build();
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除系统功能", notes = "删除系统功能")
    public ResultBean deleteSysMenu(@PathVariable("id") String id) {
        try {
            // 数据校验
            ValidatorBean.checkIsNumber(id, "系统功能id 不能为空");

            sysMenuService.deleteSysMenuById(Long.parseLong(id));
            return ResultBean.success().setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "查询所有系统功能", notes = "查询所有系统功能")
    public ResultBean findAll() {
        try {
            List<SysMenu> list = sysMenuService.findSysMenuAll();
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultList(list);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/query")
    @ApiOperation(value="查询权限",notes="组合查询权限，外带分页功能")
    public ResultBean querySysMenu(SysMenu menu, Pager pager){
        try{
            ListPager<SysMenu> listPager = sysMenuService.querySysMenu(menu, pager);

            return ResultBean.success("操作成功").setListPager(listPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/get/{id}")
    @ApiOperation(value = "查询系统功能", notes = "查询根据ID查询系统功能")
    public ResultBean getById(@PathVariable("id") String id) {
        try {
            // 数据校验
            ValidatorBean.checkNotNull(id, "功能id 不能为空");

            SysMenu menu = sysMenuService.getSysMenuById(Long.parseLong(id));

            if(menu != null){
                return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(menu);
            }else {
                return ResultBean.fail("数据不存在").setCode(ResourceEnumUtil.MESSAGE.EMPTY.getCode());
            }
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }

    }

    @PutMapping(value = "/status")
    @ApiOperation(value = "修改系统功能状态", notes = "根据ID修改系统功能状态")
    public ResultBean updateStatus(String id,int status) {
        try {
            // 数据校验
            ValidatorBean.checkNotNull(id, "功能id 不能为空");
            ValidatorBean.checkNotZero(status, "功能状态不能为空");

            sysMenuService.updateSysMenuStatus(Long.parseLong(id),status,getSessionUser());
            return new ResultBean(true);
        } catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/batch-status")
    @ApiOperation(value = "批量修改系统功能状态", notes = "根据ID修改系统功能状态")
    public ResultBean updateBatchStatus(String[] ids,int status) {
        try {
            // 数据校验
            ids = ConvertBean.modelSafeArrayNumber(ids,true);
            ValidatorBean.checkNotNull(ids, "功能id 不能为空");
            ValidatorBean.checkNotZero(status, "功能状态不能为空");

            sysMenuService.updateSysMenuStatusByIds(StringTool.getArrayLong(ids),status,getSessionUser());

            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/batch-delete")
    @ApiOperation(value = "根据id批量删除", notes = "根据id批量删除")
    public ResultBean deleteSysMenuByIds(String[] ids){
        try{
            ids = ConvertBean.modelSafeArrayNumber(ids,true);
            if(ids.length == 0){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要删除的资源。")
                        .build();
            }
            sysMenuService.deleteSysMenuByIds(StringTool.getArrayLong(ids));
            return ResultBean.success("操作成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
