package com.template.controller;


import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.template.api.SmartDataTaskControllerAPI;
import com.template.common.utils.CommonUtil;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartDataSourceLog;
import com.template.model.pojo.SmartDataTask;
import com.template.model.pojo.SmartDataTaskDebug;
import com.template.model.pojo.SmartDataTaskLog;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartDataSourceLogService;
import com.template.services.SmartDataTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
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 SmartDataTaskController implements SmartDataTaskControllerAPI {

    @Autowired
    private SmartDataTaskService smartDataTaskService;

    @Autowired
    private SmartDataSourceLogService smartDataSourceLogService;

    @Override
    public CommonResult createJob(SmartDataTask smartDataTask, 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 = smartDataTaskService.createJob(smartDataTask);
        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"));
        }
    }

    @Override
    public CommonResult pauseJob(SmartDataTask smartDataTask, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("暂停定时任务|【数据交换中心】→【数据源数据交换任务】|暂停【定时任务】|pause", httpServletRequest);
        Map<String, Object> result = smartDataTaskService.pauseJob(smartDataTask);
        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"));
        }
    }

    @Override
    public CommonResult resumeJob(SmartDataTask smartDataTask, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("恢复定时任务|【数据交换中心】→【数据源数据交换任务】|恢复【定时任务】|resume", httpServletRequest);
        Map<String, Object> result = smartDataTaskService.resumeJob(smartDataTask);
        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"));
        }
    }

    @Override
    public CommonResult updateJob(SmartDataTask smartDataTask, 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 = smartDataTaskService.updateJob(smartDataTask);
        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"));
        }
    }

    @Override
    public CommonResult deleteJob(SmartDataTask smartDataTask, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("删除定时任务|【数据交换中心】→【数据源数据交换任务】|删除【定时任务】|delete", httpServletRequest);
        Map<String, Object> result = smartDataTaskService.deleteJob(smartDataTask);
        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"));
        }
    }

    @Override
    public CommonResult runOnceJob(SmartDataTask smartDataTask, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("执行1次定时任务|【数据交换中心】→【数据源数据交换任务】|执行1次【定时任务】|runOnce", httpServletRequest);
        Map<String, Object> result = smartDataTaskService.runOnceJob(smartDataTask);
        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"));
        }
    }

    /**
     * 部门
     */
    public CommonResult getDepart() {
        Map<String, Object> result = smartDataTaskService.getDepart();
        if (Integer.parseInt((String) result.get("code")) == 0) {
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    /**
     * 同步策略
     */
    @Override
    public CommonResult getSyncPolicy() {
        Map<String, Object> result = smartDataTaskService.getSyncPolicy();
        if (Integer.parseInt((String) result.get("code")) == 0) {
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    /**
     * 交换方式
     */
    @Override
    public CommonResult getExchangeType() {
        Map<String, Object> result = smartDataTaskService.getExchangeType();
        if (Integer.parseInt((String) result.get("code")) == 0) {
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    /**
     * 测试sql
     */
    public CommonResult testSql(String json) {
        // 获取数据源id对应的数据源
        Map<String, Object> result = smartDataTaskService.testSql(json);
        if (Integer.parseInt((String) result.get("code")) == 0) {
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    @Override
    public CommonResult getTables(String json) {
        // 获取数据源id对应的数据源
        Map<String, Object> result = smartDataTaskService.getTables(json);
        if (Integer.parseInt((String) result.get("code")) == 0) {
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    @Override
    public CommonResult getViews(String json) {
        // 获取数据源id对应的数据源
        Map<String, Object> result = smartDataTaskService.getViews(json);
        if (Integer.parseInt((String) result.get("code")) == 0) {
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    /**
     * 新增任务数据库数据
     *
     * @param smartDataTask 任务数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult insertSmartDataTask1(SmartDataTask smartDataTask, 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 = smartDataTaskService.insertSmartDataTask1(smartDataTask);
        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"));
        }
    }

    @Override
    public CommonResult insertSmartDataTask2(JSONObject json, 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 = smartDataTaskService.insertSmartDataTask2(json);
        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"));
        }
    }

    @Override
    public CommonResult insertSmartDataTask3(SmartDataTask smartDataTask, 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 = smartDataTaskService.insertSmartDataTask3(smartDataTask);
        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 smartDataTask 数据库数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateSmartDataTaskById1(SmartDataTask smartDataTask, 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 = smartDataTaskService.updateSmartDataTaskById1(smartDataTask);
        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"));
        }
    }

    @Override
    public CommonResult updateSmartDataTaskById2(JSONObject json, 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 = smartDataTaskService.updateSmartDataTaskById2(json);
        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"));
        }
    }

    @Override
    public CommonResult updateSmartDataTaskById3(SmartDataTask smartDataTask, 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 = smartDataTaskService.updateSmartDataTaskById3(smartDataTask);
        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 smartDataTask 查询名称
     * @return
     */
    @Override
    public CommonResult queryPageSmartDataTasks(int currentPage, int pageCount, SmartDataTask smartDataTask) {
        PageUtils<SmartDataTask> result = smartDataTaskService.queryPageSmartDataTasks(currentPage, pageCount, smartDataTask);

        return CommonResult.ok(result);
    }


    /**
     * 删除任务数据库数据
     *
     * @param id id
     * @return
     */
    @Override
    public CommonResult deleteSmartDataTaskById(int id, int delMethod, HttpServletRequest httpServletRequest) {

        SmartDataTask smartDataTask = smartDataTaskService.getSmartById(id);
        if (smartDataTask == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        if (delMethod == 0) {
            if (smartDataTask.getTkDeleted() == 1) {
                return CommonResult.fail("当前数据已是删除状态，无需删除，删除失败！");
            }
        } else if (delMethod == 1) {
            if (smartDataTask.getTkDeleted() == 0) {
                return CommonResult.fail("当前数据已是正常状态，无需恢复，恢复失败！");
            }
        }

        SmartDataSourceLog smartDataSourceLog = null;
        if (delMethod == 0) {
            smartDataSourceLog =
                    CommonUtil.generateLog("逻辑删除数据源数据交换任务|【数据交换中心】→【数据源数据交换任务】|逻辑删除【数据源数据交换任务】|delete_logic", httpServletRequest);
        } else if (delMethod == 1) {
            smartDataSourceLog =
                    CommonUtil.generateLog("恢复为正常数据源数据交换任务|【数据交换中心】→【数据源数据交换任务】|恢复为正常【数据源数据交换任务】|delete_recover", httpServletRequest);
        } else if (delMethod == 9) {
            smartDataSourceLog =
                    CommonUtil.generateLog("物理删除数据源数据交换任务|【数据交换中心】→【数据源数据交换任务】|物理删除【数据源数据交换任务】|delete_physics", httpServletRequest);
        }
        Map<String, Object> result = smartDataTaskService.deleteSmartDataTaskById(id, delMethod);
        if (result.get("code") == "0") {
            if (smartDataSourceLog != null) {
                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 eTaskId     任务id
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @return 返回
     */
    @Override
    public CommonResult queryPageSmartDataTaskDebug(int currentPage, int pageCount, Integer eTaskId, String startTime, String endTime) {
        PageInfo<SmartDataTaskDebug> result = smartDataTaskService.queryPageSmartDataTaskDebug(currentPage, pageCount, eTaskId, startTime, endTime);

        return CommonResult.ok(result);
    }

    /**
     * 删除任务运行日志
     *
     * @param json               json数据
     * @param httpServletRequest 请求对象
     * @param bindingResult      绑定对象
     * @return 返回
     */
    @Override
    public CommonResult delSmartDataTaskDebugByIds(JSONObject json, HttpServletRequest httpServletRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartDataSourceLog smartDataSourceLog =
                CommonUtil.generateLog("删除任务运行日志数据|【数据交换中心】→【任务运行日志】|删除【任务运行日志数据】|delete", httpServletRequest);
        Map<String, Object> result = smartDataTaskService.deleteSmartDataTaskDebugByIds(json);
        if (result.get("code") == "0") {
            smartDataSourceLog.setLogActionStatus(1);
            smartDataSourceLogService.insertSmartDataSourceLog(smartDataSourceLog);
            return CommonResult.ok(result.get("msg"));
        } else {
            return CommonResult.fail((String) result.get("msg"));
        }
    }

    @Override
    public CommonResult queryPageSmartDataTaskLog(int currentPage, int pageCount, Integer eTaskId, String tkLogTaskName, Integer tkLogAutoManual,
                                                  Integer tkLogDsSourceId, Integer tkLogDsDestinationId,
                                                  Integer tkLogCostTime, Integer tkLogExeStatus, String startTime, String endTime) {
        PageInfo<SmartDataTaskLog> result = smartDataTaskService.queryPageSmartDataTaskLog(currentPage, pageCount, eTaskId, tkLogTaskName,
                tkLogAutoManual, tkLogDsSourceId, tkLogDsDestinationId, tkLogCostTime, tkLogExeStatus, startTime, endTime);

        return CommonResult.ok(result);
    }
}

