package com.template.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.template.api.SmartDataClassControllerAPI;
import com.template.common.utils.CommonUtil;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartDataClass;
import com.template.model.pojo.SmartDataSource;
import com.template.model.pojo.SmartDataSourceLog;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartDataClassService;
import com.template.services.SmartDataSourceLogService;
import com.template.services.SmartDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

/**
 * <p>
 * 数据源类别 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-12-05
 */
@RestController
public class SmartDataClassController implements SmartDataClassControllerAPI {
    @Autowired
    private SmartDataClassService smartDataClassService;

    @Autowired
    private SmartDataSourceService smartDataSourceService;

    @Autowired
    private SmartDataSourceLogService smartDataSourceLogService;

    /**
     * 新增 数据源分类
     *
     * @param smartDataClass
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult insertSmartDataClass(SmartDataClass smartDataClass, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if (smartDataClass.getDsClsName() == null) {
            return CommonResult.fail("数据源分类名称为空！");
        }
        if (smartDataClass.getDsClsDriver() == null) {
            return CommonResult.fail("数据源驱动为空！");
        }

        // 检测是否有重复名称
        QueryWrapper<SmartDataClass> smartDataClassQueryWrapper = new QueryWrapper<>();
        smartDataClassQueryWrapper.eq("ds_cls_name", smartDataClass.getDsClsName());
        smartDataClassQueryWrapper.eq("ds_cls_driver", smartDataClass.getDsClsDriver());
        int count = smartDataClassService.count(smartDataClassQueryWrapper);
        if (count > 0) {
            return CommonResult.fail("数据源分类名称和驱动有重复，新增失败！");
        }

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("新增数据源分类|【数据交换中心】→【数据源分类管理】|新增【数据源分类】|create", httpServletRequest);
        int result = smartDataClassService.insertSmartDataClass(smartDataClass);
        if (result > 0) {
            smartDataSourceLog.setLogActionStatus(1);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.ok("添加成功");
        } else {
            smartDataSourceLog.setLogActionStatus(0);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.fail("添加失败");
        }
    }

    /**
     * 更新 数据源分类
     *
     * @param smartDataClass
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateSmartDataClassById(SmartDataClass smartDataClass, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if (smartDataClass.getDsClsName() == null) {
            return CommonResult.fail("数据源分类名称为空！");
        }
        if (smartDataClass.getDsClsDriver() == null) {
            return CommonResult.fail("数据源驱动为空！");
        }

        // 检测是否有重复名称
        QueryWrapper<SmartDataClass> smartDataClassQueryWrapper = new QueryWrapper<>();
        smartDataClassQueryWrapper.eq("ds_cls_name", smartDataClass.getDsClsName());
        smartDataClassQueryWrapper.eq("ds_cls_driver", smartDataClass.getDsClsDriver());
        int count = smartDataClassService.count(smartDataClassQueryWrapper);
        if (count > 0) {
            return CommonResult.fail("数据源分类名称和驱动有重复，更新失败！");
        }

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("修改数据源分类|【数据交换中心】→【数据源分类管理】|修改【数据源分类】|update", httpServletRequest);
        int result = smartDataClassService.updateSmartDataClass(smartDataClass);
        if (result > 0) {
            smartDataSourceLog.setLogActionStatus(1);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.ok("修改成功");
        } else {
            smartDataSourceLog.setLogActionStatus(0);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.fail("修改失败");
        }
    }

    /**
     * 数据源分类 分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    public CommonResult queryPageSmartDataClass(int currentPage, int pageCount, String name) {
        PageUtils<SmartDataClass> result = smartDataClassService.queryPageSmartDataClasss(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }


    @Override
    public CommonResult deleteSmartDataClassById(int id, HttpServletRequest httpServletRequest) {

        SmartDataClass data = smartDataClassService.getSmartById(id);

        if (data == null) {
            return CommonResult.fail("当前数据源类别不存在，删除失败！");
        }

        // 检测当前类别下是否有数据源，有则不能删除，否则可以删除
        QueryWrapper<SmartDataSource> smartDataSourceQueryWrapper = new QueryWrapper<>();
        smartDataSourceQueryWrapper.eq("ds_cls_id", id);
        int count = smartDataSourceService.count(smartDataSourceQueryWrapper);
        if (count > 0) {
            return CommonResult.fail("当前类别下有数据源，不能删除，删除失败！");
        }

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("删除数据源分类|【数据交换中心】→【数据源分类管理】|删除【数据源分类】|delete", httpServletRequest);
        int result = smartDataClassService.deleteSmartDataClassById(id);
        if (result > 0) {
            smartDataSourceLog.setLogActionStatus(1);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.ok("删除成功");
        } else {
            smartDataSourceLog.setLogActionStatus(0);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.fail("删除失败");
        }
    }
}

