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.HsYiqingUserQueResult;
import com.willalp.yiqing.service.IHsYiqingUserQueResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 用户登记结果Controller
 *
 * @author willalp
 * @date 2022-03-29
 */
@RestController
@RequestMapping("/yiqing/result")
public class HsYiqingUserQueResultController extends BaseController {
    @Autowired
    private IHsYiqingUserQueResultService hsYiqingUserQueResultService;

    /**
     * 查询用户登记结果列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:result:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsYiqingUserQueResult hsYiqingUserQueResult) {
        startPage();
        List<HsYiqingUserQueResult> list = hsYiqingUserQueResultService.selectHsYiqingUserQueResultList(hsYiqingUserQueResult);
        return getDataTable(list);
    }

    /**
     * 导出用户登记结果列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:result:export')")
    @Log(title = "用户登记结果", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsYiqingUserQueResult hsYiqingUserQueResult) {
        List<HsYiqingUserQueResult> list = hsYiqingUserQueResultService.selectHsYiqingUserQueResultList(hsYiqingUserQueResult);
        ExcelUtil<HsYiqingUserQueResult> util = new ExcelUtil<HsYiqingUserQueResult>(HsYiqingUserQueResult.class);
        return util.exportExcel(list, "用户登记结果数据");
    }

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

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

    /**
     * 修改用户登记结果
     */
    @PreAuthorize("@ss.hasPermi('yiqing:result:edit')")
    @Log(title = "用户登记结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsYiqingUserQueResult hsYiqingUserQueResult) {
        hsYiqingUserQueResult.setUpdateBy(SecurityUtils.getUsername());
        hsYiqingUserQueResult.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingUserQueResultService.updateById(hsYiqingUserQueResult));
    }

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