package thyyxxk.webserver.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.publicapi.ZkList;
import thyyxxk.webserver.entity.zhuyuanyisheng.PatientAllergenInfo;
import thyyxxk.webserver.service.LoginService;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import javax.naming.Context;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 描述： 公共接口
 * </p>
 *
 * @author xc
 * @date 2021-09-17 10:53
 */
@RestController
@RequestMapping("/publicApi")
@PassToken
@Slf4j
public class PublicController {
    private final PublicServer service;

    public PublicController(PublicServer service) {
        this.service = service;
    }

    @GetMapping("/getRoleCode")
    public ResultVo<List<Integer>> getRoleCode() {
        return service.getRoleCode();
    }


    @GetMapping("/getRenYuan")
    public ResultVo<List<GetDropdownBox>> getRenYuan(@RequestParam("code") String code) {
        return service.getRenYuan(code);
    }

    @GetMapping("/getChargeCode")
    public ResultVo<List<GetDropdownBox>> getChargeCode(@RequestParam("pyCode") String pyCode) {
        return service.getChargeCode(pyCode);
    }

    @GetMapping("/getDept")
    public ResultVo<List<GetDropdownBox>> getDept() {
        return service.getDept();
    }

    @GetMapping("/maZuiFangShi")
    public ResultVo<List<GetDropdownBox>> maZuiFangShi() {
        return service.maZuiFangShi();
    }

    @GetMapping("/getDate")
    @PassToken
    public ResultVo<Date> getDate() {
        return ResultVoUtil.success(new Date());
    }


    @PostMapping("/yaoPinXiangMuPiPeiYiBao")
    public ResultVo<String> yaoPinXiangMuPiPeiYiBao(@RequestBody List<String> listCode) {
        List<String> xm = new ArrayList<>();
        List<String> yp = new ArrayList<>();
        for (String item : listCode) {
            String[] it = item.split("-");
            if (it[1].equals("00")) {
                xm.add(it[0].trim());
            } else {
                yp.add(it[0].trim() + it[1].trim());
            }
        }
        String ypTiShi = ListUtil.isBlank(yp) ? "" : service.yaoPinXianZhi(yp, false);
        String xmTiShi = ListUtil.isBlank(xm) ? "" : service.xiangMuXianZhi(xm, false);
        return ResultVoUtil.success(ypTiShi + xmTiShi);
    }

    @GetMapping("/huoQuYuanGongXinXi")
    public ResultVo<UserInfo> huoQuYuanGongXinXi(@RequestParam("code") String code) {
        return ResultVoUtil.success(service.huoQuYuanGongXinXi(code));
    }

    @PassToken
    @GetMapping("/makePyCode")
    public ResultVo<String> makePyCode(@RequestParam("tableName") String tableName,
                                       @RequestParam("idColumn") String idColumn,
                                       @RequestParam("nameColumn") String nameColumn,
                                       @RequestParam("pyColumn") String pyColumn) {
        return service.makePyCode(tableName, idColumn, nameColumn, pyColumn);
    }

    @PassToken
    @GetMapping("/getAllergens")
    public ResultVo<List<GetDropdownBox>> getAllergens(@RequestParam("name") String name) {
        return service.getAllergens(name);
    }

    @GetMapping("/getPatientAllergens")
    public ResultVo<List<PatientAllergenInfo>> getPatientAllergens(@RequestParam("patNo") String patNo) {
        return service.getPatientAllergens(patNo);
    }

    @GetMapping("/whetherThePatientHasAllergens")
    public ResultVo<Boolean> whetherThePatientHasAllergens(@RequestParam("patNo") String PatNo) {
        return service.whetherThePatientHasAllergens(PatNo);
    }

    @GetMapping("/newPatientAllergens")
    public ResultVo<Integer> newPatientAllergens(@RequestParam("patNo") String patNo,
                                                 @RequestParam("allergenCode") String allergenCode,
                                                 @RequestParam("allergenType") int allergenType) {
        return service.newPatientAllergens(patNo, allergenCode, allergenType);
    }

    @GetMapping("/removePatientAllergens")
    public ResultVo<String> removePatientAllergens(@RequestParam("id") Integer id) {
        return service.removePatientAllergens(id);
    }

    @GetMapping("/getDrugInfo")
    public ResultVo<List<GetDropdownBox>> getDrugInfo(@RequestParam("name") String name) {
        return service.getDrugInfo(name);
    }

    @GetMapping("/getBldCat")
    public ResultVo<List<GetDropdownBox>> getBldCat(@RequestParam("name") String name) {
        return service.getBldCat(name);
    }

    @GetMapping("/getTheTransferList")
    public ResultVo<List<ZkList>> getTheTransferList() {
        return service.getTheTransferList();
    }

    @GetMapping("/getPeopleToFillInInformation")
    public ResultVo<Map<String, Object>> getPeopleToFillInInformation() throws InvocationTargetException, IllegalAccessException {
        return service.getPeopleToFillInInformation();
    }

    @GetMapping("/idVerification")
    public ResultVo<Boolean> idVerification(@RequestParam("idCard") String idCard) {
        return service.idVerification(idCard);
    }

}
