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.SzxcVisitRecard;
import com.ruoyi.szxc.service.ISzxcVisitRecardService;
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/visitrecard")
public class SzxcVisitRecardController extends BaseController
{
    @Autowired
    private ISzxcVisitRecardService szxcVisitRecardService;
    @Resource
    private ISysDeptService deptService;
    /**
     * 查询走访记录列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:visitrecard:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcVisitRecard szxcVisitRecard)
    {
        startPage();
        List<SzxcVisitRecard> list = szxcVisitRecardService.selectSzxcVisitRecardList(szxcVisitRecard);
        return getDataTable(list);
    }

    /**
     * 导出走访记录列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:visitrecard:export')")
    @Log(title = "走访记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcVisitRecard szxcVisitRecard)
    {
        List<SzxcVisitRecard> list = szxcVisitRecardService.selectSzxcVisitRecardList(szxcVisitRecard);
        ExcelUtil<SzxcVisitRecard> util = new ExcelUtil<SzxcVisitRecard>(SzxcVisitRecard.class);
        util.exportExcel(response, list, "走访记录数据");
    }

    /**
     * 获取走访记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:visitrecard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcVisitRecardService.selectSzxcVisitRecardById(id));
    }

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

    /**
     * 修改走访记录
     */
    @PreAuthorize("@ss.hasPermi('szxc:visitrecard:edit')")
    @Log(title = "走访记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcVisitRecard szxcVisitRecard)
    {
        return toAjax(szxcVisitRecardService.updateSzxcVisitRecard(szxcVisitRecard));
    }

    /**
     * 删除走访记录
     */
    @PreAuthorize("@ss.hasPermi('szxc:visitrecard:remove')")
    @Log(title = "走访记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcVisitRecardService.deleteSzxcVisitRecardByIds(ids));
    }
}
