package com.ningdatech.carapi.sys.controller;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.sys.entity.Menu;
import com.ningdatech.carapi.sys.entity.RoleMenu;
import com.ningdatech.carapi.sys.entity.dto.OperatorPageQuery;
import com.ningdatech.carapi.sys.entity.dto.OperatorQueryDTO;
import com.ningdatech.carapi.sys.entity.dto.OperatorSaveDTO;
import com.ningdatech.carapi.sys.entity.dto.OperatorUpdateDTO;
import com.ningdatech.carapi.sys.entity.vo.OperatorVO;
import com.ningdatech.carapi.sys.manage.OperatorManage;
import com.ningdatech.carapi.sys.service.IRoleMenuService;
import com.ningdatech.carapi.sys.service.MenuService;
import com.ningdatech.carapi.sys.service.OperatorService;
import com.ningdatech.carapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import com.ningdatech.log.annotation.WebLog;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * 运营商管理
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/operator")
@Api(value = "Operator", tags = "系统管理-运营商")
@RequiredArgsConstructor
public class OperatorController {

    private final OperatorManage operatorManage;
    private final OperatorService operatorService;
    private final IRoleMenuService roleMenuService;
    private final MenuService menuService;

    @ApiOperation(value = "运营商列表查询", notes = "运营商列表查询")
    @GetMapping("/list")
    public PageVo<OperatorVO> queryList(@ModelAttribute OperatorPageQuery operatorPageQuery) {
        return operatorManage.queryList(operatorPageQuery);
    }

    /**
     * 查询运营商详情
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询运营商详情", notes = "查询运营商详情")
    @GetMapping("/details")
    public OperatorQueryDTO getDetails(@RequestParam Long id) {
        return operatorManage.getDetails(id);
    }

    @ApiOperation(value = "保存新运营商", notes = "保存新运营商")
    @PostMapping("/save")
    @WebLog(value = "保存运营商信息", modular = "运营商管理")
    public OperatorVO handlerSave(@RequestBody OperatorSaveDTO data) {
        operatorService.save(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, OperatorVO.class);
    }

    @ApiOperation(value = "编辑运营商", notes = "编辑运营商")
    @PostMapping("/modify")
    @WebLog(value = "编辑运营商信息", modular = "运营商管理")
    public OperatorVO handlerUpdate(@RequestBody OperatorUpdateDTO data) {
        operatorService.update(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, OperatorVO.class);
    }

    @ApiOperation(value = "删除运营商", notes = "删除运营商")
    @PostMapping("/remove")
    @WebLog(value = "删除运营商信息", modular = "运营商管理")
    public Boolean handlerDelete(@RequestBody List<Long> ids) {
        // 判断登录用户是否有运营商管理的菜单权限
        UserInfoDetails userInfoDetails = LoginUserUtil.loginUserDetail();
        Long roleId = userInfoDetails.getRoleId();
        List<Long> menuIdList = roleMenuService.list(Wrappers.lambdaQuery(RoleMenu.class).eq(RoleMenu::getRoleId, roleId)).stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        Menu menu = menuService.getOne(Wrappers.lambdaQuery(Menu.class).eq(Menu::getTitle, "运营商管理"));
        if (Objects.isNull(menu)){
            return false;
        }
        if (!menuIdList.contains(menu.getId())){
            return false;
        }
        return operatorService.removeByIdWithCache(ids);
    }
}
