package com.jeeplus.test.help.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.cw.projectBusinessType.domain.CwProjectBusinessType;
import com.jeeplus.test.cw.projectBusinessType.service.dto.CwProjectBusinessTypeDTO;
import com.jeeplus.test.cw.projectBusinessType.service.mapstruct.CwProjectBusinessTypeWrapper;
import com.jeeplus.test.help.domain.Help;
import com.jeeplus.test.help.domain.HelpTree;
import com.jeeplus.test.help.mapper.HelpMapper;
import com.jeeplus.test.help.mapper.HelpTreeMapper;
import com.jeeplus.test.help.service.dto.HelpTreeDTO;
import com.jeeplus.test.help.service.mapstruct.HelpTreeWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class HelpTreeService extends ServiceImpl<HelpTreeMapper, HelpTree> {

    @Resource
    private HelpTreeMapper helpTreeMapper;

    @Resource
    private HelpMapper helpMapper;

    public IPage<HelpTreeDTO> findList(Page<HelpTreeDTO> page, QueryWrapper<HelpTree> queryWrapper){
        queryWrapper.eq("ht.del_flag","0").orderByAsc("ht.sort");
        return helpTreeMapper.findList(page,queryWrapper);
    }

    public HelpTreeDTO queryById(String id) {

        HelpTreeDTO helpTreeDTO = helpTreeMapper.queryById(id);

        return helpTreeDTO;
    }

    public List<HelpTreeDTO> getList() {
        List<HelpTreeDTO> list = helpTreeMapper.getList();
        return list;
    }

    public ResponseEntity saveType(HelpTreeDTO helpTreeDTO) {
        HelpTree helpTree = HelpTreeWrapper.INSTANCE.toEntity(helpTreeDTO);
        if (ObjectUtil.isNotEmpty(helpTreeDTO)) {
            if (StringUtils.isBlank(helpTreeDTO.getId())) {
                if (StringUtils.isNotBlank(helpTreeDTO.getParentId())){
                    if ("0".equals(helpTreeDTO.getParentId())){
                        helpTree.setLevel(1);
                        helpTree.setParentIds("0,");
                    }else{
                        HelpTree byId = this.getById(helpTreeDTO.getParentId());
                        HelpTreeDTO pbt = HelpTreeWrapper.INSTANCE.toDTO(byId);
                        if(ObjectUtil.isNotEmpty(pbt)){
                            helpTree.setLevel(pbt.getLevel()+1);
                            helpTree.setParentIds(pbt.getParentIds()+pbt.getId()+",");
                        }
                    }
                }
            }
        }
        this.saveOrUpdate(helpTree);
        return ResponseEntity.ok("保存成功");
    }

    public ResponseEntity deleteByIds(String ids) {
        String idArray[] =ids.split(",");
        if(CollectionUtil.isNotEmpty(Lists.newArrayList (idArray))){
            List<String> list = this.list(new QueryWrapper<HelpTree>().lambda().in(HelpTree::getParentId, Lists.newArrayList(idArray))).stream().distinct().map(HelpTree::getId).collect(Collectors.toList());
            this.removeByIds (list);
        }
        this.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除成功");
    }


}
