package com.css.txw.mhzc.controller;

import cn.hutool.core.util.IdUtil;
import com.css.ggzc.framework.cache.utils.CacheUtils;
import com.css.ggzc.framework.common.pojo.CommonResult;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.ggzc.framework.file.utils.GgzcFileUtils;
import com.css.ggzc.framework.session.SessionUtils;
import com.css.txw.mhzc.constants.ErrorCodeConstants;
import com.css.txw.mhzc.pojo.QyrzReqDTO;
import com.css.txw.mhzc.pojo.vo.*;
import com.css.txw.mhzc.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Tag(name = "企业信息接口")
@RestController
@RequestMapping("/sy")
@Validated
@Slf4j
public class SyController {

    @Resource
    private TxwMhzcZxxxbService zxxxbService;

    @Resource
    private TxwMhzcSyxxbService syxxbService;

    @Resource
    private TxwMhzcGglbService gglbService;

    @PostMapping("/zxxx")
    @Operation(summary = "资讯",description = "企业认证")
    public CommonResult<Map<String, List<SyzxxxVO>>> zxxx(){
        return CommonResult.success(zxxxbService.zxxx());
    }

    @GetMapping("/sjkb")
    @Operation(summary = "数据看板",description = "数据看板")
    public CommonResult<String> sjkb(){
        return CommonResult.success(CacheUtils.dm2mc("cs_ggzc_xtcs","sjkb"));
    }

    @GetMapping("/hzhb")
    @Operation(summary = "合作伙伴",description = "合作伙伴")
    public CommonResult<String> hzhb(){
        return CommonResult.success(CacheUtils.dm2mc("cs_ggzc_xtcs","hzhb"));
    }

    @GetMapping("/getCdxx")
    @Operation(summary = "查询菜单",description = "查询菜单")
    public CommonResult<Map<String,Object>> getCdxx(){
        return CommonResult.success(syxxbService.getCdxx());
    }

    @PostMapping("/saveGgxx")
    @Operation(summary = "保存广告信息",description = "保存广告信息")
    public CommonResult<String> saveGgxx(@RequestBody GglbVO gglbVO){
        return CommonResult.success(gglbService.saveGgxx(gglbVO));
    }

    @PostMapping("/getGgxxList")
    @Operation(summary = "查询广告栏",description = "查询广告栏")
    public CommonResult<List<GglbVO>> getGgxxList(@RequestBody GglbReqVO reqVO){
        return CommonResult.success(gglbService.getGgxxList(reqVO));
    }

    @GetMapping("/delGgxx")
    @Operation(summary = "删除广告栏",description = "删除广告栏")
    public CommonResult<String> delGgxx(@RequestParam("uuid") @NotBlank String uuid){
        return CommonResult.success(gglbService.delGgxx(uuid));
    }

//    @PostMapping("/uploadFile")
    @PostMapping(value = {"/uploadFile"}, consumes = {"multipart/form-data"})
    @Operation(summary = "上传头像url")
    public CommonResult<String> uploadFile(@RequestBody MultipartFile file,                                                                                                                                                                                                                                                                                                                                                                                                                              @RequestParam(required = false) String delid) throws IOException {
        if (!GyUtils.isNull(delid)) GgzcFileUtils.delete(delid);
        String name = IdUtil.fastSimpleUUID();
        GgzcFileUtils.upload(file.getBytes(), name,  file.getContentType());
        return CommonResult.success(name);
    }

    @GetMapping("/delFile")
    @Operation(summary = "获取文件")
    public CommonResult<String> delFile(@RequestParam @NotBlank String delid) {
        GgzcFileUtils.delete(delid);
        return CommonResult.success("success");
    }

    @GetMapping("/getResources")
    @Operation(summary = "获取文件")
    public CommonResult<String> getResources(HttpServletResponse response, @RequestParam @NotBlank String url) throws Exception {
        byte[] content = GgzcFileUtils.getContent(url);
        if (GyUtils.isNull(content)){
            log.info("获取图片为空,id:{}",url);
            return CommonResult.success("");
        }
        InputStream inputStream = new ByteArrayInputStream(content);
        byte[] buffer = new byte[16384];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            response.getOutputStream().write(buffer, 0, bytesRead);
        }
        buffer = null;

        response.flushBuffer();
        inputStream.close();
        return CommonResult.success("success");
    }
}
