package com.template.controller;


import com.template.api.SmartDataSourceLogControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartDataSourceLog;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartDataSourceLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 数据源操作日志 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-12-05
 */
@RestController
public class SmartDataSourceLogController implements SmartDataSourceLogControllerAPI {

    @Autowired
    private SmartDataSourceLogService smartDataSourceLogService;

    /**
     * 新增日志
     *
     * @param smartDataSourceLog 日志数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult insertSmartDataSourceLog(SmartDataSourceLog smartDataSourceLog, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if (smartDataSourceLog.getLogActionBusiness() == null) {
            return CommonResult.fail("【业务名称】不能为空！");
        }
        if (smartDataSourceLog.getLogActionHost() == null) {
            return CommonResult.fail("【操作主机ip】不能为空！");
        }
        if (smartDataSourceLog.getLogActionModule() == null) {
            return CommonResult.fail("【操作模块】不能为空！");
        }
        if (smartDataSourceLog.getLogActionClass() == null) {
            return CommonResult.fail("【操作类型】不能为空！");
        }
        if (smartDataSourceLog.getLogActionPeople() == null) {
            return CommonResult.fail("【操作人】不能为空！");
        }
        if (smartDataSourceLog.getLogActionRemote() == null) {
            return CommonResult.fail("【操作人ip】不能为空！");
        }
        if (smartDataSourceLog.getLogActionName() == null) {
            return CommonResult.fail("【操作名称】不能为空！");
        }
        if (smartDataSourceLog.getLogActionStatus() == null) {
            return CommonResult.fail("【操作状态】不能为空！");
        }

        int result = smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);

        return result > 0 ? CommonResult.ok("操作日志添加成功") : CommonResult.fail("操作日志添加失败");
    }

    /**
     * 日志分页数据查询
     *
     * @param currentPage        当前页数
     * @param pageCount          一页数据条数
     * @param smartDataSourceLog 查询名称
     * @return
     */
    @Override
    public CommonResult queryPageSmartDataSourceLogs(int currentPage, int pageCount, String startTime, String endTime,
                                                     SmartDataSourceLog smartDataSourceLog) {

        if ((startTime != null && endTime == null) || (startTime == null && endTime != null)) {
            return CommonResult.fail("查询的【开始时间】和【结束时间】必须同时提供！");
        }
        PageUtils<SmartDataSourceLog> result = smartDataSourceLogService.queryPageSmartDataSourceLogs(currentPage, pageCount,
                startTime, endTime, smartDataSourceLog);

        return CommonResult.ok(result);
    }


    /**
     * 更新日志
     * @param smartDataSourceLog 日志数据
     * @param bindingResult
     * @return
     */
//    @Override
//    public CommonResult updateSmartDataSourceLogById(SmartDataSourceLog smartDataSourceLog, BindingResult bindingResult) {
//        if (bindingResult.hasErrors()) {
//            String st = paramUtils.getParamError(bindingResult);
//            return CommonResult.fail(st);
//        }
//
//        int result = smartDataSourceLogService.updateSmartDataSourceLog(smartDataSourceLog);
//        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
//    }


//    @Override
//    public CommonResult deleteSmartDataSourceLogById(int id) {
//
//        SmartDataSourceLog data = smartDataSourceLogService.getSmartById(id);
//
//        if(data == null){
//            return CommonResult.fail("当前数据不存在，删除失败！");
//        }
//
//        int result = smartDataSourceLogService.deleteSmartDataSourceLogById(id);
//
//        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
//    }
}

