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

import cn.estsh.i3plus.core.api.iservice.busi.ISysToolTypeService;
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.SysToolType;
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 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 : yunhao
 * @Date : 2018-10-28 12:40
 * @Modify :
 **/
@RestController
@Api(description = "硬件类型服务")
@RequestMapping(PlatformConstWords.BASE_URL +"/tool-type")
public class SysToolTypeController extends CoreBaseController{
    public static final Logger LOGGER = LoggerFactory.getLogger(SysToolTypeController.class);

    @Autowired
    private ISysToolTypeService toolTypeService;

    @PostMapping("/insert")
    @ApiOperation(value = "添加硬件类型",notes = "添加硬件类型")
    public ResultBean insertSysToolType(SysToolType toolType){
        try {
            // 条件校验
            ValidatorBean.beginValid(toolType).notNull("name",toolType.getName());

            // 添加初始化
            ConvertBean.modelInitialize(toolType, AuthUtil.getSessionUser());

            toolTypeService.insertSysToolType(toolType);
            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 updateSysToolType(SysToolType toolType){
        try {
            // 登录用户
            SessionUser user = AuthUtil.getSessionUser();

            // 条件校验
            ValidatorBean.beginValid(toolType).notNull("name",toolType.getName());

            // 添加初始化
            ConvertBean.modelUpdate(toolType,user);

            toolTypeService.updateSysToolType(toolType);
            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 deleteSysToolType(@PathVariable("id") String idStr){
        try {
            ValidatorBean.checkNotNull(idStr,"id不能为空");
            if(toolTypeService.getSysToolTypeById(Long.parseLong(idStr)) == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("硬件类型不存在")
                        .setErrorSolution("请重新输入硬件类型id")
                        .build();
            }

            toolTypeService.deleteSysToolTypeById(Long.parseLong(idStr));
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            LOGGER.error(busExcep.getErrorMsg() + "：{}",busExcep.getErrorDetail(),busExcep);
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "查询全部",notes = "查询全部")
    public ResultBean findSysToolTypeAll(){
        try{
            List<SysToolType> toolTypeList = toolTypeService.listSysToolType();
            return ResultBean.success("操作成功").setResultList(toolTypeList).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 getSysToolTypeById(@PathVariable("id") String id){
        try {
            // 条件校验
            ValidatorBean.checkNotNull(id,"id不能为空");

            SysToolType toolType = toolTypeService.getSysToolTypeById(Long.parseLong(id));
            if (toolType != null) {
                return ResultBean.success("操作成功").setResultObject(toolType).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 querySysToolTypeByPager(SysToolType toolType, Pager pager){
        try {
            ListPager sysDictionaryListPager = toolTypeService.querySysToolTypeByPager(toolType,pager);
            return  ResultBean.success("操作成功").setListPager(sysDictionaryListPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            LOGGER.error(busExcep.getErrorMsg() + "：{}",busExcep.getErrorDetail(),busExcep);
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

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