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.domain.B15c;
import com.ruoyi.system.mapper.B15cMapper;
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/B15C")
@RestController
public class B15CController {

    @Autowired
    private B15cMapper b15cMapper;

    @PreAuthorize("@ss.hasPermi('finance:B15C:upload')")
    @PostMapping("/setB15CInfo/{userName}/{unitId}/{date}")
    public AjaxResult setB15CInfo(@RequestBody String jsona, @PathVariable String userName, @PathVariable String unitId, @PathVariable String date) {
//        System.out.println(jsona);
        String monthId = date.replace("-", "").substring(0, 6);
        b15cMapper.setB15CStatus(userName,unitId,monthId);
        Map<String,String> mapType = JSON.parseObject(jsona,Map.class);
        String B15C = mapType.get("B15C").substring(1, mapType.get("B15C").length() - 1);
        for (String s : B15C.split(Constants.GROUPSPLIT)) {
            String[] B15Csplit = s.substring(1, s.length() - 1).split(Constants.SMALLSPLIT);
            B15c b15c = new B15c();
            b15c.setExpenseDay(B15Csplit[0]);
            b15c.setClaimPaymentPersonOwnerUnit(B15Csplit[1]);
            b15c.setProjectOwnerUnit(B15Csplit[2]);
            b15c.setClaimPaymentPerson(B15Csplit[3]);
            b15c.setRouteStartCity(B15Csplit[4]);
            b15c.setRouteEndCity(B15Csplit[5]);
            BigDecimal transportAllowance = new BigDecimal(B15Csplit[6]);
            b15c.setTransportAllowance(transportAllowance);
            BigDecimal foodAllowance = new BigDecimal(B15Csplit[7]);
            b15c.setFoodAllowance(foodAllowance);
            BigDecimal stayAllowance = new BigDecimal(B15Csplit[8]);
            b15c.setStayAllowance(stayAllowance);
            BigDecimal highlandAllowance = new BigDecimal(B15Csplit[9]);
            b15c.setHighlandAllowance(highlandAllowance);
            BigDecimal offLineOthers = new BigDecimal(B15Csplit[10]);
            b15c.setOffLineOthers(offLineOthers);
            BigDecimal offLineBy = new BigDecimal(B15Csplit[11]);
            b15c.setOffLineBy(offLineBy);
            BigDecimal offLineHotel = new BigDecimal(B15Csplit[12]);
            b15c.setOffLineHotel(offLineHotel);
            BigDecimal offLineTotal = new BigDecimal(B15Csplit[13]);
            b15c.setOffLineTotal(offLineTotal);
            BigDecimal rentFee = new BigDecimal(B15Csplit[14]);
            b15c.setRentFee(rentFee);
            BigDecimal tmp = new BigDecimal(B15Csplit[15]);
            b15c.setUnSurveyByCarFee(tmp);
            tmp = new BigDecimal(B15Csplit[16]);
            b15c.setOfficeFee(tmp);
            tmp = new BigDecimal(B15Csplit[17]);
            b15c.setServeFee(tmp);
            tmp = new BigDecimal(B15Csplit[18]);
            b15c.setEntryExamination(tmp);
            tmp = new BigDecimal(B15Csplit[19]);
            b15c.setWaterAndEleFee(tmp);
            tmp = new BigDecimal(B15Csplit[20]);
            b15c.setOutsourceCost(tmp);
            tmp = new BigDecimal(B15Csplit[21]);
            b15c.setLiveCountyAllowance(tmp);
            tmp = new BigDecimal(B15Csplit[22]);
            b15c.setBuySomething(tmp);

            tmp = new BigDecimal(B15Csplit[23]);
            b15c.setTotal(tmp);

            b15c.setExplainInfo(B15Csplit[24]);
            b15c.setPrincipal(B15Csplit[25]);
            b15c.setB15UploadTime(B15Csplit[26]);
            b15c.setDepartment(B15Csplit[27]);
            b15c.setUploadIsSuccessToday(B15Csplit[28]);
            b15c.setUploadIsQualifiedToday(B15Csplit[29]);
            b15c.setFailedReason(B15Csplit[30]);
            b15c.setVideoShowCity(B15Csplit[31]);
            b15c.setTransportAllowanceIsQualified(B15Csplit[32]);
            b15c.setFoodAllowanceIsQualified(B15Csplit[33]);
            b15c.setStayAllowanceIsQualified(B15Csplit[34]);
            b15c.setHighLandAllowanceIsQualified(B15Csplit[35]);
            b15c.setOffLineHotelQualified(B15Csplit[36]);
            b15c.setStatus(B15Csplit[37]);
            b15c.setPersonLiability(B15Csplit[38]);
            b15c.setPersonBeFinedMoney(B15Csplit[39]);
            b15c.setOffLineAllowanceCutPayment(B15Csplit[40]);
            b15c.setAppealContent(B15Csplit[41]);
            b15c.setAppealReply(B15Csplit[42]);
            b15c.setVerifyTime(B15Csplit[43]);
            b15c.setVerifier(B15Csplit[44]);
            b15c.setOnLineAirplane(B15Csplit[45]);
            b15c.setOnLineTrain(B15Csplit[46]);
            b15c.setOnLineHotel(B15Csplit[47]);
            b15c.setOnLineAirplaneRefund(B15Csplit[48]);
            b15c.setOnLineTrainRefund(B15Csplit[49]);
            b15c.setOnLineHotelRefund(B15Csplit[50]);
            b15c.setUploadPerson(B15Csplit[51]);
//            System.out.println(b15c.toString());
            b15cMapper.insertOne(b15c);
        }
        return AjaxResult.success();
    }

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