package com.sf.system.deployment.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.sf.common.exception.ServiceException;
import com.sf.system.deployment.domain.DeploymentWhitelistList;
import com.sf.system.deployment.service.IDeploymentWhitelistListService;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sf.common.annotation.Log;
import com.sf.common.core.controller.BaseController;
import com.sf.common.core.domain.AjaxResult;
import com.sf.common.enums.BusinessType;
import com.sf.common.utils.poi.ExcelUtil;
import com.sf.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 白名单成员列Controller
 * 
 * @author ztzh
 * @date 2024-05-06
 */
@RestController
@RequestMapping("/deployment/whitelistMember")
public class DeploymentWhitelistListController extends BaseController
{
    @Autowired
    private IDeploymentWhitelistListService deploymentWhitelistListService;

    /**
     * 查询白名单成员列列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelistMember:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeploymentWhitelistList deploymentWhitelistList)
    {
        if(null == deploymentWhitelistList.getWhitelistId()){
            throw new SecurityException("参数异常WhitelistId为空");
        }
        startPage();
        List<DeploymentWhitelistList> list = deploymentWhitelistListService.selectDeploymentWhitelistListList(deploymentWhitelistList);
        return getDataTable(list);
    }

    /**
     * 导出白名单成员列列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelistMember:export')")
    @Log(title = "白名单成员列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeploymentWhitelistList deploymentWhitelistList)
    {
        List<DeploymentWhitelistList> list = deploymentWhitelistListService.selectDeploymentWhitelistListList(deploymentWhitelistList);
        ExcelUtil<DeploymentWhitelistList> util = new ExcelUtil<DeploymentWhitelistList>(DeploymentWhitelistList.class);
        util.exportExcel(response, list, "白名单成员列数据");
    }

    /**
     * 获取白名单成员列详细信息
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelistMember:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deploymentWhitelistListService.selectDeploymentWhitelistListById(id));
    }

    /**
     * 新增白名单成员列
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelistMember:add')")
    @Log(title = "白名单成员列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeploymentWhitelistList deploymentWhitelistList)
    {
        return toAjax(deploymentWhitelistListService.insertDeploymentWhitelistList(deploymentWhitelistList,getUsername()));
    }

    /**
     * 修改白名单成员列
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelistMember:edit')")
    @Log(title = "白名单成员列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeploymentWhitelistList deploymentWhitelistList)
    {
        return toAjax(deploymentWhitelistListService.updateDeploymentWhitelistList(deploymentWhitelistList));
    }

    /**
     * 删除白名单成员列
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelistMember:remove')")
    @Log(title = "白名单成员列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deploymentWhitelistListService.deleteDeploymentWhitelistListByIds(ids));
    }
    /**
     * 批量导入
     *
     * @param file 文件
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelistMember:batch:import')")
    @Log(title = "批量上传", businessType = BusinessType.INSERT)
    @PostMapping(value = "/batch/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult batchImport(@RequestPart("file") MultipartFile file,Long whitelistId) throws IOException {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }

        return toAjax(deploymentWhitelistListService.batchImport(file,whitelistId,getUsername()));
    }
}
