package com.yida.data.system.controller;


import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.agent.EduAgentWxPublicReceiver;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.enums.RoleEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.core.utils.RoleUtil;
import com.yida.data.system.service.EduAgentWxPublicReceiverService;
import com.yida.data.system.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "区域后台接收公众号消息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/agentReceiver")
public class EduAgentWxPublicReceiverController {
    private final EduAgentWxPublicReceiverService eduAgentWxPublicReceiverService;
    private final RedisService redisService;
    private final IDeptService deptService;

    @GetMapping("/listReceiver")
    public ResultBean<IPage<EduAgentWxPublicReceiver>> listReceiverPage(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
//        Long agentId = currentUser.getAgentId();
//        Asserts.isNotNull(agentId, "请使用区域账户登录");
        LambdaQueryWrapper<EduAgentWxPublicReceiver> condition = Wrappers.<EduAgentWxPublicReceiver>lambdaQuery();

        // 查询下级部门信息
        List<Long> childIdList = this.deptService.findListByParent(Collections.singletonList(currentUser.getDeptId()), 0)
                .stream().map(Dept::getDeptId).collect(Collectors.toList());
        // 代理商管理员
        if (RoleEnum.ROLE_AGENT_ADMIN.equals(RoleUtil.getMaxRole(currentUser))) {
            childIdList.add(currentUser.getDeptId());
        }
        // 学校、教育局、代理商及以下具有创建账号的数据   自己创建的角色+当前登陆账号部门以下的角色
        if (CollectionUtils.isNotEmpty(childIdList)) {
            condition.in(EduAgentWxPublicReceiver::getCreateDeptId, childIdList).or();
        }
        // 代理商其它可以创建账号的数据
        condition.eq(EduAgentWxPublicReceiver::getCreateId, currentUser.getUserId());
        if (StrUtil.isNotBlank(keyword)) {
            condition.like(EduAgentWxPublicReceiver::getNickname, keyword);
        }
        condition.orderByDesc(EduAgentWxPublicReceiver::getCreateDate);
        return ResultBean.buildSuccess(eduAgentWxPublicReceiverService.page(new Page(pageNum, pageSize), condition));
    }

    @GetMapping("/bind")
    public ResultBean bind() {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
//        Long agentId = currentUser.getAgentId();
//        Asserts.isNotNull(agentId, "请使用区域账户登录");
        Long agentId;
        if (RoleEnum.ROLE_AGENT_ADMIN.equals(RoleUtil.getMaxRole(currentUser))) {
            agentId = currentUser.getDeptId();
        } else {
            agentId = currentUser.getMainDeptId();
        }
        return ResultBean.buildSuccess(eduAgentWxPublicReceiverService.bindReceiver(agentId, currentUser));
    }

    @PostMapping("/unbindBatch")
    public ResultBean unbind(@RequestBody List<Long> ids) {
        eduAgentWxPublicReceiverService.removeByIds(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询扫码进度")
    @GetMapping("/qrPercent")
    public ResultBean<Object> qrPercent() {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        Long agentId;
        if (RoleEnum.ROLE_AGENT_ADMIN.equals(RoleUtil.getMaxRole(currentUser))) {
            agentId = currentUser.getDeptId();
        } else {
            agentId = currentUser.getMainDeptId();
        }
        return ResultBean.buildSuccess(
                redisService.get(CachePrefixConstant.SYS_AGENT_RECEIVER_QR
                        + "-" + agentId + "-" + currentUser.getUserId()));
    }
}
