package com.template.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.template.api.SmartDataSourceControllerAPI;
import com.template.common.utils.CommonUtil;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartDataSource;
import com.template.model.pojo.SmartDataSourceLog;
import com.template.model.pojo.SmartDataTask;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartDataSourceLogService;
import com.template.services.SmartDataSourceService;
import com.template.services.SmartDataTaskService;
import org.springframework.beans.factory.annotation.Autowired;
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.util.Map;

/**
 * <p>
 * 数据源配置 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-12-05
 */
@RestController
public class SmartDataSourceController implements SmartDataSourceControllerAPI {

    @Autowired
    private SmartDataSourceService smartDataSourceService;

    @Autowired
    private SmartDataTaskService smartDataTaskService;

    @Autowired
    private SmartDataSourceLogService smartDataSourceLogService;

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

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("新增数据源|【数据交换中心】→【数据源管理】|新增【数据源】|create", httpServletRequest);
        Map<String, Object> result = smartDataSourceService.insertSmartDataSource(smartDataSource);
        if (Integer.parseInt((String) result.get("code")) == 0) {
            smartDataSourceLog.setLogActionStatus(1);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    /**
     * 测试 数据源连接状态
     *
     * @param smartDataSource
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult vcoSmartDataSource(SmartDataSource smartDataSource, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        Map<String, Object> result = smartDataSourceService.vcoSmartDataSource(smartDataSource);

        return Integer.parseInt((String) result.get("code")) == 0 ? CommonResult.ok(result.get("msg")) : CommonResult.fail((String) result.get("msg"));
    }

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


        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("更新数据源|【数据交换中心】→【数据源管理】|更新【数据源】|update", httpServletRequest);
        Map<String, Object> result = smartDataSourceService.updateSmartDataSource(smartDataSource);
        if (Integer.parseInt((String) result.get("code")) == 0) {
            smartDataSourceLog.setLogActionStatus(1);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    /**
     * 数据源分页数据查询
     *
     * @param currentPage     当前页数
     * @param pageCount       一页数据条数
     * @param smartDataSource 查询数据源实体
     * @return
     */
    @Override
    public CommonResult queryPageSmartDataSources(int currentPage, int pageCount, SmartDataSource smartDataSource) {
        PageInfo<SmartDataSource> result = smartDataSourceService.queryPageSmartDataSources(currentPage, pageCount, smartDataSource);

        return CommonResult.ok(result);
    }


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

        SmartDataSource data = smartDataSourceService.getSmartById(id);

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

        // 检测当前类别下是否有数据源，有则不能删除，否则可以删除
        QueryWrapper<SmartDataTask> smartDataTaskQueryWrapper = new QueryWrapper<>();
        smartDataTaskQueryWrapper.eq("tk_ds_id_source", id).or().eq("tk_ds_id_destination", id);
        int count = smartDataTaskService.count(smartDataTaskQueryWrapper);
        if (count > 0) {
            return CommonResult.fail("当前数据源下有任务，不能删除，删除失败！");
        }

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

