package com.crm.manager.controller;

import com.crm.manager.service.SysIpLimitService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.system.ip.SysIpLimitDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.system.ip.SysIpLimitAddEntity;
import com.crm.rely.backend.core.entity.system.ip.SysIpLimitDeleteEntity;
import com.crm.rely.backend.core.entity.system.ip.SysIpLimitSearchEntity;
import com.crm.rely.backend.core.entity.system.ip.SysIpLimitUpdateEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/ip/limit")
public class SysIpLimitController {

    @Autowired
    private SysIpLimitService service;

    /**
     * 添加
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysIpLimitAddEntity entity) throws Exception {
        if (service.validatedIpExist(entity.getIp())) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        service.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysIpLimitDeleteEntity entity) throws Exception {
        service.delete(entity);
        ;
        return BaseResultDto.success();
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysIpLimitUpdateEntity entity) throws Exception {
        service.update(entity);
        return BaseResultDto.success();
    }

    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto list(@RequestBody @Validated SysIpLimitSearchEntity entity) throws Exception {
        ResultWithPagerDto<SysIpLimitDto> result = service.searchPageList(entity);
        return result;
    }

    @PostMapping("/search/single")
    public BaseResultDto single(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        SysIpLimitDto dto = service.searchSingle(entity);
        return BaseResultDto.success(dto);
    }

    @PostMapping("/list")
    public BaseResultDto list() throws Exception {
        List<SysIpLimitDto> dtos = service.getList();
        return BaseResultDto.success(dtos);
    }
}
