package com.zanxiang.game.module.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.module.manage.pojo.params.KfUserUpdateParam;
import com.zanxiang.game.module.manage.service.IGameAppletService;
import com.zanxiang.game.module.manage.service.IKfUserService;
import com.zanxiang.game.module.mybatis.entity.GameApplet;
import com.zanxiang.game.module.mybatis.entity.KfUser;
import com.zanxiang.game.module.mybatis.mapper.KfUserMapper;
import com.zanxiang.module.util.URIUtil;
import com.zanxiang.module.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : lingfeng
 * @time : 2023-11-27
 * @description : 客服用户信息
 */
@Slf4j
@Service
public class KfUserServiceImpl extends ServiceImpl<KfUserMapper, KfUser> implements IKfUserService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IGameAppletService gameAppletService;

    @Override
    public KfUser getKfUser(Long kfUserId, String appId) {
        KfUser kfUser = super.getOne(new LambdaQueryWrapper<KfUser>()
                .eq(KfUser::getKfUserId, kfUserId)
                .eq(KfUser::getAppId, appId));
        if (kfUser == null) {
            log.error("参数错误, 客服用户信息不存在, kfUserId : {}, appId : {}", kfUserId, appId);
            throw new BaseException("参数错误, 客服用户信息不存在");
        }
        return kfUser;
    }

    @Override
    public String getAuthCode(String appId) {
        GameApplet gameApplet = gameAppletService.getOne(new LambdaQueryWrapper<GameApplet>()
                .eq(GameApplet::getAppId, appId));
        if (gameApplet == null) {
            throw new BaseException("参数错误, 小游戏信息不存在");
        }
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("type", "WEIXINKEFU");
        paramMap.put("Authorization", URIUtil.encodeURIComponent(SecurityUtil.getToken()));
        paramMap.put("appId", gameApplet.getAppId());
        paramMap.put("gameName", gameApplet.getAppName());
        String uri = URIUtil.fillUrlParams("http://47.99.157.216:8020/scan/code/auto", paramMap, false);
        return restTemplate.getForObject(uri, String.class);
    }

    @Override
    public Boolean kfUserUpdate(KfUserUpdateParam param) {
        Long userId = SecurityUtil.getUserId();
        KfUser kfUser = super.getOne(new LambdaQueryWrapper<KfUser>()
                .eq(KfUser::getKfUserId, userId)
                .eq(KfUser::getAppId, param.getAppId()));
        if (kfUser == null) {
            return super.save(KfUser.builder()
                    .kfUserId(userId)
                    .appId(param.getAppId())
                    .token(param.getToken())
                    .cookie(param.getCookie())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build());
        }
        return super.update(new LambdaUpdateWrapper<KfUser>()
                .set(KfUser::getToken, param.getToken())
                .set(KfUser::getCookie, param.getCookie())
                .set(KfUser::getUpdateTime, LocalDateTime.now())
                .eq(KfUser::getKfUserId, userId)
                .eq(KfUser::getAppId, param.getAppId()));
    }
}
