package com.hotent.portal.controller;


import com.hotent.base.controller.BaseController;
import com.hotent.base.enums.ResponseErrorEnums;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.*;
import com.hotent.base.util.time.DateFormatUtil;
import com.hotent.portal.model.Popup;
import com.hotent.portal.persistence.manager.PopupManager;
import com.hotent.table.datasource.DataSourceUtil;
import com.hotent.uc.api.impl.util.ContextUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 弹窗管理表 前端控制器
 *
 * @author zengst
 * @company 广州宏天软件股份有限公司
 * @since 2020-11-27
 */
@RestController
@RequestMapping("/portal/portalSysPopup/v1")
public class PopupController extends BaseController<PopupManager, Popup> {

    @Autowired
    PopupManager popupManager;

    @GetMapping("getCurrentUserPopup")
    @ApiOperation(value = "获取用户弹窗", httpMethod = "GET", notes = "获取用户弹窗")
    public CommonResult<List<Popup>> getCurrentUserPopup() throws Exception {
        String userId = ContextUtil.getCurrentUserId();
        List<Popup> authList = baseService.authList(userId);
        return new CommonResult<>(true, "", baseService.filterData(authList, userId));
    }

    @PostMapping("saveWithAuth")
    @ApiOperation(value = "保存弹窗并添加一个所有人的授权", httpMethod = "POST", notes = "保存弹窗提醒")
    public CommonResult<String> saveWithAuth(@ApiParam(name = "弹窗提醒", value = "POPUP") @RequestBody Popup popup) throws Exception {
        boolean result = baseService.saveWithAuth(popup);
        if (!result) {
            return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, null);
        }
        return new CommonResult<>();
    }

    @PutMapping("saveWithAuth")
    @ApiOperation(value = "更新弹窗提醒", httpMethod = "PUT", notes = "更新弹窗提醒")
    public CommonResult<String> updatePopup(@ApiParam(name = "popup", value = "实体信息") @RequestBody Popup popup) {
        return super.updateById(popup);
    }

    @GetMapping("validateSql")
    @ApiOperation(value = "验证sql语句", httpMethod = "GET", notes = "验证sql语句")
    public CommonResult<Integer> validateSql(
            @ApiParam(name = "SQL语句(base64)", value = "sql") @RequestParam String sql,
            @ApiParam(name = "数据源", value = "dsAlias") @RequestParam String dsAlias) throws Exception {
        return popupManager.validateSql(sql,dsAlias);
    }

    @PostMapping(value = "exportPopup", produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "导出弹窗提醒", httpMethod = "POST", notes = "导出弹窗提醒")
    public void exportPopup(@ApiParam(name = "ids", value = "要导出的id") @RequestBody List<String> ids,
                            HttpServletRequest request,
                            HttpServletResponse response) throws Exception {
        String fileName = "popup_" + DateFormatUtil.format(LocalDateTime.now(), "yyyy_MM_dd_HHmm");
        String json = baseService.exportPopup(ids);
        HttpUtil.downLoadFile(request, response, json, "popup.json", fileName);
    }

    @PostMapping(value = "importPopup", produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "导入弹窗提醒", httpMethod = "POST", notes = "导入弹窗提醒")
    public CommonResult<String> importPopup(@ApiParam(name="confirmImport",value="确认导入") @RequestParam Optional<Boolean> confirmImport,
                                            @ApiParam(name="cacheFileId",value="缓存的流程文件id") @RequestParam Optional<String> cacheFileId) throws Exception {
        return popupManager.importPopup(confirmImport,cacheFileId);
    }

    @RequestMapping(value="importCheck", method=RequestMethod.POST, produces={"application/json; charset=utf-8" })
    @ApiOperation(value = "导入弹窗前校验", httpMethod = "POST", notes = "导入弹窗前校验")
    public CommonResult<String> importCheck(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception {
        return popupManager.importCheck(request,response);
    }

}
