package com.bweb.controller;

import com.bweb.config.SysConfig;
import com.bweb.config.SysConst;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.CommonSysConst;
import com.hfkj.entity.SecVersion;
import com.hfkj.model.ResponseData;
import com.hfkj.obs.HuaWeiYunObs;
import com.hfkj.service.SecVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

@Controller
@Api(value = "系统版本")
@RequestMapping(value = "/sysVersion")
public class SecVersionController {

    private static Logger log = LoggerFactory.getLogger(SecVersionController.class);

    @Resource
    private SecVersionService secVersionService;

    @RequestMapping(value="/editVersion",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑版本")
    public ResponseData editVersion(@RequestBody SecVersion body) {
        try {

            if (body == null
                    || body.getTermType() == null
                    || body.getVersionType() == null
                    || StringUtils.isBlank(body.getVersion())
                    || StringUtils.isBlank(body.getContent())
                    || StringUtils.isBlank(body.getFileUrl())) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 版本
            SecVersion version = secVersionService.getDetailById(body.getId());
            if (version == null) {
                version = new SecVersion();

                Map<String, Object> param = new HashMap<>();
                param.put("term_type", body.getTermType());
                param.put("status", 2);
                if (secVersionService.getList(param).size() > 0) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "暂时无法创建新版本，有未发布的版本");
                }
                version.setStatus(2);
            }
            version.setTermType(body.getTermType());
            version.setVersionType(body.getVersionType());
            version.setVersion(body.getVersion());
            version.setContent(body.getContent());
            version.setFileUrl(body.getFileUrl());
            secVersionService.editData(version);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/releaseVersion",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "发布版本")
    public ResponseData releaseVersion(@RequestParam(name = "id", required = true) Integer id) {
        try {
            // 版本
            SecVersion version = secVersionService.getDetailById(id);
            if (version == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            if (!version.getStatus().equals(2)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }
            secVersionService.releaseVersion(version);
            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/delVersion",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除版本")
    public ResponseData delVersion(@RequestParam(name = "id", required = true) Integer id) {
        try {

            // 版本
            SecVersion version = secVersionService.getDetailById(id);
            if (version == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            if (!version.getStatus().equals(2)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }
            version.setStatus(0);
            secVersionService.editData(version);
            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getVersionList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询版本列表")
    public ResponseData getVersionList(@RequestParam(name = "termType", required = false) String termType,
                                       @RequestParam(name = "versionType", required = false) Integer versionType,
                                       @RequestParam(name = "status", required = false) Integer status,
                                       @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                       @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {

            Map<String, Object> param = new HashMap<>();
            param.put("termType", termType);
            param.put("versionType", versionType);
            param.put("status", status);

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(secVersionService.getList(param)));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
