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.B14c;
import com.ruoyi.system.mapper.B14cMapper;
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.util.Map;

/**
 * @author Shawn
 */
@RequestMapping("/finance/B14C")
@RestController
public class B14CController {

    @Autowired
    private B14cMapper b14cMapper;

    @PreAuthorize("@ss.hasPermi('finance:B14C:upload')")
    @PostMapping("/setB14CInfo/{userName}/{unitId}/{date}")
    public AjaxResult setB14CInfo(@RequestBody String jsona, @PathVariable String userName, @PathVariable String unitId, @PathVariable String date) {
//        System.out.println(jsona);
        String monthId = date.replace("-", "").substring(0, 6);
        b14cMapper.setB14CStatus(userName,unitId,monthId);
        Map<String,String> mapType = JSON.parseObject(jsona,Map.class);
        String B14C = mapType.get("B14C").substring(1, mapType.get("B14C").length() - 1);
        for (String s : B14C.split(Constants.GROUPSPLIT)) {
            String[] B14Csplit = s.substring(1, s.length() - 1).split(Constants.SMALLSPLIT);
            B14c b14c = new B14c();
            b14c.setSurveyPlotDay(B14Csplit[0]);
            b14c.setCounty(B14Csplit[1]);
            b14c.setSurveySectionStationAndMachineRoom(B14Csplit[2]);
            b14c.setSurveyPerson(B14Csplit[3]);
            b14c.setSurveyOwnerUnit(B14Csplit[4]);
            b14c.setProjectOwnerUnit(B14Csplit[5]);
            b14c.setTransportWay(B14Csplit[6]);
            b14c.setCarType(B14Csplit[7]);
            b14c.setTodayKil(Double.parseDouble(B14Csplit[8]));
            b14c.setCarCharge(Double.parseDouble(B14Csplit[9]));
            b14c.setDirectFuelCharge(Double.parseDouble(B14Csplit[10]));
            b14c.setClearingOilCharge(Double.parseDouble(B14Csplit[11]));
            b14c.setParkingCharge(Double.parseDouble(B14Csplit[12]));
            b14c.setBridgeCharge(Double.parseDouble(B14Csplit[13]));
            b14c.setCarFare(Double.parseDouble(B14Csplit[14]));
            b14c.setBreakfastBonus(Double.parseDouble(B14Csplit[15]));
            b14c.setLunchBonus(Double.parseDouble(B14Csplit[16]));
            b14c.setDinnerBonus(Double.parseDouble(B14Csplit[17]));
            b14c.setMealBonusAmount(Double.parseDouble(B14Csplit[18]));
            b14c.setOtherUseCardAndMealFee(Double.parseDouble(B14Csplit[19]));
            b14c.setPlotKil(Double.parseDouble(B14Csplit[20]));
            b14c.setPlotPrice(Double.parseDouble(B14Csplit[21]));
            b14c.setPlotAmount(Double.parseDouble(B14Csplit[22]));
            b14c.setTotal(Double.parseDouble(B14Csplit[23]));
            b14c.setTaxRate(Double.parseDouble(B14Csplit[24]));
            b14c.setTotalInTax(Double.parseDouble(B14Csplit[25]));
            b14c.setDinePeopleNameAndPhone(B14Csplit[26]);
            b14c.setExplainInfo(B14Csplit[27]);
            b14c.setPrincipal(B14Csplit[28]);
            b14c.setB14UploadTime(B14Csplit[29]);
            b14c.setStartKilTime(B14Csplit[30]);
            b14c.setEndKilTime(B14Csplit[31]);
            b14c.setCarBodyAndPlateNumberIsPassed(B14Csplit[32]);
            b14c.setStartKilIsPassed(B14Csplit[33]);
            b14c.setEndKilIsPassed(B14Csplit[34]);
            b14c.setQualifiedCount(B14Csplit[35]);
            b14c.setTaximeterIsPassed(B14Csplit[36]);
            b14c.setFuelAndPassAndBridgeIsPassed(B14Csplit[37]);
            b14c.setOthersUseCardAndMealFeeIsPassed(B14Csplit[38]);
            b14c.setPaymentScreenshotIsPassed(B14Csplit[39]);
            b14c.setPlotIsPassed(B14Csplit[40]);
            b14c.setStatus(B14Csplit[41]);
            b14c.setSurveyBeFinedMoney(Double.parseDouble(B14Csplit[42]));
            b14c.setUnitPrincipleBeFinedMoney(Double.parseDouble(B14Csplit[43]));
            b14c.setVerifyMoney(Double.parseDouble(B14Csplit[44]));
            b14c.setVerifyMoneyIncludeTax(Double.parseDouble(B14Csplit[45]));
            b14c.setAppealContent(B14Csplit[46]);
            b14c.setAppealReply(B14Csplit[47]);
            b14c.setVerifyTime(B14Csplit[48]);
            b14c.setVerifier(B14Csplit[49]);
            b14c.setUploadPerson(B14Csplit[50]);
//            System.out.println(b14c.toString());
            b14cMapper.insertOne(b14c);
        }
        return AjaxResult.success();
    }

    @RequestMapping("/B14CDownload")
    public String downloadBillFile(HttpServletResponse response) {
        try {
            // path是指欲下载的文件的路径。
//            String path = "C:\\Users\\Eric\\Desktop\\新建文件夹 (2)\\模板\\每月B14C-模板.xlsx";
            String path = "/usr/local/src/templates/每月B14C-模板.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;
    }


}
