package thyyxxk.webserver.service.medicalinsurance.costcontrol;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.medicalinsurance.costcontrol.YbUtilDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName YbUtilService
 * @Description 医保报表统计工具类
 * @Author hsh
 * @Date 2022/8/9 16:24
 **/
@Service
public class YbUtilService {
    private final YbUtilDao dao;
    private final UserCache userCache;

    @Autowired
    public YbUtilService(YbUtilDao dao, UserCache userCache) {
        this.dao = dao;
        this.userCache = userCache;
    }

    public ResultVo<List<Map<String, Object>>> selectSmallDept(Map<String, String> map) {
        String str = map.get("str");
        List<Map<String, Object>> list = dao.selectSmallDept(str);
        return ResultVoUtil.success(list);
    }

    public ResultVo<List<Map<String, Object>>> selectDoctor(Map<String, String> map) {
        String str = map.get("str");
        List<Map<String, Object>> list = dao.selectDoctor(str);
        return ResultVoUtil.success(list);
    }

    public ResultVo<List<Map<String, Object>>> selectAllSmallDept(Map<String, String> map) {
        String str = map.get("str");
        List<Map<String, Object>> list = dao.selectAllSmallDept(str);
        return ResultVoUtil.success(list);
    }

    public ResultVo<List<Map<String, Object>>> selectAllDoctor(Map<String, String> map) {
        String str = map.get("str");
        String dept = map.get("dept");
        List<Map<String, Object>> list = dao.selectAllDoctor(str, dept);
        return ResultVoUtil.success(list);
    }

    /**
     * @description: 查询所有科室
     * @author: hsh
     * @date: 2023/7/26 10:31
     * @param: [map]
     * @return: ResultVo<List<Map<String,Object>>>
     **/
    public ResultVo<List<Map<String, Object>>> selectAllDept(Map<String, String> map) {
        String str = map.get("str");
        List<Map<String, Object>> list = dao.selectAllDept(str);
        return ResultVoUtil.success(list);
    }

    /**
     * @description: 查询所有号别
     * @author: hsh
     * @date: 2023/8/3 15:32
     * @param: [map]
     * @return: ResultVo<List<Map<String,Object>>>
     **/
    public ResultVo<List<Map<String, Object>>> selectAllRegLevel(Map<String, String> map) {
        String str = map.get("str");
        List<Map<String, Object>> list = dao.selectAllRegLevel(str);
        return ResultVoUtil.success(list);
    }

    public ResultVo<List<Map<String, Object>>> selectQxSmallDept(Map<String, String> map) {
        String str = map.get("str");
        String code = map.get("code");
        List<Map<String, Object>> list;
        List<String> deptQx = new ArrayList<>();
        UserInfo user = userCache.get(TokenUtil.getInstance().getTokenUserId());
        String s = dao.selectDeptYbbQxByRole(code);
        List<Integer> roles = user.getRoles();
        boolean index = true;
        for(Integer role : roles){
            String[] st = s.split(",");
            for(String qx : st){
                String bs = String.valueOf(role);
                if(qx.equals(bs)){
                    index = false;
                    break;
                }
            }
            if(!index){
                break;
            }
        }
        if(index){
            // 需要增加科室权限限制
            deptQx.add(user.getDeptCode());
            if(null != user.getPartTimeDept() && !user.getPartTimeDept().isEmpty()){
                deptQx.addAll(user.getPartTimeDept());
            }
            list = dao.selectSmallDeptByCode(deptQx);
        } else {
            list = dao.selectSmallDept(str);
        }
        return ResultVoUtil.success(list);
    }

}
