package com.sf.system.deployment.controller;

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

import com.sf.common.utils.DateUtils;
import com.sf.system.deployment.domain.DeploymentServicePublish;
import com.sf.system.deployment.service.IDeploymentServicePublishService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.unit.DataUnit;
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-04-19
 */
@RestController
@RequestMapping("/deployment/publish")
public class DeploymentServicePublishController extends BaseController
{
    @Autowired
    private IDeploymentServicePublishService deploymentServicePublishService;

    /**
     * 查询发布列表列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:publish:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeploymentServicePublish deploymentServicePublish)
    {
        startPage();
        List<DeploymentServicePublish> list = deploymentServicePublishService.selectDeploymentServicePublishList(deploymentServicePublish);
        return getDataTable(list);
    }

    /**
     * 导出发布列表列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:publish:export')")
    @Log(title = "发布列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeploymentServicePublish deploymentServicePublish)
    {
        List<DeploymentServicePublish> list = deploymentServicePublishService.selectDeploymentServicePublishList(deploymentServicePublish);
        ExcelUtil<DeploymentServicePublish> util = new ExcelUtil<DeploymentServicePublish>(DeploymentServicePublish.class);
        util.exportExcel(response, list, "发布列表数据");
    }

    /**
     * 获取发布列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('deployment:publish:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deploymentServicePublishService.selectDeploymentServicePublishById(id));
    }

    /**
     * 新增发布列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:publish:add')")
    @Log(title = "发布列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeploymentServicePublish deploymentServicePublish)
    {
        return toAjax(deploymentServicePublishService.insertDeploymentServicePublish(deploymentServicePublish));
    }

    /**
     * 修改发布列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:publish:edit')")
    @Log(title = "发布列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeploymentServicePublish deploymentServicePublish)
    {
        return toAjax(deploymentServicePublishService.updateDeploymentServicePublish(deploymentServicePublish));
    }

    /**
     * 删除发布列表
     */
    @PreAuthorize("@ss.hasPermi('deployment:publish:remove')")
    @Log(title = "发布列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deploymentServicePublishService.deleteDeploymentServicePublishByIds(ids));
    }

    /**
     * 发布功能
     */
    @PreAuthorize("@ss.hasPermi('deployment:publish:publish')")
    @GetMapping(value = "/publish/{id}")
    public AjaxResult publish(@PathVariable("id") Long id) throws Exception {
        DeploymentServicePublish deploymentServicePublish = deploymentServicePublishService.selectDeploymentServicePublishById(id);
//        deploymentServicePublish.setUpdateBy(getUsername());
        deploymentServicePublish.setUpdateTime(DateUtils.getNowDate());
        return success(deploymentServicePublishService.publish(deploymentServicePublish));
    }

    /**
     * 获取发布列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('deployment:publish:sold:out')")
    @GetMapping(value = "/sold/out/{id}")
    public AjaxResult soldOut(@PathVariable("id") Long id)
    {
        DeploymentServicePublish deploymentServicePublish = deploymentServicePublishService.selectDeploymentServicePublishById(id);
//        deploymentServicePublish.setUpdateBy(getUsername());
        deploymentServicePublish.setUpdateTime(DateUtils.getNowDate());
        return success(deploymentServicePublishService.soldOut(deploymentServicePublish));
    }

}
