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.CashIn;
import com.ruoyi.system.mapper.CashInMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
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/cashIn")
@RestController
public class CashInController {
    @Autowired
    private CashInMapper cashInMapper;

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

    @PreAuthorize("@ss.hasPermi('finance:cashin:upload')")
    @PostMapping("/setcashInInfo/{userName}/{unitId}/{monthId}")
    @Transactional
    public AjaxResult setSalaryInfo(@RequestBody String jsona, @PathVariable String userName, @PathVariable String unitId, @PathVariable String monthId) {
        cashInMapper.SetCashInStatusInfo(userName,unitId,monthId);
//        System.out.println(jsona);
        Map<String,String> mapType = JSON.parseObject(jsona,Map.class);
        String DK = mapType.get("DK").substring(1, mapType.get("DK").length() - 1);
        for (String s : DK.split(Constants.GROUPSPLIT)) {
            String[] DKsplit = s.substring(1, s.length() - 1).split(Constants.SMALLSPLIT);
            CashIn cashIn = new CashIn();
            cashIn.setCashInDate(DKsplit[0]);
            BigDecimal cashInAmount = new BigDecimal(DKsplit[1]);
            cashIn.setCashInAmount(cashInAmount);
            cashIn.setBillId(DKsplit[2]);
            cashIn.setXwProjectId(DKsplit[3]);
            cashIn.setProjectName(DKsplit[4]);
            cashIn.setExplainInfo(DKsplit[5]);
            cashIn.setUploadPerson(DKsplit[6]);
            cashInMapper.insertOne(cashIn);
        }
        return AjaxResult.success();
    }

    @RequestMapping("/CashInDownload")
    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;
    }
}
