package com.willalp.web.controller.yiqing;

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

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
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 com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.BusinessType;
import com.willalp.yiqing.domain.HsYiqingQueGroup;
import com.willalp.yiqing.service.IHsYiqingQueGroupService;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 问题分组Controller
 *
 * @author willalp
 * @date 2022-04-28
 */
@RestController
@RequestMapping("/yiqing/queGroup")
public class HsYiqingQueGroupController extends BaseController
{
    @Resource
    private IHsYiqingQueGroupService hsYiqingQueGroupService;

    /**
     * 查询问题分组列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:queGroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsYiqingQueGroup hsYiqingQueGroup)
    {
        startPage();
        List<HsYiqingQueGroup> list = hsYiqingQueGroupService.selectHsYiqingQueGroupList(hsYiqingQueGroup);
        return getDataTable(list);
    }

    /**
     * 导出问题分组列表
     */
    @PreAuthorize("@ss.hasPermi('yiqing:queGroup:export')")
    @Log(title = "问题分组", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsYiqingQueGroup hsYiqingQueGroup)
    {
        List<HsYiqingQueGroup> list = hsYiqingQueGroupService.selectHsYiqingQueGroupList(hsYiqingQueGroup);
        ExcelUtil<HsYiqingQueGroup> util = new ExcelUtil<HsYiqingQueGroup>(HsYiqingQueGroup.class);
        return util.exportExcel(list, "问题分组数据");
    }

    /**
     * 获取问题分组详细信息
     */
    @PreAuthorize("@ss.hasPermi('yiqing:queGroup:query')")
    @GetMapping(value = "/{queGroupId}")
    public AjaxResult getInfo(@PathVariable("queGroupId") Long queGroupId)
    {
        return AjaxResult.success(hsYiqingQueGroupService.getById(queGroupId));
    }

    /**
     * 新增问题分组
     */
    @PreAuthorize("@ss.hasPermi('yiqing:queGroup:add')")
    @Log(title = "问题分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsYiqingQueGroup hsYiqingQueGroup)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsYiqingQueGroup byId = hsYiqingQueGroupService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsYiqingQueGroup.setQueGroupId(idStr);
                break;
            }
        }
        hsYiqingQueGroup.setCreateBy(SecurityUtils.getUsername());
        hsYiqingQueGroup.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingQueGroupService.save(hsYiqingQueGroup));
    }

    /**
     * 修改问题分组
     */
    @PreAuthorize("@ss.hasPermi('yiqing:queGroup:edit')")
    @Log(title = "问题分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsYiqingQueGroup hsYiqingQueGroup)
    {
        hsYiqingQueGroup.setUpdateBy(SecurityUtils.getUsername());
        hsYiqingQueGroup.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsYiqingQueGroupService.updateById(hsYiqingQueGroup));
    }

    /**
     * 删除问题分组
     */
    @PreAuthorize("@ss.hasPermi('yiqing:queGroup:remove')")
    @Log(title = "问题分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{queGroupIds}")
    public AjaxResult remove(@PathVariable Long[] queGroupIds)
    {
        return toAjax(hsYiqingQueGroupService.removeByIds(Arrays.asList(queGroupIds)));
    }
}
