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.SzxcXcRecard;
import com.ruoyi.szxc.service.ISzxcXcRecardService;
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/xcrecard")
public class SzxcXcRecardController extends BaseController
{
    @Autowired
    private ISzxcXcRecardService szxcXcRecardService;
    @Resource
    private ISysDeptService deptService;

    /**
     * 查询巡查记录列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:xcrecard:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcXcRecard szxcXcRecard)
    {
        startPage();
        List<SzxcXcRecard> list = szxcXcRecardService.selectSzxcXcRecardList(szxcXcRecard);
        return getDataTable(list);
    }

    /**
     * 导出巡查记录列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:xcrecard:export')")
    @Log(title = "巡查记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcXcRecard szxcXcRecard)
    {
        List<SzxcXcRecard> list = szxcXcRecardService.selectSzxcXcRecardList(szxcXcRecard);
        ExcelUtil<SzxcXcRecard> util = new ExcelUtil<SzxcXcRecard>(SzxcXcRecard.class);
        util.exportExcel(response, list, "巡查记录数据");
    }

    /**
     * 获取巡查记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:xcrecard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcXcRecardService.selectSzxcXcRecardById(id));
    }

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

    /**
     * 修改巡查记录
     */
    @PreAuthorize("@ss.hasPermi('szxc:xcrecard:edit')")
    @Log(title = "巡查记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcXcRecard szxcXcRecard)
    {
        return toAjax(szxcXcRecardService.updateSzxcXcRecard(szxcXcRecard));
    }

    /**
     * 删除巡查记录
     */
    @PreAuthorize("@ss.hasPermi('szxc:xcrecard:remove')")
    @Log(title = "巡查记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcXcRecardService.deleteSzxcXcRecardByIds(ids));
    }
}
