package com.tzld.piaoquan.api.service.contentplatform.impl;

import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
import com.tzld.piaoquan.api.common.enums.contentplatform.AccountStatusEnum;
import com.tzld.piaoquan.api.common.exception.CommonException;
import com.tzld.piaoquan.api.component.VideoApiService;
import com.tzld.piaoquan.api.dao.mapper.contentplatform.ContentPlatformPqAccountRelMapper;
import com.tzld.piaoquan.api.model.config.LoginUserContext;
import com.tzld.piaoquan.api.model.param.contentplatform.WxWebLoginParam;
import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformAccount;
import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformPqAccountRel;
import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformPqAccountRelExample;
import com.tzld.piaoquan.api.model.vo.contentplatform.WxBaseUserInfoVO;
import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ContentPlatformSettingServiceImpl implements ContentPlatformSettingService {

    @Autowired
    private ContentPlatformPqAccountRelMapper pqAccountRelMapper;
    @Autowired
    VideoApiService videoApiService;

    @Override
    public WxBaseUserInfoVO wxLoginByWeb(WxWebLoginParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        // 调用webLogin接口 登录/注册 票圈用户
        WxBaseUserInfoVO wxBaseUserInfoVO = videoApiService.webLogin(param.getCode(), param.getAppType(), param.getAppId(), param.getMachineCode());
        if (Objects.isNull(wxBaseUserInfoVO)) {
            throw new CommonException(ExceptionEnum.PQ_LOGIN_FAIL);
        }
        ContentPlatformPqAccountRel pqAccountRel = getPqAccountRel(user.getId());
        if (Objects.nonNull(pqAccountRel)) {
            wxBaseUserInfoVO = new WxBaseUserInfoVO();
            wxBaseUserInfoVO.setUid(pqAccountRel.getPqUid());
            wxBaseUserInfoVO.setNickName(pqAccountRel.getPqName());
            return wxBaseUserInfoVO;
        }
        pqAccountRel = new ContentPlatformPqAccountRel();
        pqAccountRel.setAccountId(user.getId());
        pqAccountRel.setPqUid(wxBaseUserInfoVO.getUid());
        pqAccountRel.setPqName(wxBaseUserInfoVO.getNickName());
        pqAccountRel.setPqTelNum(StringUtils.isBlank(wxBaseUserInfoVO.getPhoneNumber()) ? user.getTelNum() : wxBaseUserInfoVO.getPhoneNumber());
        pqAccountRel.setCreateTimestamp(System.currentTimeMillis());
        pqAccountRel.setUpdateTimestamp(System.currentTimeMillis());
        pqAccountRelMapper.insertSelective(pqAccountRel);
        // 异步修改手机号
        if (StringUtils.isBlank(wxBaseUserInfoVO.getPhoneNumber())) {
            asyncUpdatePqAccountTelNum(wxBaseUserInfoVO.getUid(), user.getTelNum());
        }

        return wxBaseUserInfoVO;
    }

    private void asyncUpdatePqAccountTelNum(Long uid, String telNum) {
        // 修改票圈用户手机号
        new Thread(() -> videoApiService.saveUserPhoneNumber(uid, telNum)).start();
    }

    @Override
    public ContentPlatformPqAccountRel getPqAccountRel(Long accountId) {
        ContentPlatformPqAccountRelExample example = new ContentPlatformPqAccountRelExample();
        example.createCriteria().andAccountIdEqualTo(accountId).andStatusEqualTo(AccountStatusEnum.NORMAL.getVal());
        List<ContentPlatformPqAccountRel> pqAccountRelList = pqAccountRelMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(pqAccountRelList)) {
            return null;
        }
        return pqAccountRelList.get(0);
    }

    @Override
    public WxBaseUserInfoVO getBindPQUserInfo() {
        ContentPlatformAccount user = LoginUserContext.getUser();
        ContentPlatformPqAccountRel pqAccountRel = getPqAccountRel(user.getId());
        if (Objects.isNull(pqAccountRel)) {
            return null;
        }
        WxBaseUserInfoVO wxBaseUserInfoVO = new WxBaseUserInfoVO();
        wxBaseUserInfoVO.setUid(pqAccountRel.getPqUid());
        wxBaseUserInfoVO.setNickName(pqAccountRel.getPqName());
        return wxBaseUserInfoVO;
    }

}
