package com.saye.hospitalgd.controller;


import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.service.BankbillsHistoryService;
import com.saye.hospitalgd.service.HisbillsHistoryService;
import com.saye.hospitalgd.service.HisbillsOriginalService;
import com.saye.hospitalgd.util.CSVUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： Mr.zs
 * @date： 2022/12/2
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("/receiveData")
public class ReceiveDataController {

    @Autowired
    private BankbillsHistoryService bankbillsHistoryService;

    @Autowired
    private HisbillsHistoryService hisbillsHistoryService;

    @Autowired
    private HisbillsOriginalService hisbillsOriginalService;

//    @Autowired
//    private HisInterfaceCountService hisInterfaceCountService;

    @PostMapping("/receiveHisData")
    @ResponseBody
    public Map<Object, Object> receiveHisData(@RequestBody HashMap<Object, Object> hisbillsOriginal) {

        log.info("接收到数据：" + hisbillsOriginal);

        Map<Object, Object> responseMap = new HashMap<>();

        try {
            //保存一份最初的
            hisbillsOriginalService.saveHisOriginalData(hisbillsOriginal);

            //保存一份历史的
            hisbillsHistoryService.saveHisHistoryData(hisbillsOriginal);

            log.info("his数据保存完毕");


//            //查看数据库今日接口统计是否记录
//            int i = hisInterfaceCountService.searchToday();
//            if (i > 0) {
//                hisInterfaceCountService.updateConut();
//            } else {
//                hisInterfaceCountService.insertCount();
//            }
            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            //记录接口调用情况

        } catch (Exception e) {
            log.error("his数据保存失败，原因：" + e.getMessage());
            responseMap.put("code", 999);
            responseMap.put("msg", e.getMessage());
        }

        return responseMap;
    }

    @PostMapping("/receiveBankData")
    @ResponseBody
    public Map<Object, Object> receiveHisData(@RequestBody MultipartFile spareFile) {
        String errCode = "0";
        String errMsg = "";
        List<HashMap<Object, Object>> savList = new ArrayList<>();
        //校验文件名是否正确
        String fileName = spareFile.getOriginalFilename();
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");

        String substring = fileName.substring(0, 8);
        boolean matches = pattern.matcher(substring).matches();
        log.info("match is :" + matches);
        if (matches) {
            log.info("切割出来的字符串是" + substring);
            String year = substring.substring(0, 4);
            String month = substring.substring(4, 6);
            String day = substring.substring(6, 8);
            String rq = year + "-" + month + "-" + day;
            log.info("日期是： " + rq);
        } else {
            errCode = "999";
            errMsg = "文件名不合法";
        }


        if (!spareFile.isEmpty()) {

            List<String[]> list = null;
            try {
                InputStream inputStream = spareFile.getInputStream();
                list = CSVUtils.getCsvData(inputStream);
            } catch (IOException e) {
                errCode = "999";
                errMsg = "获取文件失败";
                e.printStackTrace();
            }

            if (list.size() != 0) {

                for (int i = 5; i < list.size() - 2; i++) {
                    String[] s = list.get(i);
                    //遍历去除读取文件时字段前面读取的等号
                    for (int i1 = 0; i1 < s.length; i1++) {
                        if (s[i1].contains("=")) {
                            s[i1] = StringUtils.substringAfter(s[i1], "=");
                        }

                        if (s[i1].contains("\"")) {
                            s[i1] = s[i1].replace("\"", "");
                        }
                    }
                    HashMap<Object, Object> resultMap = new HashMap<>();
                    resultMap.put("I_ZDBH", s[1]);
                    resultMap.put("I_DSFZFMC", s[2]);
                    resultMap.put("I_JYLX", s[3]);
                    resultMap.put("I_DSFDDH", s[4]);
                    resultMap.put("I_ZFFS", s[5]);
                    resultMap.put("I_SPMC", s[6]);
                    resultMap.put("I_DDJE", s[7]);
                    resultMap.put("I_JYSJ", s[8]);
                    String[] s1 = s[8].split(" ");
                    resultMap.put("I_JYRQ", s1[0]);
                    resultMap.put("I_HBZL", s[9]);
                    resultMap.put("I_XFJE", s[10]);
                    resultMap.put("I_THJE", s[11]);
                    resultMap.put("I_GHDDH", s[12]);
                    resultMap.put("I_WBDDH", s[13]);
                    resultMap.put("I_XFSXFJE", s[14]);
                    resultMap.put("I_THSXFJE", s[15]);
                    resultMap.put("I_RZJE", s[16]);
                    resultMap.put("I_FKYH", s[18]);
                    resultMap.put("I_YHBS", s[19]);
                    savList.add(resultMap);
//                    log.info("接收到的数据是： " + resultMap);
                }
//                int i = bankbillsHistoryService.saveOriginalData(savList);
//                int o = bankbillsHistoryService.saveHistoryData(savList);
            } else {
                errCode = "999";
                errMsg = "读取失败";
            }
//            log.info("接收到数据：" + spareFile.getOriginalFilename());
        }
        Map<Object, Object> responseMap = new HashMap<>();
        responseMap.put("errCode", errCode);
        responseMap.put("errMsg", errMsg);
        return responseMap;
    }

}
