package com.huimv.video.controller;

import com.huimv.video.domain.PublicNetwork;
import com.huimv.video.result.Result;
import com.huimv.video.service.PublicNetworkService;
import com.huimv.video.util.IpUtil;
import com.huimv.video.util.PropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.video.controller
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2021/1/6 0006 14:13
 **/
@CrossOrigin
@RestController
@RequestMapping("/publicNetwork")
public class PublicNetworkController {
    @Autowired
    private PublicNetworkService networkService;
    //本地测试 windows 系统
   // final String fileUrl ="G:\\idea\\huimv\\huimv.shiwan\\shiwan\\video\\src\\main\\resources\\file.properties";

    //linux系统
    final String fileUrl ="/root/huimv.shimen/file/file.properties";

    @RequestMapping("/add")
    public Result addNetwork(HttpServletRequest request){

        String ipAddress = IpUtil.getIpAddr(request);
        File file = new File(fileUrl);
        if (!file.getParentFile().exists()){
            file.getParentFile().mkdir();
        }
        if (!file.exists()) {
            try {
                file.createNewFile();
            }catch (IOException e){}
        }

        if (ipAddress.equals(PropertiesUtils.getFileIO("url",fileUrl))){
            //如果ip没变，只修改配置文件
            Map map =new HashMap();
            map.put("creationTime",new Date());
            PropertiesUtils.writeData(map,fileUrl);
            return null;
        }else {
            Map map1 =new HashMap();
            Map map2 =new HashMap();
            Map map3 =new HashMap();
            map1.put("creationTime",new Date());
            map2.put("url",ipAddress);
            map3.put("id",100);
            PropertiesUtils.writeData(map1,fileUrl);
            PropertiesUtils.writeData(map2,fileUrl);
            PropertiesUtils.writeData(map3,fileUrl);
            PublicNetwork publicNetwork = new PublicNetwork();
            publicNetwork.setUrl(ipAddress);
            publicNetwork.setId(100);
            publicNetwork.setCreationTime(new Date());
            try {
                networkService.save(publicNetwork);
                return new Result(10000,"添加成功");
            }catch (Exception e){
                return new Result(10001,"添加失败");
            }

        }

    }

    @RequestMapping("/get")
    public Result getNetwork(){
        try {
            PublicNetwork publicNetwork = networkService.getNetwork();
            return new Result(10000,"查询成功",publicNetwork);
        }catch (Exception e){
            return new Result(10001,"查询失败");
        }
    }


}
