package com.ruoyi.web.controller.ehs;

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ehsDocument.domain.EhsDocument;
import com.ruoyi.ehsDocument.service.IEhsDocumentService;
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.ehsDocTransfer.domain.EhsDocTransfer;
import com.ruoyi.ehsDocTransfer.service.IEhsDocTransferService;
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/ehsDocTransfer")
public class EhsDocTransferController extends BaseController {
    @Autowired
    private IEhsDocTransferService ehsDocTransferService;
    @Autowired
    private IEhsDocumentService ehsDocumentService;

    /**
     * 查询公文流转列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EhsDocTransfer ehsDocTransfer) {
        startPage();
        List<EhsDocTransfer> list = ehsDocTransferService.selectEhsDocTransferList(ehsDocTransfer);
        return getDataTable(list);
    }

    /**
     * 导出公文流转列表
     */
    @Log(title = "公文流转", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EhsDocTransfer ehsDocTransfer) {
        List<EhsDocTransfer> list = ehsDocTransferService.selectEhsDocTransferList(ehsDocTransfer);
        ExcelUtil<EhsDocTransfer> util = new ExcelUtil<EhsDocTransfer>(EhsDocTransfer.class);
        util.exportExcel(response, list, "公文流转数据");
    }

    /**
     * 获取公文流转详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ehsDocTransferService.selectEhsDocTransferById(id));
    }

    /**
     * 新增公文流转
     */
    @ApiOperation("新增公文流转")
    @Log(title = "公文流转", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsDocTransfer ehsDocTransfer) {
        return toAjax(ehsDocTransferService.save(ehsDocTransfer));
    }

    /**
     * 借出
     */
    @ApiOperation("公文借出")
    @Log(title = "公文流转", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping("/lend")
    public AjaxResult lend(@RequestBody EhsDocTransfer ehsDocTransfer) {
        EhsDocument ehsDocument = ehsDocumentService.selectEhsDocumentById(ehsDocTransfer.getDocumentId());
        if (ehsDocument.getStatus() == 0) {
            return AjaxResult.error("该公文已被借出");
        }
        ehsDocTransfer.setDocTransferStatus(0);
        boolean result = ehsDocTransferService.save(ehsDocTransfer);
        // 借出成功后,此公文状态变为0-借出
        ehsDocument.setStatus(0);
        ehsDocumentService.updateById(ehsDocument);
        return toAjax(result);
    }

    /**
     * 归还
     */
    @ApiOperation("公文归还")
    @Log(title = "公文流转", businessType = BusinessType.INSERT)
    @Transactional
    @GetMapping("/return/{id}")
    public AjaxResult returnEhsDoc(@PathVariable("id") Long documentId) {
        EhsDocument ehsDocument = ehsDocumentService.selectEhsDocumentById(documentId);
        if (ehsDocument.getStatus() == 1){
            return AjaxResult.error("该公文已归还");
        }
        ehsDocument.setStatus(1);
        ehsDocumentService.updateById(ehsDocument);

        EhsDocTransfer ehsDocTransfer = new EhsDocTransfer();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ehsDocTransfer.setTransferUserId(user.getUserId());
        ehsDocTransfer.setTransferName(user.getNickName());
        ehsDocTransfer.setTransferTime(new Date());
        ehsDocTransfer.setDocTransferStatus(ehsDocument.getStatus());
        ehsDocTransfer.setDocumentId(documentId);
        ehsDocTransfer.setDocumentName(ehsDocument.getDocName());
        return toAjax(ehsDocTransferService.save(ehsDocTransfer));
    }

    /**
     * 修改公文流转
     */
    @ApiOperation("修改公文流转")
    @Log(title = "公文流转", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsDocTransfer ehsDocTransfer) {
        return toAjax(ehsDocTransferService.updateById(ehsDocTransfer));
    }

    /**
     * 删除公文流转
     */
    @ApiOperation("删除公文流转")
    @Log(title = "公文流转", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ehsDocTransferService.removeByIds(Arrays.asList(ids)));
    }
}
