package com.zcloud.controller.depository;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.depository.LabelFactoryService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：标签库
 * 作者：luoxiaobao
 * 时间：2022-12-27
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/labelFactory")
public class LabelFactoryController extends BaseController {

    @Resource
    private LabelFactoryService labelfactoryService;

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        page.setPd(pd);
        List<PageData> varList = labelfactoryService.list(page);    //列出LabelFacory列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**
     * description:保存根节点方法
     *
     * @author sparrow
     *
     * @return save result
     * @throws Exception all
     */
    @RequestMapping(value = "/addAncestors")
    @ResponseBody
    public Object addAncestors() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("result", "success");
        PageData pd = this.getPageData();
        PageData condition = new PageData();
        condition.put("NAME",pd.getString("NAME"));
        List<PageData> warden = labelfactoryService.listAll(condition);
        if (warden != null && warden.size() >0){
            map.put("code","9999");
            map.put("errorMessage","节点名重复");
            return map;
        }
        pd.put("BUS_LABEL_FACTORY_ID", this.get32UUID());    //主键
        pd.put("PARENT_ID", "0");
        pd.put("ANCESTORS_ID", pd.getString("BUS_LABEL_FACTORY_ID"));
        pd.put("IS_ANCESTORS_FLAG", "1");
        pd.put("CREATOR_ID", Jurisdiction.getUSER_ID());
        pd.put("CREATED_TIME", new Date());
        pd.put("ISDELETE", "0");
        pd.put("LEVEL", "0");
        labelfactoryService.save(pd);
        map.put("code","0");

        return map;
    }

    @RequestMapping(value = "/getAncestors")
    @ResponseBody
    public Object getAncestors() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = this.getPageData();
        List<PageData> list = labelfactoryService.findAllAncestors(pd);
        map.put("list",list);
        map.put("result", errInfo);
        return map;
    }

    @RequestMapping(value = "/tree")
    @ResponseBody
    public Object tree() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        List<PageData> tree = labelfactoryService.getTree(pd);    //列出LabelFacory列表
        map.put("tree", tree);
        map.put("result", "success");
        return map;
    }

    @RequestMapping(value = "/saveTree")
    @ResponseBody
    public Object saveTree() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageData pd = this.getPageData();
        // 此处一定会炸雷（解决方案见历史版本）
        List<PageData> tree = labelfactoryService.saveTree(pd);    //列出LabelFacory列表
        map.put("tree", tree);
        map.put("result", "success");
        return map;
    }

    /**
     * 新增
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = this.getPageData();
        pd.put("LABELFACORY_ID", this.get32UUID());    //主键
        labelfactoryService.save(pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 删除
     *
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("labelfacory:del")
    @ResponseBody
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        labelfactoryService.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions("labelfacory:edit")
    @ResponseBody
    public Object edit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        labelfactoryService.edit(pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 去修改页面获取数据
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit")
    @ResponseBody
    public Object goEdit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = labelfactoryService.findById(pd);    //根据ID读取
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 批量删除
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/deleteAll")
    @RequiresPermissions("labelfacory:del")
    @ResponseBody
    public Object deleteAll() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String DATA_IDS = pd.getString("DATA_IDS");
        if (Tools.notEmpty(DATA_IDS)) {
            String ArrayDATA_IDS[] = DATA_IDS.split(",");
            labelfactoryService.deleteAll(ArrayDATA_IDS);
            errInfo = "success";
        } else {
            errInfo = "error";
        }
        map.put("result", errInfo);                //返回结果
        return map;
    }

}
