123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- 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;
- /**
- * <p>
- * 用户增值权益表 服务实现类
- * </p>
- *
- * @author
- * @since 2022-06-16
- */
- @Service
- public class UserIncrementServiceImpl extends ServiceImpl<IUserIncrementMapper, UserIncrement> 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<UserIncrement> 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<Long, Long> getValidCountGroupByUserId(List<Long> userIdList) {
- HashMap<Long,Long> map = new HashMap<>();
- List<GroupByCount> 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<UserIncrement> 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<UserIncrement> page = this.page(new Page<>(pageNum, pageSize), wrapper);
- List<Long> cameraIdList = page.getRecords().parallelStream()
- .filter(camera ->camera.getCameraId()!=null)
- .map(UserIncrement::getCameraId)
- .collect(Collectors.toList());
- HashMap<Long,Camera> cameraMap = new HashMap<>();
- if(cameraIdList.size() >0){
- List<Camera> details = cameraService.getListByCameraIdList(cameraIdList);
- for (Camera detail : details) {
- cameraMap.put(detail.getId(),detail);
- }
- }
- List<UserIncrementVo> 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<UserIncrementVo> 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<User> 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<UserIncrement> 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 ; i<param.getCount() ;i++) {
- UserIncrement userIncrement = new UserIncrement();
- String date = DateUtil.date2String(new Date(), DateUtil.DEFAULT_DATE_FORMAT);
- userIncrement.setId(param.getId());
- userIncrement.setUserId(param.getUserId());
- userIncrement.setKeyWord(UUID.randomUUID().toString().replace("-", ""));
- userIncrement.setIsExpired(0);
- userIncrement.setUpdateTime(date);
- userIncrement.setIncrementEndTime(param.getIncrementEndTime());
- userIncrement.setIncrementTypeId(param.getIncrementTypeId());
- if(userIncrement.getId()== null){
- userIncrement.setIncrementStartTime(date);
- userIncrement.setCreateTime(date);
- userIncrement.setOrderSn(incrementOrderMg.getOrderSn());
- }
- if(incrementType.getValidTimeType() == 0){
- userIncrement.setMemberLevels("PR");
- }
- if(incrementType.getValidTimeType() == 1){
- userIncrement.setMemberLevels("SE");
- }
- if(param.getMonthQy() != null){
- userIncrement.setMonthQy(param.getMonthQy());
- }
- userIncrementList.add(userIncrement);
- }
- if(userIncrementList.size() >0){
- this.saveOrUpdateBatch(userIncrementList);
- User user = userService.getById(param.getUserId());
- if(user == null){
- throw new BusinessException(ResultCode.USER_NOT_EXIST);
- }
- LambdaUpdateWrapper<User> 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<UserIncrement> 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<UserIncrement> 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<UserIncrement> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(UserIncrement::getCameraId,cameraId);
- List<UserIncrement> list = this.list(wrapper);
- if(list !=null && list.size() >0){
- return list.get(0);
- }
- return null;
- }
- @Override
- public void delAgentId(Integer agentId) {
- LambdaUpdateWrapper<UserIncrement> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(UserIncrement::getAgentId,agentId);
- wrapper.set(UserIncrement::getAgentId,null);
- this.update(wrapper);
- }
- @Override
- public List<UserIncrement> getByOrderSn(String orderSn) {
- LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
- wrapper.like(UserIncrement::getOrderSn,orderSn);
- return this.list(wrapper);
- }
- }
|