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;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-12-04
 */
@Service
public class SmartAuthorGroupServiceImpl extends ServiceImpl<SmartAuthorGroupMapper, SmartAuthorGroup> implements SmartAuthorGroupService {
    @Autowired
    private SmartAuthorGroupMapper smartAuthorGroupMapper;
    @Autowired
    public SmartUserMapper smartUserMapper;
    @Autowired
    public SmartApplyMapper smartApplyMapper;
    @Autowired
    public SmartDepartmentMapper smartDepartmentMapper;
    @Autowired
    public SmartMenuMapper smartMenuMapper;

    @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<SmartAuthorGroup> queryPageSmartAuthorGroups(int currentPage, int pageCount, String name) {
        Page<SmartAuthorGroup> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
        //queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<SmartAuthorGroup> 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<SmartAuthorGroup> getAuthorGroupList() {
        QueryWrapper<SmartAuthorGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        List<SmartAuthorGroup> result = smartAuthorGroupMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<SmartAuthorGroup> getAuthorGroupByKey(QueryWrapper<SmartAuthorGroup> queryWrapper) {
        List<SmartAuthorGroup> result = smartAuthorGroupMapper.selectList(queryWrapper);
        return result;
    }


    @Override
    public List<SmartAuthorGroup> smartAuthorGroup(Integer userId){
        List<SmartAuthorGroup> result = smartAuthorGroupMapper.smartAuthorGroup(userId);
        return result;
    }

    @Override
    public List<SmartAuthorGroup> smartAuthorGroupSuperAdmins(Integer userId){
        List<SmartAuthorGroup> result = smartAuthorGroupMapper.smartAuthorGroupSuperAdmin(userId);
        return result;
    }

    @Override
    public List<AuthorListGroup> queryCommentTreeRecords(Integer pid, List<SmartAuthorGroup> lists) {
        List<AuthorListGroup> newTrees = new ArrayList<>();
        List<SmartAuthorGroup> datas = lists.stream().filter(e -> e.getParentId().equals(pid)).collect(Collectors.toList());
        for (SmartAuthorGroup data : datas) {
            QueryWrapper<SmartUser> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("deleted", 0);
            List<String> userIds = Arrays.asList(data.getUserId().split(","));
            queryWrapper1.in("id", userIds);
            List<SmartUser> list1 = smartUserMapper.selectList(queryWrapper1);
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("data", list1);
            QueryWrapper<SmartMenu> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("deleted", 0);
            List<String> applyIds = Arrays.asList(data.getApplyId().split(","));
            queryWrapper2.in("id", applyIds);
            List<SmartMenu> list2 = smartMenuMapper.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<AuthorListGroup> 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<SmartAuthorGroupManager> getSmartAuthorGroupManager(String groupId){
        List<SmartAuthorGroupManager> 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<SmartDepartment> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("deleted", 0);
                    queryWrapper1.in("id", Arrays.asList(manageId));
                    List<SmartDepartment> list1 = smartDepartmentMapper.selectList(queryWrapper1);
                    List<JSONObject> departmentManageJson = new ArrayList<>();
                    if (list1.size()>0){
                        for (int j = 0; j < list1.size(); j++) {
                            JSONObject jsonObject1 = new JSONObject();
                            jsonObject1.put("id", list1.get(j).getId());
                            jsonObject1.put("name", list1.get(j).getName());
                            departmentManageJson.add(jsonObject1);
                        }
                    }
                    list.get(i).setDepartmentManageJson(departmentManageJson);
                }
                if (smartAuthorGroupManager.getDepartmentView()!=null && !smartAuthorGroupManager.getDepartmentView().equals("")){
                    String[] viewId = smartAuthorGroupManager.getDepartmentView().split(",");
                    QueryWrapper<SmartDepartment> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("deleted", 0);
                    queryWrapper1.in("id", Arrays.asList(viewId));
                    List<SmartDepartment> list1 = smartDepartmentMapper.selectList(queryWrapper1);
                    List<JSONObject> departmentViewJson = new ArrayList<>();
                    if (list1.size()>0){
                        for (int j = 0; j < list1.size(); j++) {
                            JSONObject jsonObject1 = new JSONObject();
                            jsonObject1.put("id", list1.get(j).getId());
                            jsonObject1.put("name", list1.get(j).getName());
                            departmentViewJson.add(jsonObject1);
                        }
                    }
                    list.get(i).setDepartmentViewJson(departmentViewJson);
                }
            }
        }
        return list;
    }
}
