package com.willalp.web.controller.yiqing;

import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.yiqing.domain.HsYiqingRnsResult;
import com.willalp.yiqing.domain.vo.RnsResultVo;
import com.willalp.yiqing.service.IHsYiqingRnsResultService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 核酸检测结果Controller
 *
 * @author willalp
 * @date 2022-05-18
 */
@RestController
@RequestMapping("/yiqing/rnsResult")
public class HsYiqingRnsResultController extends BaseController {
    @Resource
    private IHsYiqingRnsResultService hsYiqingRnsResultService;

    /**
     * 查询核酸检测结果列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rnsResult:list')")
    @GetMapping("/list")
    public TableDataInfo list(RnsResultVo rnsResultVo) {
        startPage();
        List<RnsResultVo> list = hsYiqingRnsResultService.selectHsYiqingRnsResultList(rnsResultVo);
        return getDataTable(list);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<RnsResultVo> util = new ExcelUtil<>(RnsResultVo.class);
        return util.importTemplateExcel("核酸检测记录导入模板");
    }

    @Log(title = "核酸检测记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RnsResultVo> util = new ExcelUtil<>(RnsResultVo.class);
        List<RnsResultVo> list = util.importExcel(file.getInputStream());
        return AjaxResult.success(hsYiqingRnsResultService.importData(list));
    }

    /**
     * 导出核酸检测结果列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rnsResult:export')")
    @Log(title = "核酸检测结果", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RnsResultVo rnsResultVo) {
        List<RnsResultVo> list = hsYiqingRnsResultService.selectHsYiqingRnsResultList(rnsResultVo);
        ExcelUtil<RnsResultVo> util = new ExcelUtil<>(RnsResultVo.class);
        return util.exportExcel(list, "核酸检测记录数据");
    }

    /**
     * 获取核酸检测结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rnsResult:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsYiqingRnsResultService.getById(id));
    }

    /**
     * 新增核酸检测结果
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rnsResult:add')")
    @Log(title = "核酸检测结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsYiqingRnsResult hsYiqingRnsResult) {
        hsYiqingRnsResult.setCreateBy(SecurityUtils.getUsername());
        hsYiqingRnsResult.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingRnsResultService.save(hsYiqingRnsResult));
    }

    /**
     * 修改核酸检测结果
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rnsResult:edit')")
    @Log(title = "核酸检测结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsYiqingRnsResult hsYiqingRnsResult) {
        hsYiqingRnsResult.setUpdateBy(SecurityUtils.getUsername());
        hsYiqingRnsResult.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingRnsResultService.updateById(hsYiqingRnsResult));
    }

    /**
     * 删除核酸检测结果
     */
    @PreAuthorize("@ss.hasPermi('yiqing:rnsResult:remove')")
    @Log(title = "核酸检测结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsYiqingRnsResultService.removeByIds(Arrays.asList(ids)));
    }
}
