package com.ruoyi.project.business.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.importExcel.excel.tempInfo.ParaLibraryExcel;
import com.ruoyi.common.importExcel.excel.tempInfo.ParaLibraryExportHelper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.annotation.DataUpdateCheck;
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.TpParaLibrary;
import com.ruoyi.project.business.domain.vo.parabar.ParabarVO;
import com.ruoyi.project.business.service.ITpParaLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 参数Controller
 *
 * @author ruoyi
 * @date 2024-04-23
 */
@RestController
@RequestMapping("/business/library")
public class TpParaLibraryController extends BaseController {
    @Autowired
    private ITpParaLibraryService tpParaLibraryService;

    /**
     * 查询参数列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TpParaLibrary tpParaLibrary) {
        startPage();
        List<TpParaLibrary> list = tpParaLibraryService.selectTpParaLibraryList(tpParaLibrary);
        return getDataTable(list);
    }
    @GetMapping("/listExport")
    public TableDataInfo listExport(TpParaLibrary tpParaLibrary) {
        List<ParaLibraryExportHelper> list = tpParaLibraryService.selectTpParaLibraryListExport(tpParaLibrary);
        return getDataTable(list);
    }

    /**
     * 查询参数列表
     */
    @GetMapping("/listForParabar")
    public List<TpParaLibrary> listForParabar(ParabarVO parabarVO) {
        List<TpParaLibrary> list = tpParaLibraryService.selectTpParaLibraryByParabarId(parabarVO.getParabarId());
        return list;
    }

    /**
     * 导出参数列表
     */
    @Log(title = "参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TpParaLibrary tpParaLibrary) {
        List<TpParaLibrary> list = tpParaLibraryService.selectTpParaLibraryList(tpParaLibrary);
        ExcelUtil<TpParaLibrary> util = new ExcelUtil<TpParaLibrary>(TpParaLibrary.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 获取参数详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tpParaLibraryService.selectTpParaLibraryById(id));
    }

    /**
     * 新增参数
     */
    @Log(title = "参数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TpParaLibrary tpParaLibrary) {
        return toAjax(tpParaLibraryService.insertTpParaLibrary(tpParaLibrary));
    }

    /**
     * 修改参数
     */
    @Log(title = "参数", businessType = BusinessType.UPDATE)
    @DataUpdateCheck(key = "id", tableName ="tp_para_library")
    @PutMapping
    public AjaxResult edit(@RequestBody TpParaLibrary tpParaLibrary) {
        return toAjax(tpParaLibraryService.updateTpParaLibrary(tpParaLibrary));
    }

    /**
     * 删除参数
     */
    @Log(title = "参数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tpParaLibraryService.deleteTpParaLibraryByIds(ids));
    }
    /**
     * 删除参数
     */
    @Log(title = "参数", businessType = BusinessType.DELETE)
    @DataUpdateCheck(key = "id", tableName ="tp_para_library")
    @PostMapping("/delete")
    public AjaxResult removecheck(@RequestBody List<TpParaLibrary> tpParaLibrarys) {
        if(ObjectUtils.isEmpty(tpParaLibrarys)){
            return AjaxResult.error("未填入删除数据！");
        }
        List<Long> idList = tpParaLibrarys.stream().map(TpParaLibrary::getId).collect(Collectors.toList());
        Long[] ids = idList.toArray(new Long[idList.size()]);
        return toAjax(tpParaLibraryService.deleteTpParaLibraryByIds(ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "参数", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    @DataUpdateCheck(key = "id", tableName ="tp_para_library")
    public AjaxResult changeStatus(@RequestBody TpParaLibrary tpParaLibrary) {
        return toAjax(tpParaLibraryService.updateParaStatus(tpParaLibrary));
    }


    /**
     * description: listForConfig  服务于公式配置页面的查询，无需分页
     *
     * @param tpParaLibrary:
     * @return com.ruoyi.framework.web.domain.AjaxResult
     * @author jubs 😀
     * @date 2024/4/28 14:05
     */
    @GetMapping("/listForConfig")
    public AjaxResult listForConfig(TpParaLibrary tpParaLibrary) {
        List<TpParaLibrary> list = tpParaLibraryService.selectTpParaLibraryList(tpParaLibrary);
        return AjaxResult.success(list);
    }
}
