package com.ruoyi.project.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TpParaSelectItem;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.service.ITpParaSelectItemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 选项Controller
 *
 * @author ruoyi
 * @date 2024-04-23
 */
@RestController
@RequestMapping("/business/selectItem")
public class TpParaSelectItemController extends BaseController {
    @Resource
    private ITpParaSelectItemService tpParaSelectItemService;

    /**
     * 查询选项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TpParaSelectItem tpParaSelectItem) {
        startPage();
        List<TpParaSelectItem> list = tpParaSelectItemService.selectTpParaSelectItemList(tpParaSelectItem);
        return getDataTable(list);
    }

    /**
     * 导出选项列表
     */
    @Log(title = "选项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TpParaSelectItem tpParaSelectItem) {
        List<TpParaSelectItem> list = tpParaSelectItemService.selectTpParaSelectItemList(tpParaSelectItem);
        ExcelUtil<TpParaSelectItem> util = new ExcelUtil<TpParaSelectItem>(TpParaSelectItem.class);
        util.exportExcel(response, list, "选项数据");
    }

    /**
     * 获取选项详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tpParaSelectItemService.selectTpParaSelectItemById(id));
    }

    /**
     * 新增选项
     */
    @Log(title = "选项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TpParaSelectItem tpParaSelectItem) {
        return toAjax(tpParaSelectItemService.insertTpParaSelectItem(tpParaSelectItem));
    }

    /**
     * 修改选项
     */
    @Log(title = "选项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TpParaSelectItem tpParaSelectItem) {
        return toAjax(tpParaSelectItemService.updateTpParaSelectItem(tpParaSelectItem));
    }

    /**
     * 删除选项
     */
    @Log(title = "选项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tpParaSelectItemService.deleteTpParaSelectItemByIds(ids));
    }


    @GetMapping("/getSelectItemListByParaId/{paraId}")
    public AjaxResult getSelectItemListByParaId(@PathVariable("paraId") Long paraId) {
        return AjaxResult.success(
                tpParaSelectItemService.list(
                        Wrappers.<TpParaSelectItem>lambdaQuery()
                                .eq(TpParaSelectItem::getParaId, paraId)
                                .eq(TpParaSelectItem::getDelFlag, DelFlagEnum.NORMAL.getValue())
                )
        );
    }
}
