package com.ruoyi.web.controller.ehs;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ehsDocTransfer.domain.EhsDocTransfer;
import com.ruoyi.ehsDocTransfer.service.IEhsDocTransferService;
import com.ruoyi.ehsDocument.vo.DocResponseVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ehsDocument.domain.EhsDocument;
import com.ruoyi.ehsDocument.service.IEhsDocumentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公文管理Controller
 *
 * @author ruoyi
 * @date 2023-12-07
 */
@Api(tags = "公文管理管理")
@RestController
@RequestMapping("/ehs/ehsDocument")
public class EhsDocumentController extends BaseController {
    @Autowired
    private IEhsDocumentService ehsDocumentService;

    @Autowired
    private IEhsDocTransferService transferService;

    /**
     * 查询公文管理列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsDocument:list')")
    @GetMapping("/list")
    public TableDataInfo list(EhsDocument ehsDocument) {
        startPage();
        List<EhsDocument> list = ehsDocumentService.selectEhsDocumentList(ehsDocument);
        return getDataTable(list);
    }

    /**
     * 获取拟稿部门下拉列表
     */
    @GetMapping(value = "/getAllDraftDept")
    public AjaxResult getAllDraftDept() {
        return AjaxResult.success(ehsDocumentService.getAllDraftDept());
    }

    /**
     * 导出公文管理列表
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsDocument:export')")
    @Log(title = "公文管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EhsDocument ehsDocument) {
        List<EhsDocument> list = ehsDocumentService.selectEhsDocumentList(ehsDocument);
        ExcelUtil<EhsDocument> util = new ExcelUtil<EhsDocument>(EhsDocument.class);
        util.exportExcel(response, list, "公文管理数据");
    }

    /**
     * 获取公文管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ehs:ehsDocument:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ehsDocumentService.selectEhsDocumentById(id));
    }

    /**
     * 获取借出表单数据
     */
    @GetMapping(value = "/formData/{id}")
    public AjaxResult getFormData(@PathVariable("id") Long id) {
        EhsDocument ehsDocument = ehsDocumentService.selectEhsDocumentById(id);
        DocResponseVo docResponseVo = new DocResponseVo();
        docResponseVo.setDocumentName(ehsDocument.getDocName());
        docResponseVo.setTransferTime(new Date());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        docResponseVo.setTransferName(user.getNickName());
        docResponseVo.setTransferUserId(user.getUserId());
        docResponseVo.setDocumentId(ehsDocument.getId());
        return success(docResponseVo);
    }

    /**
     * 根据公文id获取公文流转信息
     */
    @GetMapping(value = "transfer/{id}")
    public TableDataInfo getTransferInfoByDocId(@PathVariable("id") Long id) {
        startPage();
        List<EhsDocTransfer> list = transferService.getTransferInfoByDocId(id);
        return getDataTable(list);
    }

    /**
     * 新增公文管理
     */
    @ApiOperation("新增公文管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsDocument:add')")
    @Log(title = "公文管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsDocument ehsDocument) {
        return toAjax(ehsDocumentService.save(ehsDocument));
    }

    /**
     * 修改公文管理
     */
    @ApiOperation("修改公文管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsDocument:edit')")
    @Log(title = "公文管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsDocument ehsDocument) {
        return toAjax(ehsDocumentService.updateById(ehsDocument));
    }

    /**
     * 删除公文管理,同时删除该公文流转记录
     */
    @ApiOperation("删除公文管理")
    @PreAuthorize("@ss.hasPermi('ehs:ehsDocument:remove')")
    @Log(title = "公文管理", businessType = BusinessType.DELETE)
    @Transactional
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

        int result = transferService.removeByDocumentIds(Arrays.asList(ids));
        return toAjax(ehsDocumentService.removeByIds(Arrays.asList(ids)));
    }
}
