package com.ruoyi.web.controller.ems;

import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ems.domain.Area;
import com.ruoyi.ems.domain.EmsFacs;
import com.ruoyi.ems.domain.ObjTagRel;
import com.ruoyi.ems.enums.BusObjType;
import com.ruoyi.ems.model.TreeEntity;
import com.ruoyi.ems.service.IAreaService;
import com.ruoyi.ems.service.IEmsFacsService;
import com.ruoyi.ems.service.IObjTagRelService;
import com.ruoyi.ems.util.AreaUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 区域对象Controller
 *
 * @author ruoyi
 * @date 2024-11-01
 */
@RestController
@RequestMapping("/ems/basecfg/area")
@Api(value = "AreaController", description = "区域管理")
public class AreaController extends BaseController {
    @Autowired
    private IAreaService areaService;

    @Autowired
    private IEmsFacsService facsService;

    @Autowired
    private IObjTagRelService objTagRelService;

    /**
     * 查询区域对象列表
     */
    @PreAuthorize("@ss.hasPermi('ems:area:list')")
    @GetMapping("/list")
    public AjaxResult list(Area area) {
        List<Area> list = areaService.selectArea(area);
        return success(list);
    }

    /**
     * 查询区域对象列表
     */
    @PreAuthorize("@ss.hasPermi('ems:area:list')")
    @GetMapping("/listDetail")
    public AjaxResult listDetail(Area area) {
        List<Area> list = areaService.selectAreaDetail(area);
        return success(list);
    }

    /**
     * 查询区域对象列表
     */
    @PreAuthorize("@ss.hasPermi('ems:area:list')")
    @GetMapping("/getAreaListByTag")
    public AjaxResult getAreaListByTag(@RequestParam(name = "parentCode", required = false, defaultValue = "0") String parentCode,
        @RequestParam(name = "recursion", required = false, defaultValue = "true") boolean recursion,
        @RequestParam(name = "tagCode") String tagCode) {
        // 查询区域树
        List<Area> areaTree = areaService.selectAreaTree(parentCode, recursion);

        // 查询列表
        List<ObjTagRel> tagRels = objTagRelService.selectListByTagCode(BusObjType.AREA.getValue(), tagCode);
        Set<String> facsAreaCodes = tagRels.stream().map(ObjTagRel::getObjCode).collect(Collectors.toSet());

        // 过滤树节点
        List<Area> areaTargets = AreaUtils.compressTree(areaTree);

        Iterator<Area> it = areaTargets.iterator();
        while (it.hasNext()) {
            if (!facsAreaCodes.contains(it.next().getAreaCode())) {
                it.remove();
            }
        }

        return success(areaTargets);
    }

    /**
     * 获取服务区树
     *
     * @param rootCode 根节点编码
     * @return 区域位置树
     */
    @GetMapping(value = "/getAreaTree")
    public AjaxResult getAreaTree(@RequestParam(name = "rootCode") String rootCode,
        @RequestParam(name = "recursion", required = false) boolean recursion) {
        List<Area> areas = areaService.selectAreaTree(rootCode, recursion);
        List<TreeEntity> ret = AreaUtils.convertAreaTree(areas);
        return success(ret);
    }

    /**
     * 查询区域树 (根据设施类型过滤)
     *
     * @param facsCategory    设施类别
     * @param facsSubCategory 设施子类别
     */
    @GetMapping("/getAreaTreeByFacsCategory")
    public AjaxResult getAreaTreeByFacsCategory(
        @RequestParam(name = "parentCode", required = false, defaultValue = "0") String parentCode,
        @RequestParam(name = "facsCategory") String facsCategory,
        @RequestParam(name = "facsSubCategory", required = false) String facsSubCategory,
        @RequestParam(name = "recursion", required = false) boolean recursion) {

        // 查询区域树
        List<Area> areaTree = areaService.selectAreaTree(parentCode, recursion);

        // 查询设施列表
        EmsFacs facsParam = new EmsFacs();
        facsParam.setFacsCategory(facsCategory);
        facsParam.setFacsSubCategory(facsSubCategory);
        List<EmsFacs> facsList = facsService.selectEmsFacsList(facsParam);
        Set<String> facsAreaCodes = facsList.stream().map(EmsFacs::getRefArea).collect(Collectors.toSet());

        // 过滤树节点
        areaTree = AreaUtils.filterTree(areaTree, facsAreaCodes);
        List<TreeEntity> retList = AreaUtils.convertAreaTree(areaTree);
        return success(retList);
    }

    /**
     * 查询区域树 (根据对象标签过滤)
     *
     * @param parentCode 区域父节点
     * @param recursion  低谷查询
     * @param tagCode    标签
     * @return 区域位置树
     */
    @GetMapping(value = "/getAreaTreeByTag")
    public AjaxResult getAreaTreeByTag(@RequestParam(name = "parentCode", required = false, defaultValue = "0") String parentCode,
        @RequestParam(name = "recursion", required = false, defaultValue = "true") boolean recursion,
        @RequestParam(name = "tagCode") String tagCode) {
        // 查询区域树
        List<Area> areaTree = areaService.selectAreaTree(parentCode, recursion);

        // 查询列表
        List<ObjTagRel> tagRels = objTagRelService.selectListByTagCode(BusObjType.AREA.getValue(), tagCode);
        Set<String> facsAreaCodes = tagRels.stream().map(ObjTagRel::getObjCode).collect(Collectors.toSet());

        // 过滤树节点
        areaTree = AreaUtils.filterTree(areaTree, facsAreaCodes);
        List<TreeEntity> retList = AreaUtils.convertAreaTree(areaTree);

        return success(retList);
    }

    /**
     * 获取区域对象详细信息
     */
    @PreAuthorize("@ss.hasPermi('ems:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(areaService.selectAreaById(id));
    }

    @PreAuthorize("@ss.hasPermi('ems:area:query')")
    @GetMapping(value = "/getByCode")
    public AjaxResult getByCode(@RequestParam("areaCode") String areaCode) {
        return success(areaService.selectAreaByCode(areaCode));
    }

    /**
     * 新增区域对象
     */
    @PreAuthorize("@ss.hasPermi('ems:area:add')")
    @Log(title = "区域对象", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Area area) {
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改区域对象
     */
    @PreAuthorize("@ss.hasPermi('ems:area:edit')")
    @Log(title = "区域对象", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Area area) {
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除区域对象
     */
    @PreAuthorize("@ss.hasPermi('ems:area:remove')")
    @Log(title = "区域对象", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(areaService.deleteAreaByIds(ids));
    }

}
