package com.fdkankan.manage.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.manage.common.CacheUtil;
import com.fdkankan.manage.common.ResultCode;
import com.fdkankan.manage.entity.*;
import com.fdkankan.manage.exception.BusinessException;
import com.fdkankan.manage.common.PageInfo;
import com.fdkankan.common.util.DateUtil;
import com.fdkankan.manage.mapper.IUserIncrementMapper;
import com.fdkankan.manage.service.*;
import com.fdkankan.manage.util.Dateutils;
import com.fdkankan.manage.vo.request.UserIncrementParam;
import com.fdkankan.manage.vo.response.GroupByCount;
import com.fdkankan.manage.vo.response.UserIncrementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 用户增值权益表 服务实现类
*
*
* @author
* @since 2022-06-16
*/
@Service
public class UserIncrementServiceImpl extends ServiceImpl implements IUserIncrementService {
@Autowired
private ICameraService cameraService;
@Autowired
IIncrementTypeService incrementTypeService;
@Autowired
IUserService userService;
@Autowired
IAgentNewLogService agentNewLogService;
@Autowired
ICameraIncrementLogService cameraIncrementLogService;
@Autowired
ISceneProService sceneProService;
@Autowired
IIncrementOrderMgService iIncrementOrderMgService;
@Override
public Long getValidCountByUserId(Long userId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserIncrement::getUserId,userId);
wrapper.gt(UserIncrement::getIncrementEndTime, DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
return this.count(wrapper);
}
@Override
public HashMap getValidCountGroupByUserId(List userIdList) {
HashMap map = new HashMap<>();
List result = this.getBaseMapper().getValidCountGroupByUserId(userIdList);
result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
return map;
}
@Override
public PageInfo pageList(Long userId, Integer pageNum, Integer pageSize) {
if(userId == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserIncrement::getUserId,userId);
wrapper.and(i ->i.eq(UserIncrement::getIsExpired,0).or().isNotNull(UserIncrement::getCameraId));
wrapper.orderByDesc(UserIncrement::getCreateTime);
Page page = this.page(new Page<>(pageNum, pageSize), wrapper);
List cameraIdList = page.getRecords().parallelStream()
.filter(camera ->camera.getCameraId()!=null)
.map(UserIncrement::getCameraId)
.collect(Collectors.toList());
HashMap cameraMap = new HashMap<>();
if(cameraIdList.size() >0){
List details = cameraService.getListByCameraIdList(cameraIdList);
for (Camera detail : details) {
cameraMap.put(detail.getId(),detail);
}
}
List voList = new ArrayList<>();
for (UserIncrement record : page.getRecords()) {
UserIncrementVo vo = new UserIncrementVo();
BeanUtils.copyProperties(record,vo);
if(record.getCameraId() != null){
Camera camera = cameraMap.get(record.getCameraId());
if(camera != null){
vo.setSnCode(camera.getSnCode());
}
}
if(record.getIncrementTypeId()!= null){
IncrementType incrementType = incrementTypeService.getById(record.getIncrementTypeId());
vo.setValidTimeType(incrementType.getValidTimeType());
}
if(!StringUtils.isEmpty(record.getOrderSn()) && !record.getOrderSn().contains("O")){
IncrementOrderMg incrementOrderMg = iIncrementOrderMgService.getByOrderSn(record.getOrderSn());
if(incrementOrderMg == null){
incrementOrderMg = iIncrementOrderMgService.getByIncrementId(record.getId());
}
if(incrementOrderMg != null){
vo.setCustomerName(incrementOrderMg.getCustomerName());
vo.setCustomerType(incrementOrderMg.getCustomerType());
vo.setEndCustomer(incrementOrderMg.getEndCustomer());
vo.setIncrementTypeId(incrementOrderMg.getIncrementType());
vo.setUseType(incrementOrderMg.getUseType());
vo.setProjectNum(incrementOrderMg.getProjectNum());
vo.setRemark(incrementOrderMg.getRemark());
}
}
voList.add(vo);
}
Page voPage = new Page<>(pageNum, pageSize);
voPage.setRecords(voList);
voPage.setTotal(page.getTotal());
return PageInfo.PageInfo(voPage);
}
@Override
public void delayById(Long id, Integer year) {
UserIncrement userIncrement = this.getById(id);
if(userIncrement == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
String incrementEndTime = userIncrement.getIncrementEndTime();
if(userIncrement.getIsExpired() == 1){
incrementEndTime = Dateutils.getDate(new Date());
}
userIncrement.setIsExpired(0);
Date date = DateUtil.string2Date(incrementEndTime, DateUtil.DEFAULT_DATE_FORMAT);
Date delay = null;
switch (incrementType.getValidTimeType()){
case 0 : delay = DateUtil.delay(date, 1, 1); break;
case 1 : delay = DateUtil.delay(date, 1, 2); break;
case 2 : delay = DateUtil.delay(date, 1, 5); break;
default: throw new BusinessException(ResultCode.INCREMENT_TYPE_ERROR);
}
userIncrement.setIncrementEndTime(DateUtil.date2String(delay,DateUtil.DEFAULT_DATE_FORMAT));
userIncrement.setUpdateTime(null);
this.updateById(userIncrement);
UserIncrementParam param = new UserIncrementParam();
IncrementOrderMg incrementOrderMg = iIncrementOrderMgService.getByOrderSn(userIncrement.getOrderSn());
if(incrementOrderMg !=null){
BeanUtil.copyProperties(incrementOrderMg,param);
}
param.setId(null);
param.setUserId(userIncrement.getUserId());
param.setCount(incrementType.getDownloadNum());
//userService.addDownNum(param);
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(User::getId,userIncrement.getUserId());
wrapper.setSql("download_num_total = download_num_total + " + incrementType.getDownloadNum());
userService.update(wrapper);
iIncrementOrderMgService.addOrder(param, incrementType);
agentNewLogService.addByUserIncrement(userIncrement);
if(userIncrement.getCameraId() != null){
sceneProService.lockOrUnLockBySpace(null,userIncrement.getCameraId(),1);
}
}
@Override
public void add(UserIncrementParam param) {
List userIncrementList = new ArrayList<>();
IncrementType incrementType = incrementTypeService.getById(param.getIncrementTypeId());
if(incrementType == null){
throw new BusinessException(ResultCode.INCREMENT_TYPE_EMPTY);
}
IncrementOrderMg incrementOrderMg = iIncrementOrderMgService.addOrder(param, incrementType);
for (int i = 0 ; i0){
this.saveOrUpdateBatch(userIncrementList);
User user = userService.getById(param.getUserId());
if(user == null){
throw new BusinessException(ResultCode.USER_NOT_EXIST);
}
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(User::getId,user.getId());
wrapper.setSql("download_num_total = download_num_total + " + param.getCount() * incrementType.getDownloadNum());
userService.update(wrapper);
}
}
@Override
public void unbindCamera(Long cameraId) {
cameraIncrementLogService.saveUnbindLog(cameraId);
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(UserIncrement::getCameraId,cameraId);
if("local".equals(CacheUtil.uploadType)){
this.remove(wrapper);
}else {
wrapper.set(UserIncrement::getCameraId,null);
this.update(wrapper);
}
}
@Override
public Long getValidCountByCameraId(Long cameraId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserIncrement::getCameraId,cameraId);
wrapper.gt(UserIncrement::getIncrementEndTime, DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
return this.count(wrapper);
}
@Override
public UserIncrement getByCameraId(Long cameraId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserIncrement::getCameraId,cameraId);
List list = this.list(wrapper);
if(list !=null && list.size() >0){
return list.get(0);
}
return null;
}
@Override
public void delAgentId(Integer agentId) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(UserIncrement::getAgentId,agentId);
wrapper.set(UserIncrement::getAgentId,null);
this.update(wrapper);
}
@Override
public List getByOrderSn(String orderSn) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.like(UserIncrement::getOrderSn,orderSn);
return this.list(wrapper);
}
}