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.B16;
import com.ruoyi.system.domain.B17;
import com.ruoyi.system.domain.B18;
import com.ruoyi.system.domain.B19;
import com.ruoyi.system.mapper.B16ToB19Mapper;
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.sql.SQLException;
import java.util.Map;

/**
 * @author Shawn
 */
@RequestMapping("/finance/B16ToB19")
@RestController
public class B16ToB19Controller {

    @Autowired
    private B16ToB19Mapper b16ToB19Mapper;

    @PreAuthorize("@ss.hasPermi('finance:B16_B19:upload')")
    @PostMapping("/setB16ToB19Info/{userName}/{unitId}/{date}")
    @Transactional
    public AjaxResult setB16ToB19Info(@RequestBody String jsona, @PathVariable String userName, @PathVariable String unitId, @PathVariable String date) {
//        System.out.println(jsona);
        String monthId = date.replace("-", "").substring(0, 6);
        b16ToB19Mapper.SetB16ToB19StatusInfo(userName,unitId,monthId);
        Map<String,String> mapType = JSON.parseObject(jsona,Map.class);

        for (Object obj : mapType.keySet()) {
//            System.out.println("key为：" + obj + "   " + "值为：" + mapType.get(obj));
            if(obj == "B16"){
                String B16 = mapType.get("B16").substring(1, mapType.get("B16").length() - 1);
                for (String s : B16.split(Constants.GROUPSPLIT)) {
                    String[] B16split = s.substring(1, s.length() - 1).split(Constants.SMALLSPLIT);
                    B16 b16 = new B16();
                    b16.setOrderId(B16split[0]);
                    b16.setPreOrderDate(B16split[1]);
                    b16.setPreOrderPerson(B16split[2]);
                    b16.setUnitId(B16split[3]);
                    b16.setDepartureDate(B16split[4]);
                    b16.setDepartureCity(B16split[5]);
                    b16.setArrivalCity(B16split[6]);
                    b16.setFeeType(B16split[7]);
                    BigDecimal settlementAmount = new BigDecimal(B16split[8]);
                    b16.setSettlementAmount(settlementAmount);
                    b16.setPart(B16split[9]);
                    b16.setUploadPerson(B16split[10]);
//                    System.out.println(b16.toString());
                    b16ToB19Mapper.insertB16One(b16);
                }

            }//b16

            if(obj == "B17"){
                String B17 = mapType.get("B17").substring(1, mapType.get("B17").length() - 1);
                for (String s : B17.split(Constants.GROUPSPLIT)) {
                    String[] B17split = s.substring(1, s.length() - 1).split(Constants.SMALLSPLIT);
                    B17 b17 = new B17();
                    b17.setOrderId(B17split[0]);
                    b17.setPreOrderDate(B17split[1]);
                    b17.setPreOrderPerson(B17split[2]);
                    b17.setUnitId(B17split[3]);
                    b17.setCheckInDate(B17split[4]);
                    b17.setCheckInCity(B17split[5]);
                    b17.setHotelName(B17split[6]);
                    b17.setNightNumber(Integer.parseInt(B17split[7]));
                    BigDecimal settlementAmount = new BigDecimal(B17split[8]);
                    b17.setSettlementAmount(settlementAmount);
                    b17.setPart(B17split[9]);
                    b17.setUploadPerson(B17split[10]);
//                    System.out.println(b17.toString());
                    b16ToB19Mapper.insertB17One(b17);
                }
            }//b17

            if(obj == "B18"){
                String B18 = mapType.get("B18").substring(1, mapType.get("B18").length() - 1);
                for (String s : B18.split(Constants.GROUPSPLIT)) {
                    String[] B18split = s.substring(1, s.length() - 1).split(Constants.SMALLSPLIT);
                    B18 b18 = new B18();
                    b18.setOrderId(B18split[0]);
                    b18.setPreOrderDate(B18split[1]);
                    b18.setPreOrderPerson(B18split[2]);
                    b18.setUnitId(B18split[3]);
                    b18.setDepartureTime(B18split[4]);
                    b18.setDepartureStation(B18split[5]);
                    b18.setArrivalStation(B18split[6]);
                    b18.setFeeType(B18split[7]);
                    BigDecimal settlementAmount = new BigDecimal(B18split[8]);
                    b18.setSettlementAmount(settlementAmount);
                    b18.setPart(B18split[9]);
                    b18.setUploadPerson(B18split[10]);
//                    System.out.println(b18.toString());
                    b16ToB19Mapper.insertB18One(b18);
                }
            }//b18

            if(obj == "B19"){
                String B19 = mapType.get("B19").substring(1, mapType.get("B19").length() - 1);
                for (String s : B19.split(Constants.GROUPSPLIT)) {
                    String[] B19split = s.substring(1, s.length() - 1).split(Constants.SMALLSPLIT);
                    B19 b19 = new B19();
                    b19.setOrderId(B19split[0]);
                    b19.setPreOrderDate(B19split[1]);
                    b19.setPreOrderPerson(B19split[2]);
                    b19.setUnitId(B19split[3]);
                    b19.setConsumeType(B19split[4]);
                    BigDecimal fuDouAmount = new BigDecimal(B19split[5]);
                    b19.setFuDouAmount(fuDouAmount);
                    b19.setPart(B19split[6]);
                    b19.setUploadPerson(B19split[7]);
//                    System.out.println(b19.toString());
                    b16ToB19Mapper.insertB19One(b19);
                }
            }//b19
        }
        return AjaxResult.success();
    }

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