package com.zcloud.controller.bus;

import com.alibaba.fastjson.JSONObject;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.bus.HiddenRegionService;
import com.zcloud.service.remote.RemoteCorpService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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;

@Controller
@RequestMapping("/hiddenRegion")
public class HiddenRegionController extends BaseController {
    @Autowired
    private HiddenRegionService hiddenRegionService;

    @Resource
    private RemoteCorpService remoteCorpService;
    @PostMapping("/helloworld")
    @ResponseBody
    public Object hello() {
        JSONObject objects = new JSONObject();
        objects.put("curPage","1");
        objects.put("limit","10");
        Object corpInfo = remoteCorpService.getCorpInfo(objects.toJSONString());
        System.out.print(corpInfo);
        return corpInfo;
    }
    /**
     * 新增
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object add(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String parentId = pd.getString("HIDDENREGION_ID");
        if(!Tools.isEmpty(pd.getString("PARENT_ID"))){
            pd.put("PARENT_ID",pd.getString("PARENT_ID"));
        }else if(pd.getString("HIDDENREGION_ID").isEmpty()){
            pd.put("PARENT_ID","0");
        }else{
            pd.put("PARENT_ID",parentId);
        }
        pd.put("HIDDENREGION_ID",this.get32UUID());
        pd.put("ISDELETE","0");
        pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        pd.put("CREATOR",Jurisdiction.getUsername());//添加人
        pd.put("CREATTIME", DateUtil.date2Str(new Date()));//添加时间
        pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
        pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
        hiddenRegionService.save(pd);
        map.put("result",errInfo);
        return map;
    }

    /**
     * 删除
     * @param HIDDENREGION_ID
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public Object delete(@RequestParam String HIDDENREGION_ID) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd.put("HIDDENREGION_ID", HIDDENREGION_ID);
        if(hiddenRegionService.getListSubset(pd).size() > 0){//判断是否有子级，是：不允许删除
            errInfo = "fail";
            map.put("result", "errInfo");				//返回结果
            map.put("msg","下级有数据，请先删除下级数据");
            return map;
        }else {
            hiddenRegionService.delete(pd);			//执行删除
        }
        map.put("result", errInfo);					//返回结果
        return map;
    }



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



    /**
     * 列表（根据ID查）
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/getListById")
    @ResponseBody
    public Object getListById(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> postList = hiddenRegionService.getListById(pd);
        map.put("result",errInfo);
        map.put("postList",postList);

        return map;
    }
    /**
     * 列表（根据ID查下级）
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/getListSubset")
    @ResponseBody
    public Object getListSubset(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> varList = hiddenRegionService.getListSubset(pd);
        map.put("varList",varList);
        map.put("page",page);
        map.put("result",errInfo);
        return map;
    }


    /**
     * 列表（根据ID查下级）
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/getPaging")
    @ResponseBody
    public Object getPaging(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 = hiddenRegionService.getPaging(page);
        map.put("result",errInfo);
        map.put("varList",varList);
        map.put("page",page);
        return map;
    }


    /**
     * 用于逐级返回的方法
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/getBack")
    @ResponseBody
    public Object getBack(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());  //当前企业id
        String HIDDENREGION_ID = null;
        if( pd.get("HIDDENREGION_ID")!=null && Tools.notEmpty( pd.get("HIDDENREGION_ID").toString())) {
            HIDDENREGION_ID = null == pd.get("HIDDENREGION_ID")?"":pd.get("HIDDENREGION_ID").toString();
            pd.put("HIDDENREGION_ID", HIDDENREGION_ID);										//当作上级ID
        }
        page.setPd(pd);
        List<PageData> varList = hiddenRegionService.getBack(page);
        if("".equals(HIDDENREGION_ID) || "0".equals(HIDDENREGION_ID)) {
            map.put("PARENT_ID", "0");											//上级ID
        }else {
            map.put("PARENT_ID", hiddenRegionService.findBypId(pd).getString("PARENT_ID"));	//上级ID
        }
        map.put("result",errInfo);
        map.put("varList",varList);
        map.put("page",page);
        return map;
    }



    /**
     * 列表all
     * @param page
     * @throws Exception
     */
    @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();
        pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
        page.setPd(pd);
        List<PageData> varList = hiddenRegionService.list(page);
        map.put("result",errInfo);
        map.put("varList",varList);
        map.put("page",page);
        return map;
    }
    /**
     * 菜单列表ztree(菜单管理)
     * @return
     */
    @RequestMapping(value="/listAll")
    @ResponseBody
    public Object listAllMenu()throws Exception{
        Map<String,String> map = new HashMap<String,String>();
        String errInfo = "success";
        String CORPINFO_ID = Jurisdiction.getCORPINFO_ID();
        JSONArray arr = JSONArray.fromObject(hiddenRegionService.listAll("0",CORPINFO_ID));
        String json = arr.toString();
        json = json.replaceAll("HIDDENREGION_ID", "id").replaceAll("PARENT_ID", "pId").replaceAll("HIDDENREGION", "name").replaceAll("subhiddenRegion", "nodes").replaceAll("hashiddenRegion", "checked").replaceAll(",\"nodes\":\\[\\]", "").replaceAll("HIDDENREGION_URL", "url");
        map.put("zTreeNodes", json);
        map.put("result", errInfo);
        return map;
    }

}
