/*
MIT License

Copyright (c) 2020 www.joolun.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.huijy.weixin.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.huijy.management.domain.EarlyWarning;
import com.huijy.management.domain.EarlyWarningSendLog;
import com.huijy.management.service.IEarlyWarningSendLogService;
import com.huijy.management.service.IEarlyWarningService;
import com.huijy.system.service.ISysConfigService;
import com.huijy.system.service.ISysDictDataService;
import com.huijy.weixin.config.WxMaConfiguration;
import com.huijy.weixin.constant.ConfigConstant;
import com.huijy.weixin.constant.WxMaConstants;
import com.huijy.weixin.entity.ThirdSession;
import com.huijy.weixin.entity.WxOpenDataDTO;
import com.huijy.weixin.entity.WxUser;
import com.huijy.weixin.handler.SubscribeHandler;
import com.huijy.weixin.mapper.WxUserMapper;
import com.huijy.weixin.service.WxUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import me.chanjar.weixin.mp.api.WxMpUserTagService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 微信用户
 *
 * @author www.joolun.com
 * @date 2019-03-25 15:39:39
 */
@Slf4j
@Service
@AllArgsConstructor
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements WxUserService {

    private final WxMpService wxService;
    private final RedisTemplate redisTemplate;

    private IEarlyWarningService earlyWarningService;

    private IEarlyWarningSendLogService earlyWarningSendLogService;

    private ISysDictDataService sysDictDataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRemark(WxUser entity) throws WxErrorException {
        String id = entity.getId();
        String remark = entity.getRemark();
        String openId = entity.getOpenId();
        entity = new WxUser();
        entity.setId(id);
        entity.setRemark(remark);
        super.updateById(entity);
        WxMpUserService wxMpUserService = wxService.getUserService();
        wxMpUserService.userUpdateRemark(openId, remark);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void tagging(String taggingType, Long tagId, String[] openIds) throws WxErrorException {
        WxMpUserTagService wxMpUserTagService = wxService.getUserTagService();
        WxUser wxUser;
        if ("tagging".equals(taggingType)) {
            for (String openId : openIds) {
                wxUser = baseMapper.selectOne(Wrappers.<WxUser>lambdaQuery()
                        .eq(WxUser::getOpenId, openId));
                Long[] tagidList = wxUser.getTagidList();
                List<Long> list = Arrays.asList(tagidList);
                list = new ArrayList<>(list);
                if (!list.contains(tagId)) {
                    list.add(tagId);
                    tagidList = list.toArray(new Long[list.size()]);
                    wxUser.setTagidList(tagidList);
                    this.updateById(wxUser);
                }
            }
            wxMpUserTagService.batchTagging(tagId, openIds);
        }
        if ("unTagging".equals(taggingType)) {
            for (String openId : openIds) {
                wxUser = baseMapper.selectOne(Wrappers.<WxUser>lambdaQuery()
                        .eq(WxUser::getOpenId, openId));
                Long[] tagidList = wxUser.getTagidList();
                List<Long> list = Arrays.asList(tagidList);
                list = new ArrayList<>(list);
                if (list.contains(tagId)) {
                    list.remove(tagId);
                    tagidList = list.toArray(new Long[list.size()]);
                    wxUser.setTagidList(tagidList);
                    this.updateById(wxUser);
                }
            }
            wxMpUserTagService.batchUntagging(tagId, openIds);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void synchroWxUser() throws WxErrorException {
        //先将已关注的用户取关
        WxUser wxUser = new WxUser();
        wxUser.setSubscribe(ConfigConstant.SUBSCRIBE_TYPE_NO);
        this.baseMapper.update(wxUser, Wrappers.<WxUser>lambdaQuery()
                .eq(WxUser::getSubscribe, ConfigConstant.SUBSCRIBE_TYPE_YES));
        WxMpUserService wxMpUserService = wxService.getUserService();
        this.recursionGet(wxMpUserService, null);
    }

    /**
     * 递归获取
     *
     * @param nextOpenid
     */
    void recursionGet(WxMpUserService wxMpUserService, String nextOpenid) throws WxErrorException {
        WxMpUserList userList = wxMpUserService.userList(nextOpenid);
        List<WxUser> listWxUser = new ArrayList<>();
        List<WxMpUser> listWxMpUser = getWxMpUserList(wxMpUserService, userList.getOpenids());
        listWxMpUser.forEach(wxMpUser -> {
            WxUser wxUser = baseMapper.selectOne(Wrappers.<WxUser>lambdaQuery()
                    .eq(WxUser::getOpenId, wxMpUser.getOpenId()));
            if (wxUser == null) {//用户未存在
                wxUser = new WxUser();
                wxUser.setSubscribeNum(1);
            }
            SubscribeHandler.setWxUserValue(wxUser, wxMpUser);
            listWxUser.add(wxUser);
        });
        this.saveOrUpdateBatch(listWxUser);
        if (userList.getCount() >= 10000) {
            this.recursionGet(wxMpUserService, userList.getNextOpenid());
        }
    }

    /**
     * 分批次获取微信粉丝信息 每批100条
     *
     * @param wxMpUserService
     * @param openidsList
     * @return
     * @throws WxErrorException
     * @author
     */
    private List<WxMpUser> getWxMpUserList(WxMpUserService wxMpUserService, List<String> openidsList) throws WxErrorException {
        // 粉丝openid数量
        int count = openidsList.size();
        if (count <= 0) {
            return new ArrayList<>();
        }
        List<WxMpUser> list = Lists.newArrayList();
        List<WxMpUser> followersInfoList;
        int a = count % 100 > 0 ? count / 100 + 1 : count / 100;
        for (int i = 0; i < a; i++) {
            if (i + 1 < a) {
                log.debug("i:{},from:{},to:{}", i, i * 100, (i + 1) * 100);
                followersInfoList = wxMpUserService.userInfoList(openidsList.subList(i * 100, ((i + 1) * 100)));
                if (null != followersInfoList && !followersInfoList.isEmpty()) {
                    list.addAll(followersInfoList);
                }
            } else {
                log.debug("i:{},from:{},to:{}", i, i * 100, count - i * 100);
                followersInfoList = wxMpUserService.userInfoList(openidsList.subList(i * 100, count));
                if (null != followersInfoList && !followersInfoList.isEmpty()) {
                    list.addAll(followersInfoList);
                }
            }
        }
        log.debug("本批次获取微信粉丝数：", list.size());
        return list;
    }

    @Override
    public WxUser getByOpenId(String openId) {
        return this.baseMapper.selectOne(Wrappers.<WxUser>lambdaQuery()
                .eq(WxUser::getOpenId, openId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WxUser loginMa(String appId, String jsCode) throws WxErrorException {
        WxMaJscode2SessionResult jscode2session = WxMaConfiguration.getMaService(appId).jsCode2SessionInfo(jsCode);
        WxUser wxUser = this.getByOpenId(jscode2session.getOpenid());
        if (wxUser == null) {
            //新增微信用户
            wxUser = new WxUser();
            wxUser.setAppType(ConfigConstant.WX_APP_TYPE_1);
            wxUser.setOpenId(jscode2session.getOpenid());
            wxUser.setSessionKey(jscode2session.getSessionKey());
            wxUser.setUnionId(jscode2session.getUnionid());
            try {
                this.save(wxUser);
            } catch (DuplicateKeyException e) {
                if (e.getMessage().contains("uk_appid_openid")) {
                    wxUser = this.getByOpenId(wxUser.getOpenId());
                }
            }
        } else {
            //更新SessionKey
            wxUser.setAppType(ConfigConstant.WX_APP_TYPE_1);
            wxUser.setOpenId(jscode2session.getOpenid());
            wxUser.setSessionKey(jscode2session.getSessionKey());
            wxUser.setUnionId(jscode2session.getUnionid());
            this.updateById(wxUser);
        }

        String thirdSessionKey = UUID.randomUUID().toString();
        ThirdSession thirdSession = new ThirdSession();
        thirdSession.setAppId(appId);
        thirdSession.setSessionKey(wxUser.getSessionKey());
        thirdSession.setWxUserId(wxUser.getId());
        thirdSession.setOpenId(wxUser.getOpenId());
        //将3rd_session和用户信息存入redis，并设置过期时间
        String key = WxMaConstants.THIRD_SESSION_BEGIN + ":" + thirdSessionKey;
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(thirdSession), WxMaConstants.TIME_OUT_SESSION, TimeUnit.HOURS);
        wxUser.setSessionKey(thirdSessionKey);
        return wxUser;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WxUser saveOrUptateWxUser(WxOpenDataDTO wxOpenDataDTO) {
        WxMaUserService wxMaUserService = WxMaConfiguration.getMaService(wxOpenDataDTO.getAppId()).getUserService();
        WxMaUserInfo wxMaUserInfo = wxMaUserService.getUserInfo(wxOpenDataDTO.getSessionKey(), wxOpenDataDTO.getEncryptedData(), wxOpenDataDTO.getIv());
        WxUser wxUser = new WxUser();
        BeanUtil.copyProperties(wxMaUserInfo, wxUser);
        wxUser.setId(wxOpenDataDTO.getUserId());
        wxUser.setSex(wxMaUserInfo.getGender());
        wxUser.setHeadimgUrl(wxMaUserInfo.getAvatarUrl());
        baseMapper.updateById(wxUser);
        wxUser = baseMapper.selectById(wxUser.getId());
        return wxUser;
    }

    @Override
    public void sendWxTemp(Long earlyWarningId, String templateId) {
        ThreadUtil.execute(() -> {
            WxMpTemplateMsgService wxMpTemplateMsgService = wxService.getTemplateMsgService();
            EarlyWarning earlyWarning = earlyWarningService.selectEarlyWarningByEarlyWarningId(earlyWarningId);
            if (earlyWarning != null) {
                String label = sysDictDataService.selectDictLabel("early_warning_type", earlyWarning.getEarlyWarningType());
                //获取所有的公众号
                List<WxUser> list = baseMapper.selectList(new QueryWrapper<WxUser>());
                Date now = new Date();
                String sendDate = DateUtil.format(now, "yyyy-MM-dd HH:mm");
                for (WxUser item : list) {
                    String toUser = item.getOpenId();
                    WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
                    wxMpTemplateMessage.setTemplateId(templateId);
                    wxMpTemplateMessage.setToUser(toUser);
                    //添加参数 first
                    WxMpTemplateData firstData = new WxMpTemplateData("first", earlyWarning.getContent());
                    WxMpTemplateData keyword1Data = new WxMpTemplateData("keyword1", sendDate);
                    WxMpTemplateData keyword2Data = new WxMpTemplateData("keyword2", label);
                    WxMpTemplateData keyword3Data = new WxMpTemplateData("keyword3", earlyWarning.getRangeText());
                    WxMpTemplateData keyword4Data = new WxMpTemplateData("keyword4", earlyWarning.getTitle());
                    WxMpTemplateData remarkData = new WxMpTemplateData("remark", earlyWarning.getRemarks());

                    wxMpTemplateMessage
                            .addData(firstData)
                            .addData(keyword1Data)
                            .addData(keyword2Data)
                            .addData(keyword3Data)
                            .addData(keyword4Data)
                            .addData(remarkData);
                    try {
                        String msgId = wxMpTemplateMsgService.sendTemplateMsg(wxMpTemplateMessage);
                        if (StringUtils.isNotBlank(msgId)) {
                            EarlyWarningSendLog sendLog = new EarlyWarningSendLog();
                            sendLog.setMpOpenId(toUser);
                            sendLog.setMsgId(msgId);
                            sendLog.setSendTime(now);
                            sendLog.setEarlyWarningId(earlyWarningId);
                            earlyWarningSendLogService.insertEarlyWarningSendLog(sendLog);
                        }
                    } catch (WxErrorException e) {
                        e.printStackTrace();
                    }
                }
            }

        });
    }
}
