package com.willalp.web.controller.yiqing;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
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.HsYiqingUser;
import com.willalp.yiqing.domain.vo.UserQueResultVo;
import com.willalp.yiqing.service.HsYiQingService;
import com.willalp.yiqing.service.IHsYiqingUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 疫情登记用户Controller
 *
 * @author willalp
 * @date 2022-03-29
 */
@RestController
@RequestMapping("/yiqing/yiqing")
public class HsYiqingUserController extends BaseController {
    @Resource
    private IHsYiqingUserService hsYiqingUserService;

    @Resource
    private HsYiQingService yiQingService;

    /**
     * 查询疫情登记用户列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yiqing:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsYiqingUser hsYiqingUser) {
        startPage();
        List<Object> userQueResultVos = yiQingService.selectRegistrationInformationList(hsYiqingUser);
        return getDataTable(userQueResultVos);
    }

    /**
     * 导出疫情登记用户列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yiqing:export')")
    @Log(title = "疫情登记用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsYiqingUser hsYiqingUser) {
        List<Object> userQueResultVos = yiQingService.selectRegistrationInformationList(hsYiqingUser);
        ExcelUtil<Object> util = new ExcelUtil<>(Object.class);
        return util.exportExcel(userQueResultVos, "疫情登记用户数据");
    }

    /**
     * 获取疫情登记用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yiqing:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsYiqingUserService.getById(id));
    }

    /**
     * 新增疫情登记用户
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yiqing:add')")
    @Log(title = "疫情登记用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsYiqingUser hsYiqingUser) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsYiqingUser byId = hsYiqingUserService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsYiqingUser.setId(idStr);
                break;
            }
        }
        hsYiqingUser.setCreateBy(SecurityUtils.getUsername());
        hsYiqingUser.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingUserService.save(hsYiqingUser));
    }

    /**
     * 修改疫情登记用户
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yiqing:edit')")
    @Log(title = "疫情登记用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsYiqingUser hsYiqingUser) {
        hsYiqingUser.setUpdateBy(SecurityUtils.getUsername());
        hsYiqingUser.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingUserService.updateById(hsYiqingUser));
    }

    /**
     * 删除疫情登记用户
     */
    @PreAuthorize("@ss.hasPermi('yiqing:yiqing:remove')")
    @Log(title = "疫情登记用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsYiqingUserService.removeByIds(Arrays.asList(ids)));
    }
}
