package com.ruoyi.web.controller.finance;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.CashOut;
import com.ruoyi.system.mapper.CashOutMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author Shawn
 */
@RequestMapping("/finance/cashOut")
@RestController
public class CashOutController {

    @Autowired
    private CashOutMapper cashOutMapper;


    @PreAuthorize("@ss.hasPermi('finance:cashout:upload')")
    @GetMapping("/getCashOutStatus/{userName}/{unitId}/{monthId}")
    public AjaxResult getCashOutStatus(@PathVariable String userName, @PathVariable String unitId, @PathVariable String monthId) {
        String s = cashOutMapper.getCashOutStatus(userName, unitId, monthId);
        return AjaxResult.success(s);
    }

    @PreAuthorize("@ss.hasPermi('finance:cashout:upload')")
    @PostMapping("/setcashOutInfo/{userName}/{unitId}/{monthId}")
    public AjaxResult setcashOutInfo(@RequestBody String jsona, @PathVariable String userName, @PathVariable String unitId, @PathVariable String monthId) {
//        System.out.println(jsona);
        cashOutMapper.SetCashOutStatusInfo(userName,unitId,monthId);
        Map<String,String> mapType = JSON.parseObject(jsona,Map.class);
        String FK = mapType.get("FK").substring(1, mapType.get("FK").length() - 1);
        for (String s : FK.split(Constants.GROUPSPLIT)) {
            String[] FKsplit = s.substring(1, s.length() - 1).split(Constants.SMALLSPLIT);
            CashOut cashOut = new CashOut();
            cashOut.setCashOutDate(FKsplit[0]);
            BigDecimal cashOutAmount = new BigDecimal(FKsplit[1]);
            cashOut.setCashOutAmount(cashOutAmount);
            cashOut.setAbstractInformation(FKsplit[2]);
            cashOut.setXwProjectId(FKsplit[3]);
            cashOut.setExplainInfo(FKsplit[4]);
            cashOut.setUploadPerson(FKsplit[5]);
            cashOutMapper.insertOne(cashOut);
        }
        return AjaxResult.success();
    }

    @RequestMapping("/CashOutDownload")
    public String downloadBillFile(HttpServletResponse response) {
        try {
            // path是指欲下载的文件的路径。
//            String path = "C:\\Users\\Eric\\Desktop\\新建文件夹 (2)\\模板\\付款-模板.xlsx";
            String path = "/usr/local/src/templates/付款-模板.xlsx";
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setContentType("application/json;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
