package com.gic.haoban.manage.web.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.right.RightMenuDTO;
import com.gic.clerk.api.service.RightService;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;

/**
 * 
* @ClassName: RightController
* @Description: 好办权限
* @author xugh
* @date 2022年4月26日 下午4:59:35
*
 */
@RestController
public class RightController extends WebBaseController {

    private static final Logger log = LogManager.getLogger(RightController.class);

    @Autowired
    private RightService rightService;

    @RequestMapping("get-haoban-menu")
    public HaobanResponse getHaobanMenu(HttpServletRequest request) {
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        log.info(JSON.toJSONString(login));
        String enterpriseId = login.getEnterpriseId() ;
        String clerkId = login.getClerkId() ;
        if(StringUtils.isBlank(enterpriseId)){
            return resultResponse(HaoBanErrCode.ERR_OTHER, "gic商户为空");
        }
        log.info("好办菜单={},{}", enterpriseId, clerkId);
        if(StringUtils.isEmpty(clerkId)) {
            return resultResponse(HaoBanErrCode.ERR_OTHER, "clerkId为空");
        }
        ServiceResponse<List<RightMenuDTO>> resp = this.rightService.getHaobanMenu(enterpriseId, clerkId);
        if (resp.isSuccess()) {
            return resultResponse(HaoBanErrCode.ERR_1, resp.getResult());
        }
        return resultResponse(HaoBanErrCode.ERR_OTHER, resp.getMessage());
    }
    
    @RequestMapping("get-haoban-right")
    public HaobanResponse listHaobanAllMenu(HttpServletRequest request) {
        WebLoginDTO login = AuthWebRequestUtil.getLoginUser();
        log.info(JSON.toJSONString(login));
        String enterpriseId = login.getEnterpriseId() ;
        String clerkId = login.getClerkId() ;
        if(StringUtils.isBlank(enterpriseId)){
            return resultResponse(HaoBanErrCode.ERR_OTHER, "gic商户为空");
        }
        log.info("好办菜单={},{}", enterpriseId, clerkId);
        if(StringUtils.isEmpty(clerkId)) {
            return resultResponse(HaoBanErrCode.ERR_OTHER, "clerkId为空");
        }
        ServiceResponse<Map<String,Object>> resp = this.rightService.listHaobanAllMenu(enterpriseId, clerkId);
        if (resp.isSuccess()) {
            List<RightMenuDTO> gicMenuList = rightService.listGicMenuByMenuCode(enterpriseId, clerkId, Lists.newArrayList("content_center"));
            Map<String, Object> result = resp.getResult();
            List<RightMenuDTO> menuList = (List<RightMenuDTO>)result.get("menu");
            menuList.addAll(gicMenuList);
            result.put("menu",menuList);
            return resultResponse(HaoBanErrCode.ERR_1, result);
        }
        return resultResponse(HaoBanErrCode.ERR_OTHER, resp.getMessage());
    }
    

}
