/**
 * 集团ip关联
 */

package com.dxhy.manage.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.common.controller.AbstractController;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.manage.entity.SysIpEntity;
import com.dxhy.manage.service.SysIpService;

/**
 * @author jiaohongyang
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/manage/ip")
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")
    public ResponseEntity<?> save(@RequestBody SysIpEntity ip) {
        try {

            sysIpService.saveIp(ip);

            return ResponseEntity.ok(R.ok("集团编码：" + ip.getCompany() + "  ,IP: " + ip.getIp() + " ,保存成功！"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("集团编码：" + ip.getCompany() + " , IP: " + ip.getIp() + " ,保存失败！"));

        }
    }

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

            return ResponseEntity.ok(R.ok("集团编码：" + ip.getCompany() + "  ,IP: " + ip.getIp() + ",保存成功！"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("集团编码：" + ip.getCompany() + "  ,IP: " + ip.getIp() + ",保存失败！"));

        }
    }

    /**
     * 删除ip
     */
    @RequestMapping("/delete")
    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));
    }

    @RequestMapping("/uploadIp")
    public ResponseEntity<?> uploadIp(@RequestBody String datalist) {
        int uploadDept = sysIpService.uploadIp(datalist);
        if (uploadDept == 0) {
            return ResponseEntity.ok(R.ok("导入成功！"));
        } else if (uploadDept == 1) {
            return ResponseEntity.ok(R.error("导入失败，录入的集团编码不存在！"));
        } else if (uploadDept == 2) {
            return ResponseEntity.ok(R.error("导入失败，请稍后重试"));
        } else {
            return ResponseEntity.ok(R.error("导入失败！"));
        }
    }
}
