package com.fdkankan.fusion.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.fusion.common.ResultCode;
import com.fdkankan.fusion.common.ResultCode;
import com.fdkankan.fusion.entity.CaseTag;
import com.fdkankan.fusion.exception.BusinessException;
import com.fdkankan.fusion.entity.HotIcon;
import com.fdkankan.fusion.mapper.IHotIconMapper;
import com.fdkankan.fusion.service.ICaseTagService;
import com.fdkankan.fusion.service.IHotIconService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-08-02
*/
@Service
public class HotIconServiceImpl extends ServiceImpl implements IHotIconService {
@Autowired
ICaseTagService caseTagService;
@Override
public List getListByUserName(String username) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.orderByDesc(HotIcon::getIsSystem) // 官方默认
.orderByDesc(HotIcon::getIsNew) // 新增
.orderByDesc(HotIcon::getLastUse) // 上次使用
.orderByDesc(HotIcon::getUseNum) // 使用次数
.orderByDesc(HotIcon::getSort)
.orderByDesc(HotIcon::getCreateTime);
return this.list(wrapper);
}
@Override
public List getDefaultIcon() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(HotIcon::getIsSystem,1);
wrapper.orderByDesc(HotIcon::getCreateTime);
List list = this.list(wrapper);
return list;
}
@Override
public void addUseNum(Integer hotIconId) {
HotIcon hotIcon = this.getById(hotIconId);
if(hotIcon == null){
throw new BusinessException(ResultCode.HOT_ICON_NOT_EXIST);
}
hotIcon.setUseNum( hotIcon.getUseNum() + 1);
hotIcon.setLastUse(1);
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.set(HotIcon::getLastUse,0);
wrapper.eq(HotIcon::getUserName,hotIcon.getUserName());
this.update(wrapper);
hotIcon.setUpdateTime(null);
this.updateById(hotIcon);
}
@Override
public void cancelIsNew(String username) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.set(HotIcon::getIsNew,1)
.eq(HotIcon::getUserName,username);
this.update(wrapper);
}
@Override
public List getListByFusionId(Integer fusionId) {
HashSet hotIconIds = new HashSet<>();
List list = caseTagService.getListByFusionId(fusionId);
if(list.size() >0){
List ids = list.parallelStream().map(CaseTag::getHotIconId).collect(Collectors.toList());
if(!ids.isEmpty()){
hotIconIds.addAll(ids);
}
}
hotIconIds.addAll(getDefaultIcon().stream().map(HotIcon::getIconId).collect(Collectors.toList()));
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(HotIcon::getFusionId,fusionId);
List list1 = this.list(wrapper);
List ids = list1.stream().map(HotIcon::getIconId).collect(Collectors.toList());
if(!ids.isEmpty()){
hotIconIds.addAll(ids);
}
if(hotIconIds.isEmpty()){
return new ArrayList<>();
}
return this.getByIds(hotIconIds);
}
@Override
public List getByIds( HashSet hotIconIds) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(HotIcon::getIconId,hotIconIds);
wrapper.orderByDesc(HotIcon::getIsSystem) // 官方默认
.orderByAsc(HotIcon::getIconTitle)
.orderByAsc(HotIcon::getSort);
return this.list(wrapper);
}
@Override
public List treeList(List iconList) {
HashMap map = new HashMap<>();
List list = new ArrayList<>();
List result = new ArrayList<>();
for (HotIcon hotIcon : iconList) {
if(hotIcon.getParentId() == null){
list.add(hotIcon);
}
map.put(hotIcon.getIconId(),hotIcon);
}
for (HotIcon hotIcon : iconList) {
if(hotIcon.getParentId() == null){
continue;
}
HotIcon parent = map.get(hotIcon.getParentId());
if(parent.getChildrenList() == null){
parent.setChildrenList(new ArrayList<>());
}
parent.getChildrenList().add(hotIcon);
}
for (HotIcon hotIcon : list) {
result.add( map.get(hotIcon.getIconId()));
}
return result;
}
}