package thyyxxk.webserver.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.dao.his.PublicDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.service.medicalinsurance.EmrUploadService;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    private final PublicDao dao;
    private final EmrUploadService emrUploadService;

    @Autowired
    public TestController(PublicDao dao, EmrUploadService emrUploadService) {
        this.dao = dao;
        this.emrUploadService = emrUploadService;
    }

    @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);
    }
}

