package thyyxxk.webserver.controller;

import cn.hutool.core.net.NetUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.login.UserInfoSelect;
import thyyxxk.webserver.entity.publicapi.ItemDrug;
import thyyxxk.webserver.entity.publicapi.ZkList;
import thyyxxk.webserver.entity.zhuyuanyisheng.PatientAllergenInfo;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.SnowFlakeId;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <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, @RequestParam("deptCode") String deptCode) {
        return service.getRenYuan(code, deptCode);
    }

    @GetMapping("/getRenYuanPage")
    public ResultVo<IPage<GetDropdownBox>> getRenYuan(@RequestParam("code") String code, @RequestParam("curPage") Integer curPage) {
        IPage<GetDropdownBox> page = new Page<>();
        ResultVo<List<GetDropdownBox>> renYuan = service.getRenYuan(code, "");
        List<GetDropdownBox> pageData = renYuan.getData().stream().skip((curPage - 1) * 10)
                .limit(10)
                .collect(Collectors.toList());
        page.setRecords(pageData);
        page.setTotal(renYuan.getData().size());
        return ResultVoUtil.success(page);
    }


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

    /**
     * @description:查询有住院的科室
     * @author: lihong
     * @date: 2022/12/26 16:57
     * @return: thyyxxk.webserver.entity.ResultVo<java.util.List < thyyxxk.webserver.entity.datamodify.GetDropdownBox>>
     **/
    @GetMapping("/getZhuYuanDept")
    public ResultVo<List<GetDropdownBox>> getZhuYuanDept() {
        return service.getZhuYuanDept();
    }

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

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

    @PassToken
    @GetMapping("/getUuid")
    public ResultVo<String> getUuid() {
        return ResultVoUtil.success(SnowFlakeId.instance().nextId());
    }

    @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 ("00".equals(it[1])) {
                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("/makeDCode")
    public ResultVo<String> makeDCode(@RequestParam("tableName") String tableName, @RequestParam("idColumn") String idColumn, @RequestParam("nameColumn") String nameColumn, @RequestParam("dColumn") String dColumn) {
        return service.makeDCode(tableName, idColumn, nameColumn, dColumn);
    }

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

    @GetMapping("/getOperationGuide")
    @PassToken

    public ResultVo<String> getOperationGuide(@RequestParam("folderName") String folderName) {
        String path = "D:\\a.snapshot\\download\\操作指南\\" + folderName;
        File folder = new File(path);
        String[] docFiles = folder.list(new MyExtFilter("docx"));
        if (docFiles == null) {
            return ResultVoUtil.success("v1.1.0");
        }
        for (String item : docFiles) {
            int start = item.indexOf("v");
            int end = item.indexOf(".docx");
            return ResultVoUtil.success(item.substring(start, end));
        }
        return ResultVoUtil.success("v1.1.0");
    }

    static class MyExtFilter implements FilenameFilter {
        private final String ext;

        MyExtFilter(String ext) {
            this.ext = ext;
        }

        public boolean accept(File dir, String name) {
            return name.endsWith(ext);
        }
    }

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

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

    @GetMapping("/getUserConfigByCode")
    @PassToken
    public ResultVo<JSONObject> getUserConfigByCode(@RequestParam("code") String code) {
        return service.getUserConfigByCode(code);
    }

    @PostMapping("/setUserConfigByCode")
    @PassToken
    public ResultVo<String> setUserConfigByCode(@RequestBody JSONObject config) {
        return service.setUserConfigByCode(config);
    }

    @PassToken
    @GetMapping("/refreshUserInfo")
    public ResultVo<String> refreshUserInfo(@RequestParam("code") String code) {
        return service.refreshUserInfo(code);
    }


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

    @PassToken
    @GetMapping("/getIp")
    public ResultVo<LinkedHashSet<String>> getIp() {
        return ResultVoUtil.success(NetUtil.localIpv4s());
    }

}
