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.HsPositiveContiguityUser;
import com.willalp.yiqing.domain.vo.OrdersVo;
import com.willalp.yiqing.domain.vo.PcuVo;
import com.willalp.yiqing.domain.vo.RegisterVo;
import com.willalp.yiqing.service.IHsOrdersLogDataService;
import com.willalp.yiqing.service.IHsPositiveContiguityUserService;
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-15
 */
@RestController
@RequestMapping("/yiqing/yquser")
public class HsPositiveContiguityUserController extends BaseController {
    @Resource
    private IHsPositiveContiguityUserService hsPositiveContiguityUserService;

    @Resource
    private IHsOrdersLogDataService ordersLogDataService;


    /**
     * 派单导出
     *
     * @param ordersVo
     */
    @Log(title = "疫情登记信息", businessType = BusinessType.EXPORT)
    @GetMapping("/ordersImport")
    public AjaxResult seventhDayNucleicAcid(OrdersVo ordersVo) {
        List<RegisterVo> list = hsPositiveContiguityUserService.ordersImport(ordersVo);
        ordersLogDataService.insertDatas(list, ordersVo);
        ExcelUtil<RegisterVo> util = new ExcelUtil<>(RegisterVo.class);
        if (0 == list.size()) {
            return AjaxResult.error("暂无可导出数据");
        }
        return util.exportExcel(list, "派单导出表");
    }

    /**
     * 查询阳性及密接人员列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yquser:list')")
    @GetMapping("/list")
    public TableDataInfo list(PcuVo pcuVo) {
        startPage();
        List<HsPositiveContiguityUser> list = hsPositiveContiguityUserService.selectHsPositiveContiguityUserList(pcuVo);
        return getDataTable(list);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HsPositiveContiguityUser> util = new ExcelUtil<>(HsPositiveContiguityUser.class);
        return util.importTemplateExcel("人员异常登记表导入模板");
    }

    /**
     * 导出阳性及密接人员列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yquser:export')")
    @Log(title = "阳性及密接人员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PcuVo pcuVo) {
        List<HsPositiveContiguityUser> list = hsPositiveContiguityUserService.selectHsPositiveContiguityUserList(pcuVo);
        ExcelUtil<HsPositiveContiguityUser> util = new ExcelUtil<HsPositiveContiguityUser>(HsPositiveContiguityUser.class);
        return util.exportExcel(list, "人员异常登记表");
    }

    @Log(title = "人员异常登记表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<HsPositiveContiguityUser> util = new ExcelUtil<>(HsPositiveContiguityUser.class);
        List<HsPositiveContiguityUser> list = util.importExcel(file.getInputStream());
        return AjaxResult.success(hsPositiveContiguityUserService.importData(list));
    }

    /**
     * 获取阳性及密接人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yquser:query')")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") String dataId) {
        return AjaxResult.success(hsPositiveContiguityUserService.getById(dataId));
    }

    /**
     * 新增阳性及密接人员
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yquser:add')")
    @Log(title = "阳性及密接人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsPositiveContiguityUser hsPositiveContiguityUser) {
        hsPositiveContiguityUser.setCreateBy(SecurityUtils.getUsername());
        hsPositiveContiguityUser.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsPositiveContiguityUserService.save(hsPositiveContiguityUser));
    }

    /**
     * 修改阳性及密接人员
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yquser:edit')")
    @Log(title = "阳性及密接人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsPositiveContiguityUser hsPositiveContiguityUser) {
        hsPositiveContiguityUser.setUpdateBy(SecurityUtils.getUsername());
        hsPositiveContiguityUser.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsPositiveContiguityUserService.updateById(hsPositiveContiguityUser));
    }

    /**
     * 删除阳性及密接人员
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yquser:remove')")
    @Log(title = "阳性及密接人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable String[] dataIds) {
        return toAjax(hsPositiveContiguityUserService.removeByIds(Arrays.asList(dataIds)));
    }
}
