package com.repair.controller;


import com.repair.api.RepairAreaControllerAPI;
import com.repair.common.utils.paramUtils;
import com.repair.model.pojo.RepairArea;
import com.repair.model.request.updateRepairAreaRequest;
import com.repair.model.result.CommonResult;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.RepairAreaTreeVo;
import com.repair.model.vo.RepairSchoolVo;
import com.repair.services.RepairAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-07-12
 */
@RestController
public class RepairAreaController implements RepairAreaControllerAPI {

    @Autowired
    private RepairAreaService repairAreaService;

    @Override
    public CommonResult InsertRepairArea(RepairArea repairArea, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        int result = repairAreaService.insertRepairArea(repairArea);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    @Override
    public CommonResult updateRepairAreaById(updateRepairAreaRequest urar, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        RepairArea ra = new RepairArea();
        ra.setId(urar.getId());
        ra.setName(urar.getName());
        int result = repairAreaService.updateRepairArea(ra);

        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    @Override
    public CommonResult queryPageRepairAreas(int currentPage, int pageCount, String name) {
        PageUtils<RepairArea> result = repairAreaService.queryPageRepairAreas(currentPage, pageCount, name);
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryRepairSchools() {
        List<RepairSchoolVo> result = repairAreaService.queryRepairSchools();
        return CommonResult.ok(result);
    }

    @Override
    public CommonResult deleteRepairAreaById(int id) {
        RepairArea data = repairAreaService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairAreaService.deleteRepairAreaById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

    @Override
    public CommonResult queryRepairAreaTree(int schoolId) {
        List<RepairArea> result = repairAreaService.getRepairAreas();
        List<RepairAreaTreeVo> datas = QueryAreaTreeRecords(schoolId, result);
        return CommonResult.ok(datas);
    }

    /*
    根据父类ID获取树形菜单数据
     */
    private List<RepairAreaTreeVo> QueryAreaTreeRecords(Integer parentID, List<RepairArea> lists) {
        List<RepairAreaTreeVo> newTrees = new ArrayList<>();

        List<RepairArea> datas = lists.stream().filter(e -> e.getParentId() == parentID).collect(Collectors.toList());

        for (RepairArea data : datas) {
            RepairAreaTreeVo item = RepairAreaTreeVo.builder()
                    .id(data.getId())
                    .parentId(data.getParentId())
                    .name(data.getName())
                    .build();
            List<RepairAreaTreeVo> news = QueryAreaTreeRecords(item.getId(), lists);
            if (news == null || news.size() == 0) {
                newTrees.add(item);
                continue;
            } else {
                item.setChildren(news);
                newTrees.add(item);
            }
        }

        return newTrees;
    }

    @Override
    public CommonResult queryRepairAreaSecond(int schoolId) {
        List<RepairArea> result = repairAreaService.getRepairAreas();
        List<RepairAreaTreeVo> datas = QueryAreaTreeRecords(schoolId, result, 2);
        return CommonResult.ok(datas);
    }

    /*
    查询二层楼栋
     */
    private List<RepairAreaTreeVo> QueryAreaTreeRecords(Integer parentID, List<RepairArea> lists, int i) {
        i--;
        List<RepairAreaTreeVo> newTrees = new ArrayList<>();

        List<RepairArea> datas = lists.stream().filter(e -> e.getParentId() == parentID).collect(Collectors.toList());
        if(i < 0){
            datas = new ArrayList<>();
        }
        for (RepairArea data : datas) {
            RepairAreaTreeVo item = RepairAreaTreeVo.builder()
                    .id(data.getId())
                    .parentId(data.getParentId())
                    .name(data.getName())
                    .build();
            List<RepairAreaTreeVo> news = QueryAreaTreeRecords(item.getId(), lists, i);
            if (news == null || news.size() == 0) {
                newTrees.add(item);
                continue;
            } else {
                item.setChildren(news);
                newTrees.add(item);
            }
        }

        return newTrees;
    }

}

