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.SzxcWorkRecard;
import com.ruoyi.szxc.service.ISzxcWorkRecardService;
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/workrecard")
public class SzxcWorkRecardController extends BaseController
{
    @Autowired
    private ISzxcWorkRecardService szxcWorkRecardService;
    @Resource
    private ISysDeptService deptService;
    /**
     * 查询工作记录列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:workrecard:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcWorkRecard szxcWorkRecard)
    {
        startPage();
        List<SzxcWorkRecard> list = szxcWorkRecardService.selectSzxcWorkRecardList(szxcWorkRecard);
        return getDataTable(list);
    }

    /**
     * 导出工作记录列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:workrecard:export')")
    @Log(title = "工作记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcWorkRecard szxcWorkRecard)
    {
        List<SzxcWorkRecard> list = szxcWorkRecardService.selectSzxcWorkRecardList(szxcWorkRecard);
        ExcelUtil<SzxcWorkRecard> util = new ExcelUtil<SzxcWorkRecard>(SzxcWorkRecard.class);
        util.exportExcel(response, list, "工作记录数据");
    }

    /**
     * 获取工作记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:workrecard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcWorkRecardService.selectSzxcWorkRecardById(id));
    }

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

    /**
     * 修改工作记录
     */
    @PreAuthorize("@ss.hasPermi('szxc:workrecard:edit')")
    @Log(title = "工作记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcWorkRecard szxcWorkRecard)
    {
        return toAjax(szxcWorkRecardService.updateSzxcWorkRecard(szxcWorkRecard));
    }

    /**
     * 删除工作记录
     */
    @PreAuthorize("@ss.hasPermi('szxc:workrecard:remove')")
    @Log(title = "工作记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcWorkRecardService.deleteSzxcWorkRecardByIds(ids));
    }
}
