
package com.tmerclub.cloud.user.service.impl;

import com.tmerclub.cloud.api.payment.vo.PaySettlementConfigVO;
import com.tmerclub.cloud.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.api.user.dto.FinanceDetailDTO;
import com.tmerclub.cloud.api.user.vo.FinanceDetailExcelVO;
import com.tmerclub.cloud.api.user.vo.FinanceDetailVO;
import com.tmerclub.cloud.api.user.vo.UserApiVO;
import com.tmerclub.cloud.api.user.vo.UserPayInfoVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.user.constant.LevelTypeEnum;
import com.tmerclub.cloud.user.dto.UserLevelLogDTO;
import com.tmerclub.cloud.user.mapper.UserLevelLogMapper;
import com.tmerclub.cloud.user.model.UserLevelLog;
import com.tmerclub.cloud.user.service.UserLevelLogService;
import com.tmerclub.cloud.user.vo.UserLevelLogVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 用户等级记录
 *
 * @author tmerclub
 * @date 2021-05-14 11:04:52
 */
@Service
public class UserLevelLogServiceImpl implements UserLevelLogService {

    @Autowired
    private UserLevelLogMapper userLevelLogMapper;
    @DubboReference
    private ConfigFeignClient configFeignClient;

    @Override
    public PageVO<UserLevelLogVO> pageBuyLevelLog(PageDTO pageDTO, UserLevelLogDTO userLevelLogDTO) {
        return PageUtil.doPage(pageDTO, () -> userLevelLogMapper.pageBuyLevelLog(userLevelLogDTO));
    }

    @Override
    public UserLevelLog getByLevelLogId(Long levelLogId) {
        return userLevelLogMapper.getByLevelLogId(levelLogId);
    }

    @Override
    public void save(UserLevelLog userLevelLog) {
        userLevelLogMapper.save(userLevelLog);
    }

    @Override
    public void update(UserLevelLog userLevelLog) {
        userLevelLogMapper.update(userLevelLog);
    }

    @Override
    public void deleteById(Long levelLogId) {
        userLevelLogMapper.deleteById(levelLogId);
    }

    @Override
    public void batchSaveUserLevelLogs(List<UserLevelLog> userLevelLogs) {
        userLevelLogMapper.batchSaveUserLevelLogs(userLevelLogs);
    }

    @Override
    public Integer getMaxLevelByUserId(Long userId) {
        return userLevelLogMapper.getMaxLevelByUserId(userId);
    }

    @Override
    public int updateToSuccess(Long levelLogId, Long payId, Integer payType) {
        return userLevelLogMapper.updateToSuccess(levelLogId, payId, payType);
    }

    @Override
    public void deleteByUserId(Long userId) {
        userLevelLogMapper.deleteByUserId(userId);
    }

    @Override
    public UserApiVO getUserTypeAndUserNameAtTime(Long userId, Date createTime) {
        UserApiVO userVO = userLevelLogMapper.getUserTypeAtTime(userId, createTime);
        if (Objects.isNull(userVO)) {
            userVO = new UserApiVO();
        }
        if (Objects.isNull(userVO.getLevelType())) {
            userVO.setLevelType(LevelTypeEnum.ORDINARY_USER.value());
        }
        return userVO;
    }

    @Override
    public PageVO<FinanceDetailVO> pageFinanceDetail(PageDTO pageDTO, FinanceDetailDTO financeDetailDTO) {
        return PageUtil.doPage(pageDTO, () -> userLevelLogMapper.listFinanceDetail(financeDetailDTO));
    }

    @Override
    public List<FinanceDetailExcelVO> listFinanceDetail(FinanceDetailDTO financeDetailDTO) {
        return BeanUtil.mapAsList(userLevelLogMapper.listFinanceDetail(financeDetailDTO), FinanceDetailExcelVO.class);
    }

    @Override
    public List<UserPayInfoVO> listPayInfoVO(Date startTime, Date endTime) {
        Integer paySysType = getPaySysType();
        return userLevelLogMapper.listPayInfo(startTime, endTime, paySysType);
    }

    private Integer getPaySysType() {
        ServerResponseEntity<String> configResponse = configFeignClient.getConfig(Constant.PAY_SETTLEMENT_CONFIG);
        if (!configResponse.isSuccess()) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
        PaySettlementConfigVO paySettlementConfigVO = Json.parseObject(configResponse.getData(), PaySettlementConfigVO.class);
        return paySettlementConfigVO.getPaySettlementType();
    }

}
