package com.schisandra.system.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;

import com.schisandra.system.application.convert.SchisandraSysLogDTOConverter;
import com.schisandra.system.application.dto.SchisandraSysLogDTO;
import com.schisandra.system.common.entity.Result;
import com.schisandra.system.domain.bo.SchisandraSysLogBO;
import com.schisandra.system.domain.service.SchisandraSysLogDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 *  controller
 *
 * @author landaiqing
 * @since 2024-05-12 14:21:42
 */
@RestController
@RequestMapping("/system/log/")
@Slf4j
public class SchisandraSysLogController {

    @Resource
    private SchisandraSysLogDomainService schisandraSysLogDomainService;

    /**
     * 新增
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SchisandraSysLogDTO schisandraSysLogDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraSysLogController.add.dto:{}", JSON.toJSONString(schisandraSysLogDTO));
            }
            Preconditions.checkNotNull(schisandraSysLogDTO.getId(), "编号不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getLogType(), "日志类型不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getLogTitle(), "日志标题不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateBy(), "创建者不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateByName(), "用户名称不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateDate(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestUri(), "请求URI不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestMethod(), "操作方式不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestParams(), "操作提交的数据不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getDiffModifyData(), "新旧数据比较结果不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBizKey(), "业务主键不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBizType(), "业务类型不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRemoteAddr(), "操作IP地址不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getServerAddr(), "请求服务器地址不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getIsException(), "是否异常不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getExceptionInfo(), "异常信息不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getUserAgent(), "用户代理不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getDeviceName(), "设备名称/操作系统不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBrowserName(), "浏览器名称不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getExecuteTime(), "执行时间不能为空");
            SchisandraSysLogBO SchisandraSysLogBO = SchisandraSysLogDTOConverter.INSTANCE.convertDTOToBO(schisandraSysLogDTO);
            return Result.ok(schisandraSysLogDomainService.add(SchisandraSysLogBO));
        } catch (Exception e) {
            log.error("SchisandraSysLogController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增失败");
        }

    }

    /**
     * 修改
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SchisandraSysLogDTO schisandraSysLogDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraSysLogController.update.dto:{}", JSON.toJSONString(schisandraSysLogDTO));
            }
            Preconditions.checkNotNull(schisandraSysLogDTO.getId(), "编号不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getLogType(), "日志类型不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getLogTitle(), "日志标题不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateBy(), "创建者不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateByName(), "用户名称不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateDate(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestUri(), "请求URI不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestMethod(), "操作方式不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestParams(), "操作提交的数据不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getDiffModifyData(), "新旧数据比较结果不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBizKey(), "业务主键不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBizType(), "业务类型不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRemoteAddr(), "操作IP地址不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getServerAddr(), "请求服务器地址不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getIsException(), "是否异常不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getExceptionInfo(), "异常信息不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getUserAgent(), "用户代理不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getDeviceName(), "设备名称/操作系统不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBrowserName(), "浏览器名称不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getExecuteTime(), "执行时间不能为空");
            SchisandraSysLogBO schisandraSysLogBO = SchisandraSysLogDTOConverter.INSTANCE.convertDTOToBO(schisandraSysLogDTO);
            return Result.ok(schisandraSysLogDomainService.update(schisandraSysLogBO));
        } catch (Exception e) {
            log.error("SchisandraSysLogController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新信息失败");
        }

    }

    /**
     * 删除
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SchisandraSysLogDTO schisandraSysLogDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SchisandraSysLogController.delete.dto:{}", JSON.toJSONString(schisandraSysLogDTO));
            }
            Preconditions.checkNotNull(schisandraSysLogDTO.getId(), "编号不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getLogType(), "日志类型不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getLogTitle(), "日志标题不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateBy(), "创建者不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateByName(), "用户名称不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getCreateDate(), "创建时间不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestUri(), "请求URI不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestMethod(), "操作方式不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRequestParams(), "操作提交的数据不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getDiffModifyData(), "新旧数据比较结果不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBizKey(), "业务主键不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBizType(), "业务类型不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getRemoteAddr(), "操作IP地址不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getServerAddr(), "请求服务器地址不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getIsException(), "是否异常不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getExceptionInfo(), "异常信息不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getUserAgent(), "用户代理不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getDeviceName(), "设备名称/操作系统不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getBrowserName(), "浏览器名称不能为空");
            Preconditions.checkNotNull(schisandraSysLogDTO.getExecuteTime(), "执行时间不能为空");
            SchisandraSysLogBO schisandraSysLogBO = SchisandraSysLogDTOConverter.INSTANCE.convertDTOToBO(schisandraSysLogDTO);
            return Result.ok(schisandraSysLogDomainService.delete(schisandraSysLogBO));
        } catch (Exception e) {
            log.error("SchisandraSysLogController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除信息失败");
        }

    }

}
