package com.zcloud.controller.electronicFence;

import com.alibaba.fastjson.JSON;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.electronicFence.ElectronicFenceService;
import com.zcloud.util.InitPageDataUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.ReturnMap;
import com.zcloud.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 说明：电子围栏
 * 作者：wangxuan
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/electronicfence")
public class ElectronicFenceController extends BaseController {

    @Autowired
    private ElectronicFenceService electronicFenceService;
    @Autowired
    private InitPageDataUtil initPageDataUtil;

    /**
     * @Description: 列表
     * @Author: dearLin
     * @Date: 2024/1/30/030 9:38
     * @Param: [com.zcloud.entity.Page] [page]
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Page page) {
        PageData pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        pd.put("PARENT_ID", pd.getString("ELECTRONIC_FENCE_AREA_ID"));
        pd.remove("ELECTRONIC_FENCE_AREA_ID");
        page.setPd(pd);
        return ReturnMap.ok().put("varList", electronicFenceService.list(page)).put("page", page);
    }

    /**
     * @Description: 列表
     * @Author: dearLin
     * @Date: 2024/1/30/030 9:38
     * @Param: [com.zcloud.entity.Page] [page]
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/listTree")
    @ResponseBody
    public Object listTree() {
        PageData pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        pd.put("childKey", "nodes");
        return ReturnMap.ok().put("zTreeNodes", JSON.toJSONString(electronicFenceService.listTree(pd)));
    }


    /**
     * @Description: 列表
     * @Author: dearLin
     * @Date: 2024/1/30/030 9:38
     * @Param: [com.zcloud.entity.Page] [page]
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/goEdit")
    @ResponseBody
    public Object goEdit() {
        PageData pd = this.getPageData();
        return ReturnMap.ok().put("pd", electronicFenceService.findById(pd));

    }

    /**
     * @Description: 新增
     * @Author: dearLin
     * @Date: 2024/1/30/030 9:38
     * @Param: [] []
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save() {
        PageData pd = this.getPageData();
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        initPageDataUtil.initSave(pd, "ELECTRONIC_FENCE_AREA_ID");
        electronicFenceService.save(pd);
        return ReturnMap.ok();
    }

    /**
     * @Description: 修改
     * @Author: dearLin
     * @Date: 2024/1/30/030 9:38
     * @Param: [] []
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit() {
        PageData pd = this.getPageData();
        initPageDataUtil.initEdit(pd);
        electronicFenceService.edit(pd);
        return ReturnMap.ok();
    }

    /**
     * @Description: 修改
     * @Author: dearLin
     * @Date: 2024/1/30/030 9:38
     * @Param: [] []
     * @Return: java.lang.Object
     */
    @RequestMapping(value = "/removeByIds")
    @ResponseBody
    public Object removeByIds() {
        PageData pd = this.getPageData();
        // 判断一下当前要删除的是否有下级 有下级 禁止删除
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        if (electronicFenceService.hasChild(pd)) return ReturnMap.error("该区域下有子区域，禁止删除，请先删除子区域后删除该区域。");
        initPageDataUtil.initEdit(pd);
        electronicFenceService.removeByIds(pd);
        return ReturnMap.ok();
    }
}
