package com.sunyard.chsm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.constant.AuditLogConst;
import com.sunyard.chsm.dto.IpWhitelistDTO;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.service.IpWhitelistService;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 访问控制接口
 *
 * @author liulu
 * @since 2024/11/22
 */
@RestController
@RequestMapping("/sysconf/ipWhitelist")
public class IpWhitelistController {

    @Resource
    private IpWhitelistService iIpWhitelistService;

    /**
     * 分页查询设备组列表
     *
     * @param query 查询条件
     * @return 分页列表
     */
    @PostMapping("/queryList")
    public R<Page<IpWhitelistDTO.IpView>> servicePageList(@RequestBody IpWhitelistDTO.Query query) {

        Page<IpWhitelistDTO.IpView> page = iIpWhitelistService.selectPageList(query);

        return R.data(page);
    }

    /**
     * 新增白名单
     *
     * @param save save
     * @return
     */
    @PostMapping("/save")
    @AuditControllerLog(description = "新增白名单", operateType = AuditLogConst.ADD)
    public R<Long> save(@Valid IpWhitelistDTO.IpSave save) {
        Long id = iIpWhitelistService.save(save);
        return R.data(id);
    }

    /**
     * 修改白名单
     *
     * @param update update
     */
    @PutMapping("/update")
    @AuditControllerLog(description = "修改白名单", operateType = AuditLogConst.UPDATE)
    public R<Void> update(@Valid IpWhitelistDTO.IpSave update) {
        iIpWhitelistService.update(update);
        return R.ok();
    }

    /**
     * 删除白名单
     *
     * @param ids ids
     * @return void
     */
    @DeleteMapping("/deleteByIds")
    @AuditControllerLog(description = "删除白名单", operateType = AuditLogConst.DELETE)
    public R<Void> delete(@RequestParam Long[] ids) {
        iIpWhitelistService.delete(ids);
        return R.ok("批量通过id删除数据成功");
    }

}
