package cn.hnthyy.thmz.controller;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.entity.thmz.*;
import cn.hnthyy.thmz.service.thmz.MenuService;
import cn.hnthyy.thmz.service.thmz.RoleMenuRelationService;
import cn.hnthyy.thmz.service.thmz.UserMenuRelationService;
import cn.hnthyy.thmz.service.thmz.UserRoleRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 导航控制层，负责路由功能
 */
@Controller
@Slf4j
public class NavigationController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserMenuRelationService userMenuRelationService;
    @Autowired
    private RoleMenuRelationService roleMenuRelationService;
    @Autowired
    private UserRoleRelationService userRoleRelationService;

    /**
     * 打开登录页面
     *
     * @return
     */
    @RequestMapping("/login/view")
    public String loginView() {
        return "login";
    }

    /**
     * 打开导航页面
     *
     * @return
     */
    @RequestMapping("/menu/view")
    public String menuView() {
        return "menu";
    }

    /**
     * 打开主页面
     *
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/index")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "index";
    }

    /**
     * 打开科室列表页面
     *
     * @return
     */
    @RequestMapping("/unit-code")
    public String unitCode(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/unit-code")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/unit_code";
    }

    /**
     * 打开挂号管理页面
     *
     * @return
     */
    @RequestMapping("/registration")
    public String registration(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/registration")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/registration";
    }

    /**
     * 打开挂号列表页面
     *
     * @return
     */
    @RequestMapping("/registration-list")
    public String registrationList(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/registration-list")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/registration_list";
    }

//    private List<String> getUrls(HttpServletRequest httpServletRequest) throws Exception {
//        User tokenUser = TokenUtil.getUser(httpServletRequest);
//        List<UserMenuRelation> userMenuRelationList = userMenuRelationService.queryByUserId(tokenUser.getId());
//        List<Long> menuIds = userMenuRelationList.stream().map(UserMenuRelation::getMenuId).collect(Collectors.toList());
//        if (menuIds == null || menuIds.size() == 0) {
//            throw new Exception("您没有此模块的权限，请联系管理员开通！");
//        }
//        List<Menu> menuList = menuService.queryByIds(menuIds);
//        return menuList.stream().map(Menu::getMenuUrl).collect(Collectors.toList());
//    }

    private List<String> getRoleUrls(HttpServletRequest httpServletRequest) throws Exception {
        User tokenUser = TokenUtil.getUser(httpServletRequest);
        List<UserRoleRelation> userRoleRelations = userRoleRelationService.queryByUserId(tokenUser.getId());
        Set<RoleMenuRelation> roleMenuRelationSet = new HashSet<RoleMenuRelation>();
        for (UserRoleRelation userRoleRelation : userRoleRelations) {
            List<RoleMenuRelation> list = roleMenuRelationService.queryByRoleId(userRoleRelation.getRoleId());
            roleMenuRelationSet.addAll(list);
        }
        List<Long> menuIdsNew = roleMenuRelationSet.stream().map(RoleMenuRelation::getMenuId).collect(Collectors.toList());
        List<Long> menuIds=new ArrayList<>(new HashSet(menuIdsNew));//利用Set去重多个角色相同的菜单权限
        if (menuIds == null || menuIds.size() == 0) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        List<Menu> menuList = menuService.queryByIds(menuIds);
        return menuList.stream().map(Menu::getMenuUrl).collect(Collectors.toList());
    }

    /**
     * 打开收费管理页面
     *
     * @return
     */
    @RequestMapping({"/toll-administration"})
    public String tollAdministration(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/toll-administration")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/toll_administration";
    }


//    /**
//     * 打开收费管理页面，带参数跳转
//     *
//     * @return
//     */
//    @RequestMapping({"/toll-administration/{patientId}"})
//    public String tollAdministration(@PathVariable String patientId, HttpServletRequest httpServletRequest) throws Exception {
//        List<String> urls = getUrls(httpServletRequest);
//        httpServletRequest.setAttribute("patientId",patientId);
//        if (!urls.contains("/thmz/toll-administration")) {
//            throw new Exception("您没有此模块的权限，请联系管理员开通！");
//        }
//        return "toll_administration";
//    }

    /**
     * 打开收费员基础设置页面
     *
     * @return
     */
    @RequestMapping("/sfy-config")
    public String sfyConfig(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/sfy-config")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "sfy_config";
    }

    /**
     * 打印机设置页面
     *
     * @return
     */
    @RequestMapping("/print-config")
    public String printConfig(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/print-config")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/print_config";
    }

    /**
     * 打开发票管理页面
     *
     * @return
     */
    @RequestMapping("/receipt")
    public String receipt(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/receipt")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "receipt";
    }


    /**
     * 打开日结处理页面
     *
     * @return
     */
    @RequestMapping("/daily")
    public String daily(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/daily")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/daily";
    }


    /**
     * 打开重打日结报表页面
     *
     * @return
     */
    @RequestMapping("/daily-repeat-print")
    public String dailyRepeatPrint(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/daily-repeat-print")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/daily_repeat_print";
    }

    /**
     * 日结汇总页面
     *
     * @return
     */
    @RequestMapping("/daily-collect")
    public String dailyCollect(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/daily-collect")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/daily_collect";
    }

    /**
     * 病人费用清单
     *
     * @return
     */
    @RequestMapping("/charge-list")
    public String chargeList(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/charge-list")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/charge_list";
    }


    /**
     * 退药申请
     *
     * @return
     */
    @RequestMapping("/refund-medicine")
    public String refundMedicine(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/refund-medicine")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/refund_medicine";
    }


    /**
     * 门诊收入明细
     *
     * @return
     */
    @RequestMapping("/mzsrmx")
    public String mzsrmx(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/mzsrmx")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/mzsrmx";
    }


    /**
     * 门诊号别统计
     *
     * @return
     */
    @RequestMapping("/mzhbtj")
    public String mzhbtj(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/mzhbtj")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/mzhbtj";
    }


    /**
     * 门诊应收核算报表
     *
     * @return
     */
    @RequestMapping("/bissinessReport")
    public String bissinessReport(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/bissinessReport")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/bissinessReport";
    }

    /**
     * 号表字典维护
     *
     * @return
     */
    @RequestMapping("/request")
    public String request(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/request")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/request";
    }

    /**
     * 收费员工作量统计
     *
     * @return
     */
    @RequestMapping("/cash-work-count")
    public String cashWorkCount(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/cash-work-count")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/cash_work_count";
    }


    /**
     * 菜单管理页面
     *
     * @return
     */
    @RequestMapping("/menu-manage")
    public String menuManage(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/menu-manage")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "menu_manage";
    }

    /**
     * 菜单权限设置管理页面
     *
     * @return
     */
    @RequestMapping("/user-menu-relation")
    public String userMenuRelation(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/user-menu-relation")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "user_menu_relation";
    }


    /**
     * 号表预警设置页面
     *
     * @return
     */
    @RequestMapping("/request-config")
    public String requestConfig(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/request-config")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/request-config";
    }

    /**
     * 医技预约科室设置页面
     *
     * @return
     */
    @RequestMapping("/schedule-of-medical")
    public String scheduleOfMedical(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/schedule-of-medical")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/schedule-of-medical";
    }


    /**
     * 医技预约科室预约页面
     *
     * @return
     */
    @RequestMapping("/schedule-of-medical-apply")
    public String scheduleOfMedicalApply(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/schedule-of-medical-apply")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/schedule-of-medical-apply";
    }


    /**
     * 文件上传页面
     *
     * @return
     */
    @RequestMapping("/profile-common")
    public String profileCommon(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/profile-common")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "profile_common";
    }


    /**
     * 入院登记
     *
     * @return
     */
    @RequestMapping("/hospitalized")
    public String hospitalized(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/hospitalized")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "zy/hospitalized";
    }


    /**
     * 节假日配置挂号
     *
     * @return
     */
    @RequestMapping("/request-holidays-config")
    public String requestHolidaysConfig(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/request-holidays-config")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/request_holidays_config";
    }


    /**
     * 预交金处理
     *
     * @return
     */
    @RequestMapping("/accepting")
    public String accepting(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/accepting")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "zy/accepting";
    }


    /**
     * 病人列表
     *
     * @return
     */
    @RequestMapping("/actpatient")
    public String actpatient(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/actpatient")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "zy/actpatient";

    }

    /**
     * 号别类型
     *
     * @return
     */
    @RequestMapping("/request-type")
    public String requestType(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/request-type")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/request_type";
    }

    /**
     * 收费项目字典维护
     *
     * @return
     */
    @RequestMapping("/charge-detail")
    public String chargeDetail(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/charge-detail")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/charge_detail";
    }


    /**
     * 发药提醒
     *
     * @return
     */
    @RequestMapping("/to-medicine")
    public String toMedicine(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/to-medicine")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/to-medicine";
    }


    /**
     * 手机自助缴费查询
     *
     * @return
     */
    @RequestMapping("/paid-from-zz")
    public String paidFromZz(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/paid-from-zz")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "charge/paid_from_zz";
    }

    /**
     * 数据修复工具
     *
     * @return
     */
    @RequestMapping("/data-util")
    public String dataUtil(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/data-util")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/data_util";
    }


    /**
     * 惠民活动
     *
     * @return
     */
    @RequestMapping("/discount")
    public String discount(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/discount")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "charge/discount";
    }

    /**
     * 角色管理
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/role-manage")
    public String roleManage(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/role-manage")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "role_manage";
    }

    /**
     * 用户管理
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/user-manage")
    public String userManage(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/user-manage")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "user_manage";
    }

    /**
     * 药房库字典管理
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/group-name")
    public String groupName(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/group-name")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/group_name";
    }

    /**
     * 发药窗口字典管理
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/drug-win")
    public String drugWin(HttpServletRequest httpServletRequest) throws Exception {
        List<String> urls = getRoleUrls(httpServletRequest);
        if (!urls.contains("/thmz/drug-win")) {
            throw new Exception("您没有此模块的权限，请联系管理员开通！");
        }
        return "mz/drug_win";
    }
}
