package com.usky.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.website.domain.SiteArticle;
import com.usky.website.domain.SiteCategory;
import com.usky.website.mapper.SiteCategoryMapper;
import com.usky.website.service.SiteArticleService;
import com.usky.website.service.SiteCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 官网_栏目管理 服务实现类
 * </p>
 *
 * @author ya
 * @since 2022-08-31
 */
@Service
public class SiteCategoryServiceImpl extends AbstractCrudService<SiteCategoryMapper, SiteCategory> implements SiteCategoryService {

    @Autowired
    private SiteArticleService siteArticleService;

    @Override
    public List<SiteCategory> siteCategoryList(String categoryName) {
        LambdaQueryWrapper<SiteCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SiteCategory::getStatus, 1)
                .like(StringUtils.isNotBlank(categoryName),SiteCategory::getCategoryName,categoryName)
                .orderByAsc(SiteCategory::getSortindex);
        List<SiteCategory> list = this.list(queryWrapper);
        //取得所有parentId为0的数据，也就是一级目录
        //用于封装数据，取得他的孩子（也就是下级目录）的数据
        List<SiteCategory> list1 = list.stream().filter(subjectVO ->
                subjectVO.getPid() == 0
        ).map((menu) -> {
            menu.setChildren(getChildrenData(menu, list));
            return menu;
        }).collect(Collectors.toList());
        return list1;
    }


    @Override
    public List<SiteCategory> siteCategorySonList(Integer pid) {
        LambdaQueryWrapper<SiteCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SiteCategory::getStatus, 1)
                .eq(SiteCategory::getPid, pid)
                .orderByAsc(SiteCategory::getSortindex);
        List<SiteCategory> list = this.list(queryWrapper);
        return list;
    }


    @Override
    public void addSiteCategory(SiteCategory siteCategory) {
        LambdaQueryWrapper<SiteCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SiteCategory::getStatus, 1)
                .eq(SiteCategory::getPid, siteCategory.getPid())
                .eq(SiteCategory::getCategoryName, siteCategory.getCategoryName());
        List<SiteCategory> list = this.list(queryWrapper);
        if (list.size() > 0) {
            throw new BusinessException("栏目名称不能重复，请重新填写栏目名称");
        }
        siteCategory.setCreatedate(new Date());
        siteCategory.setStatus(1);
        this.save(siteCategory);
    }

    @Override
    public void updateSiteCategory(SiteCategory siteCategory) {
        LambdaQueryWrapper<SiteCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SiteCategory::getStatus, 1)
                .eq(SiteCategory::getPid, siteCategory.getPid())
                .eq(SiteCategory::getCategoryName, siteCategory.getCategoryName())
                .ne(SiteCategory::getId, siteCategory.getId());
        List<SiteCategory> list = this.list(queryWrapper);
        if (list.size() > 0) {
            throw new BusinessException("栏目名称不能重复，请重新填写栏目名称");
        }
        siteCategory.setModifydate(new Date());
        this.updateById(siteCategory);
    }

    @Override
    public void delSiteCategory(Integer id) {
        if (id == null || id == 0) {
            throw new BusinessException("ID不能等于空或者等于0");
        }
        LambdaQueryWrapper<SiteArticle> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SiteArticle::getCategoryid, id);
        List<SiteArticle> list = siteArticleService.list(queryWrapper);
        if (list.size() > 0) {
            throw new BusinessException("该栏目存在栏目内容不可删除");
        }
        LambdaQueryWrapper<SiteCategory> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(SiteCategory::getPid,id)
                .eq(SiteCategory::getStatus,1);
        List<SiteCategory> list1 = this.list(queryWrapper1);
        if(list1.size() > 0){
            throw new BusinessException("该栏目存在子栏目不可删除");
        }
        this.removeById(id);
    }


    private List<SiteCategory> getChildrenData(SiteCategory root, List<SiteCategory> all) {
        List<SiteCategory> children = all.stream().filter(subjectVO ->
                subjectVO.getPid().equals(root.getId())
        ).map(subjectVO -> {
            subjectVO.setChildren(getChildrenData(subjectVO, all));
            return subjectVO;
        }).collect(Collectors.toList());
        return children;
    }

}
