package com.jeeplus.test.help.controller;

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.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.help.domain.HelpTree;
import com.jeeplus.test.help.service.HelpTreeService;
import com.jeeplus.test.help.service.dto.HelpTreeDTO;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api("帮助管理树形")
@RestController
@RequestMapping(value = "/help_tree")
public class HelpTreeController {

    @Resource
    private HelpTreeService helpTreeService;

    /**
     * 查询帮助管理树形列表
     * @param helpTreeDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询帮助管理树形列表")
    @PreAuthorize("hasAuthority('help:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<HelpTreeDTO>> data(HelpTreeDTO helpTreeDTO, Page<HelpTreeDTO> page) throws Exception {
        page.setSize(-1);
        IPage<HelpTreeDTO> result = new Page<HelpTreeDTO>();
        if(ObjectUtil.isNotEmpty(helpTreeDTO)){
            if(StringUtils.isNotBlank(helpTreeDTO.getName())){
                //根据type模糊查询
                List<HelpTree> helpTreeList = helpTreeService.list(new QueryWrapper<HelpTree>().lambda()
                        .like(StringUtils.isNotBlank(helpTreeDTO.getName()), HelpTree::getName, helpTreeDTO.getName())
                );
                List<String> idList = new ArrayList<>();
                helpTreeList.stream().forEach(item->{
                    if (StringUtils.isNotBlank(item.getParentIds())){
                        String[] split = item.getParentIds().split(",");
                        for (String s : split) {
                            idList.add(s);
                        }
                    }
                    List<HelpTree> list = helpTreeService.list(new QueryWrapper<HelpTree>().lambda().like(HelpTree::getParentIds, "," + item.getId() + ","));
                    idList.addAll(list.stream().map(HelpTree::getId).collect(Collectors.toList()));
                    idList.add(item.getId());
                });
                List<String> ids = idList.stream().distinct().filter(item->{
                    if (!"0".equals(item)){
                        return true;
                    }
                    return false;
                }).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(ids)){
                    QueryWrapper<HelpTree> wrapper = new QueryWrapper<HelpTree>()
                            .in("ht.id",idList);
                    result = helpTreeService.findList (page,wrapper);
                }
            }else{
                result = helpTreeService.findList (page,new QueryWrapper<HelpTree>());
            }
        }
        return ResponseEntity.ok (result);
    }


    /**
     * 查询帮助管理树形数据
     * @param id
     * @return
     */
    @ApiLog("查询帮助管理树形数据")
    @PreAuthorize("hasAnyAuthority('help:view','help:add','help:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        HelpTreeDTO helpTreeDTO = helpTreeService.queryById ( id );
        return ResponseEntity.ok (helpTreeDTO);
    }

    /**
     * 查询全部一级数据
     * @return
     */
    @ApiLog("查询全部一级数据")
    @GetMapping("getList")
    public ResponseEntity<List<HelpTreeDTO>> getList() {
        List<HelpTreeDTO> list = helpTreeService.getList();
        return ResponseEntity.ok (list);
    }

    /**
     * 保存帮助管理树形
     * @param helpTreeDTO
     * @return
     */
    @ApiLog(value = "修改/新增帮助管理树形", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('help:add','help:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody HelpTreeDTO helpTreeDTO) {
        helpTreeService.saveType(helpTreeDTO);
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 删除帮助管理树形
     * @param ids
     * @return
     */
    @ApiLog(value = "删除帮助管理树形", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('help:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return helpTreeService.deleteByIds(ids);
    }


}
