package com.jsc.oscm.controller;

import com.alibaba.fastjson.JSON;
import com.jsc.oscm.entity.TargetSensitive;
import com.jsc.oscm.model.ReturnT;
import com.jsc.oscm.service.TargetSensitiveService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TargetSensitiveController {
    @Resource
    TargetSensitiveService targetSensitiveService;

    @PostMapping("/target/sensitive/add")
    public String add(@RequestBody TargetSensitive targetSensitive) {
        ReturnT<TargetSensitive> returnT;
        try {
            targetSensitive.setDelFlag("0");
            returnT = new ReturnT<>(200, "添加成功", targetSensitiveService.save(targetSensitive));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/target/sensitive/update")
    public String update(@RequestBody TargetSensitive targetSensitive) {
        ReturnT<TargetSensitive> returnT;
        try {
            returnT = new ReturnT<>(200, "修改成功", targetSensitiveService.save(targetSensitive));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/target/sensitive/deleteByIds")
    public String deleteByIds(@RequestBody List<String> ids) {
        ReturnT<String> returnT;
        try {
            if (targetSensitiveService.delete(ids)) {
                returnT = new ReturnT<>(200, "批量删除成功", null);
            } else {
                returnT = new ReturnT<>(500, "批量删除失败", null);
            }
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/target/sensitive/delete")
    public String delete(@RequestBody String id) {
        ReturnT<String> returnT;
        try {
            if (targetSensitiveService.delete(Integer.parseInt(id))) {
                returnT = new ReturnT<>(200, "删除成功", null);
            } else {
                returnT = new ReturnT<>(500, "删除失败", null);
            }
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/target/sensitive/queryPageable/{pageIndex}/{pageSize}")
    public String queryPageable(@RequestBody TargetSensitive targetSensitive,
                                @PathVariable("pageIndex") Integer pageIndex,
                                @PathVariable("pageSize") Integer pageSize) {
        ReturnT<List<TargetSensitive>> returnT;
        try {
            returnT = new ReturnT<>(200,
                    targetSensitiveService.queryCount(targetSensitive).toString(),
                    targetSensitiveService.queryPageable(targetSensitive, pageIndex, pageSize));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/open/sensitiveTarget/query")
    public String query(@RequestBody TargetSensitive targetSensitive) {
        ReturnT<List<TargetSensitive>> returnT;
        try {
            returnT = new ReturnT<>(200,
                    targetSensitiveService.queryCount(targetSensitive).toString(),
                    targetSensitiveService.query(targetSensitive));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/target/sensitive/getCountryList")
    public String getCountryList() {
        ReturnT<List<Object>> returnT;
        try {
            returnT = new ReturnT<>(200, "", targetSensitiveService.getCountryList());
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

}
