package thyyxxk.webserver.controller.examinations;

import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import thyyxxk.webserver.config.envionment.ArchiveConfig;
import thyyxxk.webserver.dao.his.examinations.InspectionsDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.yiji.IndexInquiry;
import thyyxxk.webserver.entity.yiji.YjReqIndex;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.service.yj_req.MzYjReqService;
import thyyxxk.webserver.service.yj_req.YjReqInterface;
import thyyxxk.webserver.service.yj_req.ZyYjReqService;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.List;

@RestController
@RequestMapping("/yjReq")
public class YjReqController {
    private final MzYjReqService mzYjReqService;
    private final ZyYjReqService zyYjReqService;
    private final InspectionsDao dao;
    private final UserCache userCache;
    private final ArchiveConfig archiveConfig;

    @Autowired
    public YjReqController(MzYjReqService mzYjReqService, ZyYjReqService zyYjReqService, InspectionsDao dao, UserCache userCache, ArchiveConfig archiveConfig) {
        this.mzYjReqService = mzYjReqService;
        this.zyYjReqService = zyYjReqService;
        this.dao = dao;
        this.userCache = userCache;
        this.archiveConfig = archiveConfig;
    }

    @PostMapping("/queryYjReqIndex")
    public ResultVo<List<YjReqIndex>> queryYjReqIndex(@RequestBody IndexInquiry inquiry) {
        if (inquiry.getPatNo().contains("-")) {
            return ResultVoUtil.success(mzYjReqService.queryYjReqIndex(inquiry));
        }
        return ResultVoUtil.success(zyYjReqService.queryYjReqIndex(inquiry));
    }

    @PostMapping("/uploadYjPdf")
    public ResultVo<String> uploadYjPdf(YjReqIndex index) {
        UserInfo user = userCache.getUserInfoByToken();
        index.setUid(index.getPatNo() + "_" + index.getReqNo());
        index.setStaffId(user.getCode());
        index.setStaffDept(user.getDeptCode());
        String archiveDir = archiveConfig.getPath();
        String suffixDir = "/archive/emr/" + index.getPatNo() + "/"
                + index.getTimes() + "/jianCha/" + index.getUid() + ".pdf";
        index.setArchiveDir(archiveDir + suffixDir);
        index.setSuffixDir(suffixDir);
        index.setArchiveId(IdUtil.simpleUUID());
        if (null == index.getReqDoctor()) {
            index.setReqDoctor("");
        }
        return ResultVoUtil.success(YjReqInterface.uploadPdf(index, dao));
    }
}
