package com.jeethink.receive.controller;

import com.jeethink.bean.domain.ArchivesMaster;
import com.jeethink.common.annotation.Log;
import com.jeethink.common.constant.Constants;
import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.core.domain.model.LoginUser;
import com.jeethink.common.core.page.TableDataInfo;
import com.jeethink.common.enums.BusinessType;
import com.jeethink.common.utils.ServletUtils;
import com.jeethink.common.utils.poi.ExcelUtil;
import com.jeethink.receive.service.IArchivesService;
import com.jeethink.receive.vo.ArchivesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 健康档案接收Controller
 *
 * @author sh
 * @date 2022-11-08
 */
@Api("健康档案接收")
@RestController
@RequestMapping("/receive/jkReceive")
public class JkArchivesController extends BaseController {
    
    @Autowired
    private IArchivesService service;

    /**
     * 查询健康档案接收列表
     */
    @ApiOperation("健康档案接收页面list列表")
    @PreAuthorize("@ss.hasPermi('receive:jkReceive:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArchivesMaster archives) {

        startPage();
        archives.setType(Constants.ARCHIVES_TYPE_JK);
        archives.setStage(Constants.ARCHIVES_STAGE_RECEIVE);
        List<ArchivesMaster> list = service.selectArchivesList(archives);
        return getDataTable(list);
    }

    /**
     * 获取健康档案接收详细信息
     */
    @PreAuthorize("@ss.hasPermi('receive:jkReceive:query')")
    @GetMapping
    public AjaxResult getInfo(@RequestParam String archivesId) {
        
        return AjaxResult.success(service.selectArchivesById(archivesId));
    }

    @PreAuthorize("@ss.hasPermi('receive:jkReceive:add')")
    @Log(title = "健康档案接收", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArchivesVO archives) {

        archives.setType(Constants.ARCHIVES_TYPE_JK);
        service.insertArchives(archives);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('receive:jkReceive:edit')")
    @Log(title = "健康档案接收", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody ArchivesVO archives) {

        service.updateArchives(archives);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('receive:jkReceive:submit')")
    @Log(title = "健康档案接收", businessType = BusinessType.OTHER)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody ArchivesVO archives) {

        service.submitArchives(archives);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('receive:jkReceive:remove')")
    @Log(title = "健康档案接收", businessType = BusinessType.DELETE)
    @DeleteMapping("/{archivesId}")
    public AjaxResult remove(@PathVariable("archivesId") String archivesId) {

        return toAjax(service.removeArchives(archivesId));
    }

    /**
     * 导出健康档案接收列表
     */
    @PreAuthorize("@ss.hasPermi('receive:jkReceive:export')")
    @Log(title = "健康档案接收", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ArchivesMaster archives) {
        archives.setType(Constants.ARCHIVES_TYPE_JK);
        archives.setStage(Constants.ARCHIVES_STAGE_RECEIVE);
        List<ArchivesMaster> list = service.selectArchivesList(archives);
        ExcelUtil<ArchivesMaster> util = new ExcelUtil<ArchivesMaster>(ArchivesMaster.class);
        return util.exportExcel(list, "rsReceive");
    }

    @Log(title = "档案导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('receive:jkReceive:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ArchivesMaster> util = new ExcelUtil<ArchivesMaster>(ArchivesMaster.class);
        List<ArchivesMaster> archivesMasterList = util.importExcel(file.getInputStream());
        String message = service.importArchives2(archivesMasterList, updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 批量导入附件并与主表关联
     * @return
     */
    @GetMapping("/uploadArchiveList")
    public AjaxResult uploadArchiveList(){
        service.uploadArchiveFileList();
        return AjaxResult.success();
    }
}
