package com.zcloud.controller.corp;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.corp.CorpPlsInfoService;
import com.zcloud.util.*;
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;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.zcloud.util.PLSUtil.checkWebSiteStatus;

/**
 * 说明：企业人员定位系统参数表
 * 作者：chenxinying
 * 时间：2024-01-10
 * 官网：www.qdkjchina.com
 */
@Controller
@RequestMapping("/corpplsinfo")
public class CorpPlsInfoController extends BaseController {

    @Autowired
    private CorpPlsInfoService corpplsinfoService;

    @Resource
    private RedisUtil redisUtil;

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

    /**
     * 删除
     *
     * @param out
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    // @RequiresPermissions("corpplsinfo: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();
        corpplsinfoService.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    // @RequiresPermissions("corpplsinfo: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();
        corpplsinfoService.edit(pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    // @RequiresPermissions("corpplsinfo: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();
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        page.setPd(pd);
        List<PageData> varList = corpplsinfoService.list(page);    //列出corpplsinfo列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 去修改页面获取数据
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit")
//	// @RequiresPermissions("corpplsinfo:edit")
    @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 = corpplsinfoService.findById(pd);    //根据ID读取
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

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

    /**
     * 刷新网站状态接口。
     * 该方法旨在更新Redis中存储的网站状态信息，确保信息的实时性和准确性。
     * 目前该方法实现了基础的框架搭建，但实际的刷新逻辑需要根据业务需求进一步完善。
     *
     * @return ReturnMap 返回操作结果的状态码和消息。
     * @throws Exception 如果操作过程中出现异常，会抛出Exception。
     */
    @RequestMapping(value = "/refreshWebsiteStatus")
    @ResponseBody
    public ReturnMap refreshWebsiteStatus() throws Exception {
        PageData pageData = new PageData();
        pageData.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        List<PageData> plsInfoList = corpplsinfoService.listAll(pageData);
        ReturnMap ok = ReturnMap.ok();
        for (PageData plsInfo : plsInfoList) {
            boolean webSiteStatus = checkWebSiteStatus(plsInfo);
            ok.put("webSiteStatus", webSiteStatus);
            redisUtil.hset(Const.INTEGRATED_WHB_WEBSITE_SURVIVAL_CHECK_KEY, plsInfo.getString("CORPINFO_ID"), webSiteStatus);
        }
        return ok;
    }
}
