package com.ruoyi.web.controller.drillDeduction;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.WordUtil;
import com.ruoyi.drill.dto.TyDrillInfoDetailsDto;
import com.ruoyi.drill.service.ITyDrillInfoDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TyDrilInfoDetailsController
 *
 * @Auther: mtx
 * @Date: 2023/09/26 15:34
 */
@RestController
@RequestMapping("/drillInfoDetails")
@Api(tags = "推演详情")
public class TyDrilInfoDetailsController extends BaseController {

    @Value("${ruoyi.profile}")
    public String url;

    @Resource
    private ITyDrillInfoDetailsService drillInfoDetailsService;

    @GetMapping("/getDetails")
    @ApiOperation("查询推演详情列表")
    public AjaxResult getDetails(String drillInfoId) {
        return drillInfoDetailsService.getDetails(drillInfoId);
    }

    @Log(title = "新增推演详情", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增推演详情")
    public void save(HttpServletResponse resp, HttpServletRequest req, @RequestBody TyDrillInfoDetailsDto drillInfoDetailsDto) throws IOException {
        drillInfoDetailsService.saveAndUpdate(drillInfoDetailsDto);

        String picUrl = drillInfoDetailsDto.getPicUrl();
        List<String> picUrls = new ArrayList<>();
        if (StringUtils.isNotEmpty(picUrl)) {
            for (String pic : picUrl.split(",")) {
                File file = new File(url + pic);
                InputStream fileImageInputStream = Files.newInputStream(file.toPath());
                byte[] bytes = IOUtils.toByteArray(fileImageInputStream);
                String encoded = Base64.getEncoder().encodeToString(bytes);
                picUrls.add(encoded);
            }
        }
        //生成结果集
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("scheme", drillInfoDetailsDto);
        dataMap.put("picUrls", picUrls);
        //设置生成文件存储路径
        String directory = "/schemeList";
        WordUtil.checkDownloadPath(directory);
        //生成文件
        WordUtil.createWord(resp, req, dataMap, this.getClass(), "/template", "ty.ftl"
                , RuoYiConfig.getDownloadPath() + directory + "/ty.docx");
        //下载后删除生成文件
        FileUtils.deleteFile(RuoYiConfig.getDownloadPath() + directory + "/ty.docx");
    }

}
