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

import cn.estsh.i3plus.core.api.iservice.busi.ISysToolService;
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.SessionUser;
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
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 io.swagger.annotations.ApiParam;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description : 硬件服务
 * @Reference :
 * @Author : yunhao
 * @Date : 2018-10-25 20:53
 * @Modify :
 **/
@RestController
@Api(description = "硬件服务")
@RequestMapping(PlatformConstWords.BASE_URL +"/tool")
public class SysToolController extends CoreBaseController{
    public static final Logger LOGGER = LoggerFactory.getLogger(SysToolController.class);

    @Autowired
    private ISysToolService toolService;

    @PostMapping("/insert")
    @ApiOperation(value = "添加硬件",notes = "添加硬件")
    public ResultBean insertSysTool(SysTool tool){
        try {
            // 登录用户
            SessionUser user = AuthUtil.getSessionUser();

            // 条件校验
            ValidatorBean.beginValid(tool)
                    .notNull("name",tool.getName())
                    .notNull("toolTypeId",tool.getToolTypeId())
                    .notNull("toolConnType",tool.getToolConnType())
                    .notNull("toolDataType",tool.getToolDataType())
                    .notNull("toolIp",tool.getToolIp())
                    .notNull("toolPort",tool.getToolPort());

            // 添加初始化
            tool.setToolStatus(CommonEnumUtil.DATA_STATUS.ENABLE.getValue());

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

    @PutMapping("/update")
    @ApiOperation(value = "修改硬件",notes = "修改硬件")
    public ResultBean updateSysTool(SysTool tool){
        try {
            // 登录用户
            SessionUser user = AuthUtil.getSessionUser();

            // 条件校验
            ValidatorBean.beginValid(tool)
                    .notNull("name",tool.getName())
                    .notNull("toolTypeId",tool.getToolTypeId())
                    .notNull("toolConnType",tool.getToolConnType())
                    .notNull("toolDataType",tool.getToolDataType())
                    .notNull("toolIp",tool.getToolIp())
                    .notNull("toolPort",tool.getToolPort());

            // 修改初始化
            ConvertBean.modelUpdate(tool,user);

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

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除硬件",notes = "根据id删除硬件")
    public ResultBean deleteSysTool(@PathVariable String id){
        try {
            ValidatorBean.checkNotNull(id,"id不能为空");

            toolService.deleteSysToolById(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 findSysToolAll(){
        try{
            List<SysTool> toolList = toolService.listSysTool();
            return ResultBean.success("操作成功").setResultList(toolList).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 = "根据id查询硬件",notes = "根据id查询硬件")
    public ResultBean getSysToolById(@PathVariable("id") String id){
        try {
            // 条件校验
            ValidatorBean.checkNotNull(id,"id不能为空");

            SysTool tool = toolService.getSysToolById(Long.parseLong(id));
            if (tool != null) {
                return ResultBean.success("操作成功").setResultObject(tool).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
            }else {
                return ResultBean.fail("硬件不存在").setCode(ResourceEnumUtil.MESSAGE.EMPTY.getCode());
            }
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/query")
    @ApiOperation(value = "硬件类型复杂查询，分页，排序",notes = "硬件类型复杂查询，分页，排序")
    public ResultBean querySysToolByPager(SysTool tool, Pager pager){
        try {
            ListPager sysSysToolListPager = toolService.querySysToolByPager(tool,pager);
            return  ResultBean.success("操作成功").setListPager(sysSysToolListPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/status")
    @ApiOperation(value = "硬件状态",notes = "硬件类型禁用")
    public ResultBean updateStatusEnable(String id,Integer status){
        try {
            toolService.updateSysToolStatusById(Long.parseLong(id),status);
            return  ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping("/batch-delete")
    @ApiOperation(value = "批量删除硬件",notes = "批量删除硬件")
    public ResultBean deleteSysToolByIds(String[] ids){
        try{
            if(ids.length == 0){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要删除的资源。")
                        .build();
            }
            toolService.deleteSysToolByIds(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);
        }
    }

    @PutMapping("/batch-status")
    @ApiOperation(value = "批量切换硬件状态",notes = "批量切换硬件状态")
    public ResultBean updateSysToolStatusByIds(String[] ids,Integer status){
        try{
            if(ids.length == 0){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            toolService.updateSysToolStatusByIds(StringTool.getArrayLong(ids),status);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
