package com.sf.system.deployment.controller;

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

import com.sf.common.enums.RequestHeaderEnums;
import com.sf.common.utils.http.RequestUtils;
import com.sf.system.deployment.domain.DeploymentWhitelistInfo;
import com.sf.system.deployment.service.IDeploymentWhitelistInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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.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;

/**
 * 白名单列表Controller
 * 
 * @author ztzh
 * @date 2024-05-06
 */
@RestController
@RequestMapping("/deployment/whitelist")
public class DeploymentWhitelistInfoController extends BaseController
{
    @Autowired
    private IDeploymentWhitelistInfoService deploymentWhitelistInfoService;

    /**
     * 查询白名单列表列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelist:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeploymentWhitelistInfo deploymentWhitelistInfo)
    {
        deploymentWhitelistInfo.setAppCode(RequestUtils.getHeader(RequestHeaderEnums.APP_CODE.getCode()));
        startPage();
        List<DeploymentWhitelistInfo> list = deploymentWhitelistInfoService.selectDeploymentWhitelistInfoList(deploymentWhitelistInfo);
        return getDataTable(list);
    }

    /**
     * 导出白名单列表列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelist:export')")
    @Log(title = "白名单列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeploymentWhitelistInfo deploymentWhitelistInfo)
    {
        deploymentWhitelistInfo.setAppCode(RequestUtils.getHeader(RequestHeaderEnums.APP_CODE.getCode()));
        List<DeploymentWhitelistInfo> list = deploymentWhitelistInfoService.selectDeploymentWhitelistInfoList(deploymentWhitelistInfo);
        ExcelUtil<DeploymentWhitelistInfo> util = new ExcelUtil<DeploymentWhitelistInfo>(DeploymentWhitelistInfo.class);
        util.exportExcel(response, list, "白名单列表数据");
    }

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

    /**
     * 新增白名单列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelist:add')")
    @Log(title = "白名单列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeploymentWhitelistInfo deploymentWhitelistInfo)
    {
        deploymentWhitelistInfo.setAppCode(RequestUtils.getHeader(RequestHeaderEnums.APP_CODE.getCode()));
        return toAjax(deploymentWhitelistInfoService.insertDeploymentWhitelistInfo(deploymentWhitelistInfo));
    }

    /**
     * 修改白名单列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelist:edit')")
    @Log(title = "白名单列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeploymentWhitelistInfo deploymentWhitelistInfo)
    {
        return toAjax(deploymentWhitelistInfoService.updateDeploymentWhitelistInfo(deploymentWhitelistInfo));
    }

    /**
     * 删除白名单列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelist:remove')")
    @Log(title = "白名单列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deploymentWhitelistInfoService.deleteDeploymentWhitelistInfoByIds(ids));
    }
    /**
     * 发布功能
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelist:publish')")
    @GetMapping(value = "/publish/{id}")
    public AjaxResult publish(@PathVariable("id") Long id) throws Exception {
        return success(deploymentWhitelistInfoService.publish(id));
    }
    /**
     * 下架功能
     */
    @PreAuthorize("@ss.hasPermi('deployment:whitelist:sold:out')")
    @GetMapping(value = "/sold/out/{id}")
    public AjaxResult soldOut(@PathVariable("id") Long id)
    {
        return success(deploymentWhitelistInfoService.soldOut(id));
    }

}
