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.SzxcHelp;
import com.ruoyi.szxc.service.ISzxcHelpService;
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/help")
public class SzxcHelpController extends BaseController
{
    @Autowired
    private ISzxcHelpService szxcHelpService;
    @Resource
    private ISysDeptService deptService;

    /**
     * 查询帮办事列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:help:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcHelp szxcHelp)
    {
        startPage();
        List<SzxcHelp> list = szxcHelpService.selectSzxcHelpList(szxcHelp);
        return getDataTable(list);
    }

    /**
     * 导出帮办事列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:help:export')")
    @Log(title = "帮办事", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcHelp szxcHelp)
    {
        List<SzxcHelp> list = szxcHelpService.selectSzxcHelpList(szxcHelp);
        ExcelUtil<SzxcHelp> util = new ExcelUtil<SzxcHelp>(SzxcHelp.class);
        util.exportExcel(response, list, "帮办事数据");
    }

    /**
     * 获取帮办事详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:help:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcHelpService.selectSzxcHelpById(id));
    }

    /**
     * 新增帮办事
     */
    @PreAuthorize("@ss.hasPermi('szxc:help:add')")
    @Log(title = "帮办事", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SzxcHelp szxcHelp)
    {
        //根据前端传递过来的部门存储，如果没传则存储当前用户的部门
        if(szxcHelp.getDeptId()==null){
            szxcHelp.setDeptId(getDeptId());
        }
        szxcHelp.setUserId(getUserId());
        //根据DeptId存储上级为默认审核部门及id
        SysDept sysDept = deptService.selectDeptById(szxcHelp.getDeptId());
        SysDept parentDept = deptService.selectDeptById(sysDept.getParentId());
        szxcHelp.setHandleDeptid(parentDept.getDeptId());
        szxcHelp.setHandleDept(parentDept.getDeptName());
        return toAjax(szxcHelpService.insertSzxcHelp(szxcHelp));
    }

    /**
     * 修改帮办事
     */
    @PreAuthorize("@ss.hasPermi('szxc:help:edit')")
    @Log(title = "帮办事", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcHelp szxcHelp)
    {
        return toAjax(szxcHelpService.updateSzxcHelp(szxcHelp));
    }

    /**
     * 删除帮办事
     */
    @PreAuthorize("@ss.hasPermi('szxc:help:remove')")
    @Log(title = "帮办事", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcHelpService.deleteSzxcHelpByIds(ids));
    }
}
