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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.authcenter.commons.dto.AuthcenterUserDetails;
import com.gic.authcenter.commons.util.SessionContextUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.manage.api.dto.qdto.role.HaobanRoleQDTO;
import com.gic.haoban.manage.api.dto.role.HaobanMenuDTO;
import com.gic.haoban.manage.api.dto.role.HaobanRoleDTO;
import com.gic.haoban.manage.api.service.role.HaobanMenuApiService;
import com.gic.haoban.manage.api.service.role.HaobanRoleApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author xuwenqian
 * @date 2022/9/13 3:07 下午
 */
@RequestMapping("role")
@RestController
public class HaobanRoleOperationController {

    private static Logger logger  = LoggerFactory.getLogger(HaobanRoleOperationController.class);


    @Autowired
    private HaobanMenuApiService haobanMenuApiService;
    @Autowired
    private HaobanRoleApiService haobanRoleApiService;

    /**
     * 获取菜单树
     * @return
     */
    @RequestMapping("/getMenuTreeList")
    public RestResponse<List<HaobanMenuDTO>> getMenuTreeList() {
        ServiceResponse<List<HaobanMenuDTO>> serviceMenuTreeList = haobanMenuApiService.getMenuTreeList();
        return RestResponse.successResult(serviceMenuTreeList.getResult());
    }

    /**
     * 修改
     * @param haobanRoleQDTO
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public RestResponse edit(@RequestBody HaobanRoleQDTO haobanRoleQDTO, HttpServletRequest request) {
        AuthcenterUserDetails loginUser = (AuthcenterUserDetails) SessionContextUtil.getUser(request) ;
        String operationUserId = String.valueOf(loginUser.getId());
        WebLoginDTO login = new WebLoginDTO();
        login.setWxEnterpriseId(haobanRoleQDTO.getWxEnterpriseId());
        login.setClerkId(operationUserId);
        login.setClerkName(loginUser.getLoginName());
        haobanRoleQDTO.setSystemFlag(0);
        logger.info("运维好办权限={},{}", JSONObject.toJSONString(haobanRoleQDTO),operationUserId);
        return RestResponse.successResult(haobanRoleApiService.edit(haobanRoleQDTO,login));
    }

    /**
     * 根据类型查询菜单
     * @return
     */
    @RequestMapping(value = "/queryMenuByClerkType",method = RequestMethod.GET)
    public RestResponse<HaobanRoleDTO> queryMenuByClerkType(Integer clerkType, String wxEnterpriseId) {
        ServiceResponse<HaobanRoleDTO> serviceResponse = haobanRoleApiService.getMenuByWxEnterpriseIdAndClerkType(wxEnterpriseId, clerkType,0);
        return RestResponse.successResult(serviceResponse.getResult());
    }

}
