package com.pqs9900.controller.webtool;

import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.commons.device.Tree;
import com.njcn.pojo.configuration.Device;
import com.njcn.service.configuration.DeviceService;
import com.njcn.service.configuration.TreeService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.PubUtils;
import com.njcn.utils.XssFilterUtil;
import com.pqs9900.controller.business.DeviceController;
import com.pqs9900.service.webtool.CsvDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author cdf
 * @Date 2020/6/3 13:25
 **/
@Controller
@RequestMapping(value = "deviceTree")
public class DeviceTreeController {

    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);

    @Resource
    private DeviceService getDeviceInfo;

    @Resource
    private TreeService getDeviceTree;

    @Resource
    private CsvDataService csvDataService;


    /**
     * 终端管理界面终端树
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "deviceTrees", method = RequestMethod.POST)
    @ResponseBody
    public List<Tree> getdeviceMainTree(HttpServletRequest request, HttpSession session) {
        if (session.getAttribute("line_index") != null || session.getAttribute("line_index") != "") {
            session.removeAttribute("line_index");
        }

        List<Tree> result = new ArrayList();
        try {
            result = getDeviceInfo.getDeviceMainTree();
        } catch (Exception e) {
            logger.error("获取终端树异常，异常是" + e.toString());
        }
        return result;
    }

    // 查询终端类型
    @RequestMapping(value = "devNameType", method = RequestMethod.POST)
    @ResponseBody
    public Device devNameType(String devName) {
        // 后台Xss攻击处理、包括html/js/sql
        devName = XssFilterUtil.dealString(devName);
        Device quarryOutValue = new Device();
        try {
            quarryOutValue = getDeviceInfo.devNameType(devName);
        } catch (Exception e) {
            logger.error("查找终端类型异常，异常是" + e.toString());
        }
        return quarryOutValue;
    }


    /**
     * 终端树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "nodetree", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getNodeTree(HttpServletRequest request, String startTime, String endTime) {
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "tree");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }

        User user = TokenManager.getToken();
        List<Tree> data;
        try {
            data = getDeviceTree.getNodeTreedata(user.getLoginName(), datePojo.getStartTime(), datePojo.getEndTime());
            result = PubUtils.assignmentResult(data, 200, "获取设备树信息成功");
        } catch (Exception e) {
            result = PubUtils.assignmentResult(null, 200, "获取设备树信息失败");
        }
        return result;
    }

    /**
     * 终端树
     * cdf
     */
    @RequestMapping(value = "getTree", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getTree() {
        HttpResult result = null;
        try {
            List<Tree>  data = csvDataService.getTree();
            result = PubUtils.assignmentResult(data, 200, "获取设备树信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = PubUtils.assignmentResult(null, 200, "获取设备树信息失败");
        }
        return result;
    }


}
