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.SzxcWishManage;
import com.ruoyi.szxc.domain.SzxcXyAuditRecard;
import com.ruoyi.szxc.service.ISzxcWishManageService;
import com.ruoyi.szxc.service.ISzxcXyAuditRecardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 心愿管理Controller
 * 
 * @author hs
 * @date 2024-04-03
 */
@RestController
@RequestMapping("/szxc/wish")
public class SzxcWishManageController extends BaseController
{
    @Autowired
    private ISzxcWishManageService szxcWishManageService;
    @Resource
    private ISysDeptService deptService;
    @Autowired
    private ISzxcXyAuditRecardService szxcXyAuditRecardService;

    /**
     * 查询心愿管理列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:wish:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcWishManage szxcWishManage)
    {
        startPage();
        List<SzxcWishManage> list = szxcWishManageService.selectSzxcWishManageList(szxcWishManage);
        return getDataTable(list);
    }

    /**
     * 导出心愿管理列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:wish:export')")
    @Log(title = "心愿管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcWishManage szxcWishManage)
    {
        List<SzxcWishManage> list = szxcWishManageService.selectSzxcWishManageList(szxcWishManage);
        ExcelUtil<SzxcWishManage> util = new ExcelUtil<SzxcWishManage>(SzxcWishManage.class);
        util.exportExcel(response, list, "心愿管理数据");
    }

    /**
     * 获取心愿管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:wish:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcWishManageService.selectSzxcWishManageById(id));
    }

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

    /**
     * 修改心愿管理
     */
    @PreAuthorize("@ss.hasPermi('szxc:wish:edit')")
    @Log(title = "心愿管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcWishManage szxcWishManage)
    {
        return toAjax(szxcWishManageService.updateSzxcWishManage(szxcWishManage));
    }

    /**
     * todo 审核心愿管理  (当前未使用，前端先调用修改心愿接口，成功后调用的审核记录新增接口)
     */
    @PreAuthorize("@ss.hasPermi('szxc:wish:audit')")
    @Log(title = "心愿管理", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    @Transactional
    public AjaxResult audit(@RequestBody SzxcWishManage szxcWishManage)
    {
        //同时新增审核记录
        SzxcXyAuditRecard szxcXyAuditRecard = new SzxcXyAuditRecard();
        szxcXyAuditRecard.setAuditDept(szxcWishManage.getAuditDept());
        szxcXyAuditRecard.setAuditName(szxcWishManage.getAuditName());
        szxcXyAuditRecard.setAuditReason(szxcWishManage.getAuditReason());
        szxcXyAuditRecard.setAuditResult(szxcWishManage.getAuditResult());
        szxcXyAuditRecard.setJlId(szxcWishManage.getId());
        szxcXyAuditRecardService.insertSzxcXyAuditRecard(szxcXyAuditRecard);
        return toAjax(szxcWishManageService.updateSzxcWishManage(szxcWishManage));
    }

    /**
     * 删除心愿管理
     */
    @PreAuthorize("@ss.hasPermi('szxc:wish:remove')")
    @Log(title = "心愿管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcWishManageService.deleteSzxcWishManageByIds(ids));
    }
}
