package com.yida.data.system.service.impl;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.WxPublicQr;
import com.yida.data.common.core.entity.agent.EduAgent;
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.constant.WxPublicQrTypeConstant;
import com.yida.data.common.core.entity.system.EduDeptWxPublic;
import com.yida.data.common.core.entity.system.SystemUser;
import com.yida.data.common.core.utils.WxPublicUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.system.mapper.EduAgentMapper;
import com.yida.data.system.mapper.EduAgentWxPublicReceiverMapper;
import com.yida.data.system.mapper.EduDeptWxPublicMapper;
import com.yida.data.system.service.EduAgentWxPublicReceiverService;
import com.yida.data.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Slf4j
@Service
@RequiredArgsConstructor
public class EduAgentWxPublicReceiverServiceImpl extends ServiceImpl<EduAgentWxPublicReceiverMapper,
    EduAgentWxPublicReceiver> implements EduAgentWxPublicReceiverService {

    private final EduAgentMapper eduAgentMapper;
    private final EduDeptWxPublicMapper eduDeptWxPublicMapper;

    private final RedisService redisService;
    private final WxPublicUtil wxPublicUtil;
    private final CommonService commonService;
    private final IUserService userService;

    @Override
    public String bindReceiver(Long agentId, CurrentUser currentUser) {
        // 此处查询知不知公众号
        EduDeptWxPublic sysWxPublic = eduDeptWxPublicMapper.selectOne(
                new LambdaQueryWrapper<>(new EduDeptWxPublic()).eq(EduDeptWxPublic::getDeptId, 1));
        String accessToken = wxPublicUtil.getAccessToken(sysWxPublic.getAppId(), sysWxPublic.getSecret());
        // 场景参数为   区域后台消息接收绑定常量+代理商部门id+当前登陆用户id
        String ticket = wxPublicUtil
            .generateQR(accessToken,
                    WxPublicQrTypeConstant.AGENT_RECEIVE_MSG + "-" + agentId + "-" + currentUser.getUserId(),
                    null);
        redisService.set(CachePrefixConstant.SYS_AGENT_RECEIVER_QR + "-" + agentId + "-" + currentUser.getUserId(), 0, 60L);
        return ticket;
    }

    @Override
    public List<EduAgentWxPublicReceiver> listReceiverBySchool(Long schoolId) {
        return baseMapper.listReceiverBySchool(schoolId);
    }

    @Override
    public void wxPublicQrCall(WxPublicQr wxPublicQr) {
        // 增加扫描记录
        redisService.incr(CachePrefixConstant.SYS_AGENT_RECEIVER_QR + wxPublicQr.getFlag(), 1L);
        // 处理扫描逻辑
        Long agentId = Long.valueOf(wxPublicQr.getFlag());
        EduAgent agent = eduAgentMapper.selectById(agentId);
        if (agent != null && count(Wrappers.<EduAgentWxPublicReceiver>lambdaQuery()
            .eq(EduAgentWxPublicReceiver::getAgentId, agentId)
            .eq(EduAgentWxPublicReceiver::getWxPublicOpenId, wxPublicQr.getUserOpenId())) == 0) {
            // 公司公众号数据
            EduDeptWxPublic sysWxPublic = commonService.getWxPublic(1L);
            String accessToken = wxPublicUtil.getAccessToken(sysWxPublic.getAppId(), sysWxPublic.getSecret());
            // 该用户微信信息
            JSONObject userInfo = wxPublicUtil.getUserInfo(accessToken, wxPublicQr.getUserOpenId());
            EduAgentWxPublicReceiver receiver = new EduAgentWxPublicReceiver();
            // 查询用户信息
            SystemUser systemUser = userService.getById(wxPublicQr.getUserId());

            receiver.setCreateId(systemUser.getUserId());
            receiver.setAgentId(agentId);
            receiver.setCreateDeptId(systemUser.getDeptId());
            receiver.setNickname(userInfo.getStr("nickname"));
            receiver.setAvatar(userInfo.getStr("headimgurl"));
            receiver.setSex(userInfo.getInt("sex"));
            receiver.setWxPublicOpenId(wxPublicQr.getUserOpenId());

            save(receiver);
        }
    }
}
