package com.sptg.video.cloud.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.video.cloud.api.beans.ApiResultBean;
import com.sptg.video.cloud.api.entity.VCApiLog;
import com.sptg.video.cloud.api.entity.SysParams;
import com.sptg.video.cloud.api.dto.VCAccountDto;
import com.sptg.video.cloud.api.entity.VCAccount;
import com.sptg.video.cloud.api.utils.VideoCloudHttpUtil;
import com.sptg.video.cloud.biz.mapper.AccountMapper;
import com.sptg.video.cloud.biz.service.VCAccountServiceInterface;
import com.sptg.video.cloud.biz.service.VCApiLogServiceInterface;
import com.sptg.video.cloud.biz.service.VCSysParamsServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zwx
 * @create 2020/10/17 12:02
 * 用户 服务层实现层
 */
@Service
@RequiredArgsConstructor
public class VCAccountServiceImpl extends ServiceImpl<AccountMapper, VCAccount> implements VCAccountServiceInterface {

    private final VCSysParamsServiceInterface sysParamsServiceInterface;
    private final VCApiLogServiceInterface apiLogServiceInterface;
    private final RedisTemplate redisTemplate;
    //视频云登录token缓存名字
    private final static String VIDEO_CLOUD_LOGIN_TOKEN = "video:cloud:login:token";

    @Override
    public ApiResultBean login(String account, String userPwd) {
        String token = null;
        SysParams sysParams = this.sysParamsServiceInterface.findSysParamsByAcountPwd(account, userPwd);
        VideoCloudHttpUtil httpUtil = new VideoCloudHttpUtil(sysParams);
        ApiResultBean resultBean = httpUtil.login();
        if(resultBean.getReturnCode().equals(OperationConstant.ONE + "")){
            token = resultBean.getToken();
            sysParams.setToken(token);
            this.sysParamsServiceInterface.updateById(sysParams);
            VCApiLog apiLog = BeanUtil.copyProperties(resultBean, VCApiLog.class);
            apiLog.setCreateDate(new Date());
            apiLog.setApiMethodName("用户登录");
            apiLog.setAccountName(account);
            this.apiLogServiceInterface.insert(apiLog);
            VCAccount source = resultBean.getAccount();
            VCAccount target = null;
            target = this.findByAccountId(source.getAccountId());
            if(target == null){
                target = new VCAccount();
            }else{
                source.setId(target.getId());
            }
            BeanUtil.copyProperties(source, target);
            this.insertOrUpdate(target);

            //把token写进缓存里，并设置有效期，到期后重新登录
            String key = VIDEO_CLOUD_LOGIN_TOKEN + "-"+ account;
            //  验证码存入缓存
            ValueOperations<String, String> operations = this.redisTemplate.opsForValue();
            //  设置缓存有效期 20分钟
            operations.set(key,token,1200, TimeUnit.SECONDS);
        }
        return resultBean;
    }

    @Override
    public Page<VCAccount> pagingQuery(PageBean<VCAccount, VCAccountDto> pageBean) {
        VCAccountDto accountDto = pageBean.getDto();
        // 构建查询条件
        Wrapper<VCAccount> wrapper = new EntityWrapper<>();

        if(null != accountDto){
            if(StringUtils.hasText(accountDto.getAccountName())){
                wrapper.eq("s_account_name", accountDto.getAccountName());
            }
        }
        //  查询账号列表
        Page<VCAccount> page = this.selectPage(pageBean, wrapper);
        return page;
    }

    @Override
    public VCAccount findAccountById(Long id) {
        return this.selectById(id);
    }

    @Override
    public VCAccount findByAccountId(Long accountId) {
        Wrapper<VCAccount> wrapper = new EntityWrapper<>();
        wrapper.eq("l_account_id", accountId);
        return this.selectOne(wrapper);
    }

    @Override
    public List<VCAccount> getList() {
        Wrapper<VCAccount> wrapper = new EntityWrapper<>();
        return this.selectList(wrapper);
    }
}
