package com.fdkankan.agent.service.impl;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.agent.entity.AgentNew;
import com.fdkankan.agent.entity.AgentNewLog;
import com.fdkankan.agent.entity.IncrementType;
import com.fdkankan.agent.mapper.IAgentNewMapper;
import com.fdkankan.agent.response.AgentNewVo;
import com.fdkankan.agent.service.IAgentNewLogService;
import com.fdkankan.agent.service.IAgentNewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-11-09
*/
@Service
public class AgentNewServiceImpl extends ServiceImpl implements IAgentNewService {
@Autowired
IAgentNewLogService agentNewLogService;
@Override
public AgentNewVo getByUserName(String phoneNum) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(AgentNew::getUserName,phoneNum);
List list = this.list(wrapper);
if(list == null || list.size() <=0){
return null;
}
List listVo = new ArrayList<>();
for (AgentNew agentNew : list) {
AgentNewVo vo = new AgentNewVo();
BeanUtils.copyProperties(agentNew,vo);
vo.setDownSubNum(vo.getDownTotalNum() - vo.getDownUseNum());
vo.setHighSubNum(vo.getHighTotalNum() - vo.getHighUseNum());
vo.setMajorSubNum(vo.getMajorTotalNum() - vo.getMajorUseNum());
listVo.add(vo);
}
return listVo.get(0);
}
@Override
public void subNum(AgentNewVo agentNewVo, Long userId,IncrementType incrementType, Integer count,Integer addType) {
if(count == null || count <=0){
return;
}
AgentNewLog agentNewLog = new AgentNewLog();
agentNewLog.setAgentId(agentNewVo.getId());
agentNewLog.setUserId(userId);
agentNewLog.setCount(count);
agentNewLog.setGiveType(addType);
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(AgentNew::getId,agentNewVo.getId());
if(incrementType != null && incrementType.getValidTimeType() == 0){
agentNewLog.setType(0);
wrapper.set(AgentNew::getMajorUseNum,agentNewVo.getMajorUseNum() + count);
}
if(incrementType != null && incrementType.getValidTimeType() == 1){
agentNewLog.setType(1);
wrapper.set(AgentNew::getHighUseNum,agentNewVo.getHighUseNum() + count);
}
if(incrementType == null){
agentNewLog.setType(2);
wrapper.set(AgentNew::getDownUseNum,agentNewVo.getDownUseNum() + count);
}
this.update(wrapper);
agentNewLogService.save(agentNewLog);
}
}