package thyyxxk.webserver.controller;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.parsing.XPathParser;
import org.apache.ibatis.scripting.xmltags.DynamicContext;
import org.apache.ibatis.scripting.xmltags.XMLScriptBuilder;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.bouncycastle.asn1.pkcs.Pfx;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.dao.his.PublicDao;
import thyyxxk.webserver.dao.his.dictionary.clinicalPathway.ClinicalPathwayDao;
import thyyxxk.webserver.dao.his.xcTest.TestDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayTemplates;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.SiLimitRequest;
import thyyxxk.webserver.http.websocket.SocketV2;
import thyyxxk.webserver.http.websocket.dto.SocketTask;
import thyyxxk.webserver.service.medicalinsurance.EmrUploadService;
import thyyxxk.webserver.service.medicalinsurance.SiChargeLimitService;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.SnowFlakeId;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.StringReader;
import java.util.*;

@RestController
@RequestMapping("/test")
@Slf4j
@RequiredArgsConstructor
public abstract class TestController {
    private final PublicDao dao;
    private final EmrUploadService emrUploadService;
    private final SiChargeLimitService siChargeLimitService;
    private final SocketV2 socketV2;
    private final ClinicalPathwayDao cdao;

    @GetMapping("/test1")
    @PassToken
    public ResultVo<String> getTest1() throws InterruptedException {
        log.info("test1循环开始:{}", DateUtil.formatDatetime(new Date()));
        for (int j = 0; j < 100; j++) {
            Thread.sleep(100);
        }
        log.info("test1循环结束:{}", DateUtil.formatDatetime(new Date()));
        return ResultVoUtil.success("test1");
    }

    @GetMapping("test2")
    @PassToken
    public ResultVo<String> getTest2() {
        log.info("test2，Dao开始:{}", DateUtil.formatDatetime(new Date()));
        dao.getDept();
        log.info("test2，Dao开始:{}", DateUtil.formatDatetime(new Date()));
        return ResultVoUtil.success("test2");
    }


    @PassToken
    @GetMapping("/test3")
    public ResultVo<String> getTest3() {
        List<Map<String, Object>> list = dao.test3();
        for (Map<String, Object> item : list) {
            log.info("item: {}", item);
            dao.test3_1(item.get("trade_no").toString(), item.get("fund_pay_sumamt").toString(), item.get("acct_pay").toString(), item.get("psn_cash_pay").toString());
        }
        return ResultVoUtil.success("OK");
    }

    @PassToken
    @GetMapping("/test4")
    public String getTest4(@RequestParam("patNo") String patNo,
                           @RequestParam("times") Integer times) {
        return emrUploadService.testUpload(patNo, times);
    }

    @PassToken
    @PostMapping("/test5")
    public String test5(@RequestBody SiLimitRequest request) {
        return siChargeLimitService.analyzeSiChargeLimit(request);
    }


    @PassToken
    @GetMapping("/test6")
    public void test6() {
        SocketTask socketTask = new SocketTask();
        socketTask.setUserCode("01897");
        socketTask.setName("费用上传(共10人，正在上传第1人)");
        socketTask.setId("测试");
        socketTask.setTextInside(true);

        for (int i = 1; i <= 10; i++) {
            ThreadUtil.sleep(500);

            socketTask.setName("费用上传(共10人，正在上传第" + i + "人)");
            socketTask.setPercentage(i * 10);

            socketV2.sendTask(socketTask);
        }
    }

    @PassToken
    @GetMapping("/test7")
    public void test7() {
        List<ClinicalPathwayTemplates> tmp = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            ClinicalPathwayTemplates build = ClinicalPathwayTemplates.builder()
                    .id(SnowFlakeId.instance().nextId())
                    .name("测试" + i)
                    .deptCode("00000")
                    .delFlag(true)
                    .build();
            tmp.add(build);
        }
        cdao.insert(tmp);
    }

}

