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.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.szxc.domain.SzxcButie;
import com.ruoyi.szxc.service.ISzxcButieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 补贴管理Controller
 * 
 * @author hs
 * @date 2024-03-20
 */
@RestController
@RequestMapping("/szxc/butie")
public class SzxcButieController extends BaseController
{
    @Autowired
    private ISzxcButieService szxcButieService;
    @Resource
    private ISysDeptService deptService;

    /**
     * 查询补贴管理列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:butie:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcButie szxcButie)
    {
        startPage();
        List<SzxcButie> list = szxcButieService.selectSzxcButieList(szxcButie);
        return getDataTable(list);
    }

    /**
     * 导出补贴管理列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:butie:export')")
    @Log(title = "补贴管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcButie szxcButie)
    {
        List<SzxcButie> list = szxcButieService.selectSzxcButieList(szxcButie);
        ExcelUtil<SzxcButie> util = new ExcelUtil<SzxcButie>(SzxcButie.class);
        util.exportExcel(response, list, "补贴管理数据");
    }

    /**
     * 获取补贴管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:butie:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcButieService.selectSzxcButieById(id));
    }

    /**
     * 新增补贴管理
     */
    @PreAuthorize("@ss.hasPermi('szxc:butie:add')")
    @Log(title = "补贴管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SzxcButie szxcButie)
    {
        //根据前端传递过来的部门存储，如果没传则存储当前用户的部门
        if(szxcButie.getDeptId()==null){
            szxcButie.setDeptId(getDeptId());
        }
        szxcButie.setUserId(getUserId());
        //根据DeptId存储上级为默认审核部门及id
        SysDept sysDept = deptService.selectDeptById(szxcButie.getDeptId());
        SysDept parentDept = deptService.selectDeptById(sysDept.getParentId());
        szxcButie.setAuditDeptid(parentDept.getDeptId());
        szxcButie.setAuditDept(parentDept.getDeptName());
        return toAjax(szxcButieService.insertSzxcButie(szxcButie));
    }

    /**
     * 修改补贴管理
     */
    @PreAuthorize("@ss.hasPermi('szxc:butie:edit')")
    @Log(title = "补贴管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcButie szxcButie)
    {
        return toAjax(szxcButieService.updateSzxcButie(szxcButie));
    }

    /**
     * 删除补贴管理
     */
    @PreAuthorize("@ss.hasPermi('szxc:butie:remove')")
    @Log(title = "补贴管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcButieService.deleteSzxcButieByIds(ids));
    }
}
