package com.ruoyi.szxc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.szxc.domain.SzxcPublicActivily;
import com.ruoyi.szxc.service.ISzxcPublicActivilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 公益活动Controller
 * 
 * @author hs
 * @date 2024-03-20
 */
@RestController
@RequestMapping("/szxc/activily")
public class SzxcPublicActivilyController extends BaseController
{
    @Autowired
    private ISzxcPublicActivilyService szxcPublicActivilyService;

    /**
     * 查询公益活动列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:activily:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcPublicActivily szxcPublicActivily)
    {
        startPage();
        List<SzxcPublicActivily> list = szxcPublicActivilyService.selectSzxcPublicActivilyList(szxcPublicActivily);
        return getDataTable(list);
    }

    /**
     * 导出公益活动列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:activily:export')")
    @Log(title = "公益活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcPublicActivily szxcPublicActivily)
    {
        List<SzxcPublicActivily> list = szxcPublicActivilyService.selectSzxcPublicActivilyList(szxcPublicActivily);
        ExcelUtil<SzxcPublicActivily> util = new ExcelUtil<SzxcPublicActivily>(SzxcPublicActivily.class);
        util.exportExcel(response, list, "公益活动数据");
    }

    /**
     * 获取公益活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:activily:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcPublicActivilyService.selectSzxcPublicActivilyById(id));
    }

    /**
     * 新增公益活动
     */
    @PreAuthorize("@ss.hasPermi('szxc:activily:add')")
    @Log(title = "公益活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SzxcPublicActivily szxcPublicActivily)
    {
        //根据前端传递过来的部门存储，如果没传则存储当前用户的部门
        if(szxcPublicActivily.getDeptId()==null){
            szxcPublicActivily.setDeptId(getDeptId());
        }
        szxcPublicActivily.setUserId(getUserId());
        return toAjax(szxcPublicActivilyService.insertSzxcPublicActivily(szxcPublicActivily));
    }

    /**
     * 修改公益活动
     */
    @PreAuthorize("@ss.hasPermi('szxc:activily:edit')")
    @Log(title = "公益活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcPublicActivily szxcPublicActivily)
    {
        return toAjax(szxcPublicActivilyService.updateSzxcPublicActivily(szxcPublicActivily));
    }

    /**
     * 删除公益活动
     */
    @PreAuthorize("@ss.hasPermi('szxc:activily:remove')")
    @Log(title = "公益活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcPublicActivilyService.deleteSzxcPublicActivilyByIds(ids));
    }
}
