package com.xjrsoft.module.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.system.dto.AddXjrMenuGroupDto;
import com.xjrsoft.module.system.dto.XjrMenuGroupMenuListDto;
import com.xjrsoft.module.system.entity.XjrMenuGroup;
import com.xjrsoft.module.system.entity.XjrMenuGroupSet;
import com.xjrsoft.module.system.mapper.XjrMenuGroupMapper;
import com.xjrsoft.module.system.mapper.XjrMenuGroupSetMapper;
import com.xjrsoft.module.system.service.IXjrMenuGroupSetService;
import com.xjrsoft.module.system.vo.XjrMenuGroupMenuListVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @title: 功能分类设置
* @Author dzx
* @Date: 2024-08-09
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class XjrMenuGroupSetServiceImpl extends MPJBaseServiceImpl<XjrMenuGroupSetMapper, XjrMenuGroupSet> implements IXjrMenuGroupSetService {
    private final XjrMenuGroupSetMapper setMapper;

    private final XjrMenuGroupMapper groupMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(XjrMenuGroupSet xjrMenuGroupSet) {
        setMapper.insert(xjrMenuGroupSet);
        for (XjrMenuGroup xjrMenuGroup : xjrMenuGroupSet.getXjrMenuGroupList()) {
            xjrMenuGroup.setXjrMenuGroupSetId(xjrMenuGroupSet.getId());
            groupMapper.insert(xjrMenuGroup);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(XjrMenuGroupSet xjrMenuGroupSet) {
        setMapper.updateById(xjrMenuGroupSet);
        //********************************* XjrMenuGroup  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<XjrMenuGroup> xjrMenuGroupList = groupMapper.selectList(Wrappers.lambdaQuery(XjrMenuGroup.class).eq(XjrMenuGroup::getXjrMenuGroupSetId, xjrMenuGroupSet.getId()).select(XjrMenuGroup::getId));
            List<Long> xjrMenuGroupIds = xjrMenuGroupList.stream().map(XjrMenuGroup::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> xjrMenuGroupOldIds = xjrMenuGroupSet.getXjrMenuGroupList().stream().map(XjrMenuGroup::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> xjrMenuGroupRemoveIds = xjrMenuGroupIds.stream().filter(item -> !xjrMenuGroupOldIds.contains(item)).collect(Collectors.toList());

            for (XjrMenuGroup xjrMenuGroup : xjrMenuGroupSet.getXjrMenuGroupList()) {
                //如果不等于空则修改
                if (xjrMenuGroup.getId() != null) {
                    groupMapper.updateById(xjrMenuGroup);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    xjrMenuGroup.setXjrMenuGroupSetId(xjrMenuGroupSet.getId());
                    groupMapper.insert(xjrMenuGroup);
                }
            }
            //已经不存在的id 删除
            if(xjrMenuGroupRemoveIds.size() > 0){
                groupMapper.deleteBatchIds(xjrMenuGroupRemoveIds);
            }
        }
        //********************************* XjrMenuGroup  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        setMapper.deleteBatchIds(ids);
        groupMapper.delete(Wrappers.lambdaQuery(XjrMenuGroup.class).in(XjrMenuGroup::getXjrMenuGroupSetId, ids));

        return true;
    }

    @Override
    public List<XjrMenuGroupMenuListVo> getMenuList(XjrMenuGroupMenuListDto dto) {
        return groupMapper.getMenuList(dto);
    }

    @Override
    public Boolean setMenu(AddXjrMenuGroupDto dto) {
        groupMapper.delete(
                new QueryWrapper<XjrMenuGroup>().lambda()
                        .eq(XjrMenuGroup::getXjrMenuGroupSetId, dto.getXjrMenuGroupSetId())
        );
        for (Long xjrMenuId : dto.getXjrMenuIds()) {
            groupMapper.insert(
                    new XjrMenuGroup(){{
                        setCreateDate(new Date());
                        setXjrMenuId(xjrMenuId);
                        setXjrMenuGroupSetId(dto.getXjrMenuGroupSetId());
                    }}
            );
        }
        return true;
    }

    @Override
    public List<XjrMenuGroup> getXjrMenuGroupMenu() {
        return groupMapper.selectList(new QueryWrapper<XjrMenuGroup>());
    }
}
