package com.boman.web.core.controller;

import java.io.IOException;
import java.util.List;

import com.boman.common.core.utils.SecurityUtils;
import com.boman.common.core.utils.poi.ExcelUtil;
import com.boman.common.core.web.controller.BaseController;
import com.boman.common.core.web.page.TableDataInfo;
import com.boman.common.log.annotation.Log;
import com.boman.common.log.enums.BusinessType;
import com.boman.common.security.annotation.PreAuthorize;
import com.boman.domain.dto.AjaxResult;
import com.boman.web.core.domain.VaccineInfoOperation;
import com.boman.web.core.domain.VaccineInfoUser;
import com.boman.web.core.service.vaccineInfo.IVaccineInfoService;
import com.boman.web.core.service.vaccineInfo.IVaccineInfoUserService;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletResponse;

/**
 * 疫苗信息Controller
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@RestController
@RequestMapping("/core/info")
public class VaccineInfoController extends BaseController {
    @Autowired
    private IVaccineInfoService vaccineInfoService;

    @Autowired
    private IVaccineInfoUserService vaccineInfoUserService;

    /**
     * 查询疫苗信息列表
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(VaccineInfoOperation vaccineInfo) {
        startPage();
        List<VaccineInfoOperation> list = vaccineInfoService.selectVaccineInfoList(vaccineInfo);
        return getDataTable(list);
    }

    @Log(title = "导出疫苗信息", businessType = BusinessType.EXPORT)
//    @PreAuthorize(hasPermi = "core:info:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, VaccineInfoOperation vaccineInfo) throws IOException  {
        List<VaccineInfoOperation> list = vaccineInfoService.selectVaccineInfoList(vaccineInfo);
        ExcelUtil<VaccineInfoOperation> util = new ExcelUtil<VaccineInfoOperation>(VaccineInfoOperation.class);
        util.exportExcel(response, list, "疫苗信息");
    }

    /**
     * 获取疫苗信息详细信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(vaccineInfoService.selectVaccineInfoById(id));
    }

    /**
     * 新增疫苗信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:add')")
    @Log(title = "疫苗信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VaccineInfoOperation vaccineInfo) {
        vaccineInfo.setCreateBy(SecurityUtils.getUsername());
        return toAjax(vaccineInfoService.insertVaccineInfo(vaccineInfo));
    }

    /**
     * 修改疫苗信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:edit')")
    @Log(title = "疫苗信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VaccineInfoOperation vaccineInfo) {
        vaccineInfo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(vaccineInfoService.updateVaccineInfo(vaccineInfo));
    }

    /**
     * 删除疫苗信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:info:remove')")
    @Log(title = "疫苗信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(vaccineInfoService.deleteVaccineInfoByIds(ids));
    }

    /**
     * 删除用户接种疫苗信息
     */
//    @PreAuthorize(hasPermi = "@ss.hasPermi('core:user:remove')")
    @Log(title = "用户接种疫苗信息", businessType = BusinessType.DELETE)
    @GetMapping("/vaccineInfoUser/{ids}")
    public AjaxResult removeVaccineInfoUser(@PathVariable Long[] ids) {
        return toAjax(vaccineInfoUserService.deleteVaccineInfoUserByIds(ids));
    }

    /**
     * 根据身份证号查询用户信息
     */
    @PostMapping("/findHjInfo")
    public AjaxResult findHjInfo(@RequestBody VaccineInfoOperation vaccineInfo) {
        VaccineInfoOperation hjInfo = vaccineInfoService.findHjInfo(vaccineInfo);
        return AjaxResult.success(hjInfo);
    }

    /**
     * 根据身份证号查询用户疫苗信息集合
     */
    @PostMapping("/findInfoUser")
    public AjaxResult findInfoUser(@RequestBody VaccineInfoUser vaccineInfoUser) {
        List<VaccineInfoUser> vaccineInfoUsers = vaccineInfoUserService.selectInfoUserByIdCard(vaccineInfoUser);
        return AjaxResult.success(vaccineInfoUsers);
    }

}
