package com.template.services.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.mapper.*;
import com.template.model.pojo.*;
import com.template.model.pojo.SmartAuthorGroup;
import com.template.mapper.SmartAuthorGroupMapper;
import com.template.model.result.PageUtils;
import com.template.model.weixin.AuthorListGroup;
import com.template.services.SmartAuthorGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author ceshi
* @since 2023-12-04
*/
@Service
public class SmartAuthorGroupServiceImpl extends ServiceImpl implements SmartAuthorGroupService {
@Autowired
private SmartAuthorGroupMapper smartAuthorGroupMapper;
@Autowired
public SmartUserMapper smartUserMapper;
@Autowired
public SmartApplyMapper smartApplyMapper;
@Autowired
public SmartDepartmentMapper smartDepartmentMapper;
@Override
public int insertSmartAuthorGroup(SmartAuthorGroup sa) {
int result = smartAuthorGroupMapper.insert(sa);
return result;
}
@Override
public int updateSmartAuthorGroup(SmartAuthorGroup sa) {
int result = smartAuthorGroupMapper.updateById(sa);
return result;
}
@Override
public PageUtils queryPageSmartAuthorGroups(int currentPage, int pageCount, String name) {
Page page = new Page<>(currentPage, pageCount);
QueryWrapper queryWrapper = new QueryWrapper<>();
//queryWrapper.like(StringUtils.hasText(name), "name", name);
IPage result = smartAuthorGroupMapper.selectPage(page,queryWrapper);
return new PageUtils<>(result);
}
@Override
public int deleteSmartAuthorGroupById(int id) {
int result = smartAuthorGroupMapper.deleteById(id);
return result;
}
@Override
public SmartAuthorGroup getSmartById(int id) {
SmartAuthorGroup result = smartAuthorGroupMapper.selectById(id);
return result;
}
@Override
public List getAuthorGroupList() {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("deleted", 0);
List result = smartAuthorGroupMapper.selectList(queryWrapper);
return result;
}
@Override
public List getAuthorGroupByKey(QueryWrapper queryWrapper) {
List result = smartAuthorGroupMapper.selectList(queryWrapper);
return result;
}
@Override
public List smartAuthorGroup(Integer userId){
List result = smartAuthorGroupMapper.smartAuthorGroup(userId);
return result;
}
@Override
public List queryCommentTreeRecords(Integer pid, List lists) {
List newTrees = new ArrayList<>();
List datas = lists.stream().filter(e -> e.getParentId().equals(pid)).collect(Collectors.toList());
for (SmartAuthorGroup data : datas) {
QueryWrapper queryWrapper1 = new QueryWrapper<>();
queryWrapper1.eq("deleted", 0);
List userIds = Arrays.asList(data.getUserId().split(","));
queryWrapper1.in("id", userIds);
List list1 = smartUserMapper.selectList(queryWrapper1);
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("data", list1);
QueryWrapper queryWrapper2 = new QueryWrapper<>();
queryWrapper2.eq("deleted", 0);
List applyIds = Arrays.asList(data.getApplyId().split(","));
queryWrapper2.in("id", applyIds);
List list2 = smartApplyMapper.selectList(queryWrapper2);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("data", list2);
AuthorListGroup item = AuthorListGroup.builder()
.id(data.getId())
.parentId(data.getParentId())
.name(data.getName())
.userId(jsonObject1.toJSONString())
.applyId(jsonObject2.toJSONString())
.updateTime(data.getUpdateTime())
.createUser(data.getCreateUser())
.updateUser(data.getUpdateUser())
.deleted(data.getDeleted())
.build();
List news = queryCommentTreeRecords(item.getId(), lists);
if (news == null || news.size() == 0) {
newTrees.add(item);
continue;
} else {
item.setAuthorListGroups(news);
newTrees.add(item);
}
}
return newTrees;
}
@Override
public List getSmartAuthorGroupManager(String groupId){
List list = smartAuthorGroupMapper.getSmartAuthorGroupManager(groupId);
if (list.size()>0){
for (int i = 0; i < list.size(); i++) {
SmartAuthorGroupManager smartAuthorGroupManager = list.get(i);
if (smartAuthorGroupManager.getDepartmentManage()!=null && !smartAuthorGroupManager.getDepartmentManage().equals("")){
String[] manageId = smartAuthorGroupManager.getDepartmentManage().split(",");
QueryWrapper queryWrapper1 = new QueryWrapper<>();
queryWrapper1.eq("deleted", 0);
queryWrapper1.in("id", Arrays.asList(manageId));
List list1 = smartDepartmentMapper.selectList(queryWrapper1);
List departmentManageJson = new ArrayList<>();
if (list1.size()>0){
for (int j = 0; j < list1.size(); j++) {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", list1.get(i).getId());
jsonObject1.put("name", list1.get(i).getName());
departmentManageJson.add(jsonObject1);
}
}
list.get(i).setDepartmentManageJson(departmentManageJson);
}
if (smartAuthorGroupManager.getDepartmentView()!=null && !smartAuthorGroupManager.getDepartmentView().equals("")){
String[] viewId = smartAuthorGroupManager.getDepartmentView().split(",");
QueryWrapper queryWrapper1 = new QueryWrapper<>();
queryWrapper1.eq("deleted", 0);
queryWrapper1.in("id", Arrays.asList(viewId));
List list1 = smartDepartmentMapper.selectList(queryWrapper1);
List departmentViewJson = new ArrayList<>();
if (list1.size()>0){
for (int j = 0; j < list1.size(); j++) {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", list1.get(i).getId());
jsonObject1.put("name", list1.get(i).getName());
departmentViewJson.add(jsonObject1);
}
}
list.get(i).setDepartmentViewJson(departmentViewJson);
}
}
}
return list;
}
}