package com.dxhy.admin.sys.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dxhy.admin.sys.entity.SysIpEntity;
import com.dxhy.admin.sys.service.SysIpService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@RestController
@RequestMapping("/sys/ip")
@Slf4j
public class SysIpController extends AbstractController {
    @Resource
    private SysIpService sysIpService;

    /**
     * 集团ip查询
     */
    @RequestMapping("/list")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
        PageUtils page = sysIpService.queryPage(params);

        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 保存ip
     */
    @RequestMapping("/save")
    @SysLog("进项平台保存ip")
    public ResponseEntity<?> save(@RequestBody SysIpEntity ip) {
        try {

            sysIpService.saveIp(ip);

            return ResponseEntity.ok(R.ok("保存成功！"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败！"));

        }
    }

    /**
     * 修改ip
     */
    @RequestMapping("/update")
    @SysLog("进项平台修改ip")
    public ResponseEntity<?> update(@RequestBody SysIpEntity ip) {
        try {
            sysIpService.update(ip);

            return ResponseEntity.ok(R.ok("保存成功！"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败！"));

        }
    }

    /**
     * 删除ip
     */
    @RequestMapping("/delete")
    @SysLog("进项平台删除ip")
    public ResponseEntity<?> delete(long id) {
        try {
            sysIpService.deleteBatch(id);

            return ResponseEntity.ok(R.ok("删除成功！"));

        } catch (Exception e) {
            return ResponseEntity.ok(R.ok("删除失败！"));

        }
    }

    /**
     * ip信息
     */
    @RequestMapping("/info/{id}")
    public ResponseEntity<?> info(@PathVariable("id") Long id) {
        SysIpEntity ipEntity = sysIpService.queryById(id);

        return ResponseEntity.ok(R.ok().put("ip", ipEntity));
    }

}
