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

import cn.estsh.i3plus.core.api.iservice.busi.ICoreTreeService;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SysDepartment;
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 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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Description : 系统资源服务demo
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-09-26 10:34
 * @Modify:
 **/
@RestController
@Api(description="树和异常demo")
@RequestMapping(PlatformConstWords.BASE_URL + "/demo")
public class DemoTreeAndExceptionController extends CoreBaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoTreeAndExceptionController.class);

    @Autowired
    private ICoreTreeService coreTreeService;

    @GetMapping(value="/tree/department")
    @ApiOperation(value="查询部门树",notes="通过部门主键获取部门树，-1为根节点")
    public ResultBean listDepartmentTree(long depParentId) {
        try {
            SysDepartment department = coreTreeService.getSysDepartmentTreeById(depParentId);

            return ResultBean.success("查询部门树成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(department);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @GetMapping(value="/excep")
    @ApiOperation(value="异常处理",notes="异常处理")
    public ResultBean excepTest(int num) {
        try {
            if(num == 1){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.SYSTEM_EXCEPTION.getCode())
                        .setErrorDetail("测试异常")
                        .setErrorSolution("测试异常解决方案")
                        .build();
            }else if(num == 2){
                throw new RuntimeException("运行中异常");
            }else if(num == 3){
                System.out.println("分母0异常");
                int i = 0/0;
            }else if(num == 4){
                System.out.println("连接异常");
                URLConnection urlCon = new URLConnection(new URL("192.168.1.0")) {
                    @Override
                    public void connect() throws IOException {

                    }
                };
            }else{
                System.out.println("文件异常");
                throw new FileNotFoundException();
            }
            return ResultBean.success("操作成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            //业务异常
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            //其他异常
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
