package com.fdkankan.ucenter.service.impl;

import com.fdkankan.ucenter.entity.*;
import com.fdkankan.ucenter.mapper.IAgentNewLogMapper;
import com.fdkankan.ucenter.service.IAgentNewLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IIncrementTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-11-23
 */
@Service
public class AgentNewLogServiceImpl extends ServiceImpl<IAgentNewLogMapper, AgentNewLog> implements IAgentNewLogService {

    @Autowired
    IIncrementTypeService incrementTypeService;

    @Value("${agent.wherefor.key:wherefor}")
    private String whereforKey;

    @Value("${agent.wherefor.id:5}")
    private Integer whereforId;

    @Override
    public void addByUserIncrement(UserIncrement userIncrement,String orderSn) {
        if(userIncrement.getAgentId() !=null){
            AgentNewLog log = new AgentNewLog();
            if(userIncrement.getIncrementTypeId() != null){
                IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
                if(incrementType!=null){
                    log.setType(incrementType.getValidTimeType());
                }
            }
            log.setAgentId(userIncrement.getAgentId());
            log.setUserId(userIncrement.getUserId());
            log.setGiveType(2);
            log.setCount(1);
            log.setTotalTime(userIncrement.getMonthQy());
            log.setOrderSn(orderSn);
            this.save(log);
        }
    }


    @Override
    public void addByOrder(DownloadOrder responseIncrementOrder) {
        if(StringUtils.isNotBlank(responseIncrementOrder.getAgentKey()) && whereforKey.equals(responseIncrementOrder.getAgentKey())){
            AgentNewLog log = new AgentNewLog();
            log.setType(2);
            log.setAgentId(whereforId);
            log.setUserId(responseIncrementOrder.getUserId());
            log.setGiveType(4);
            log.setCount(responseIncrementOrder.getCount());
            log.setOrderSn(responseIncrementOrder.getOrderSn());
            this.save(log);
        }
    }

    @Override
    public Boolean addByOrder(IncrementOrder responseIncrementOrder) {
        if(StringUtils.isNotBlank(responseIncrementOrder.getAgentKey()) && whereforKey.equals(responseIncrementOrder.getAgentKey())){
            AgentNewLog log = new AgentNewLog();
            if(responseIncrementOrder.getIncrementType() != null){
                IncrementType incrementType = incrementTypeService.getById(responseIncrementOrder.getIncrementType());
                if(incrementType!=null){
                    log.setType(incrementType.getValidTimeType());
                }
            }
            log.setAgentId(whereforId);
            log.setUserId(responseIncrementOrder.getUserId());
            log.setGiveType(4);
            log.setCount(responseIncrementOrder.getCount());
            log.setTotalTime(responseIncrementOrder.getMonthQy());
            log.setOrderSn(responseIncrementOrder.getOrderSn());
            this.save(log);
            return true;
        }
        return false;
    }
}
