package com.ruoyi.interfaces.controller;

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

import com.ruoyi.common.utils.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.interfaces.domain.PtServiceReturn;
import com.ruoyi.interfaces.service.IPtServiceReturnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 返回数据Controller
 *
 * @author 朱得糠
 * @date 2025-08-29
 */
@RestController
@RequestMapping("/service/return")
public class PtServiceReturnController extends BaseController {
    @Autowired
    private IPtServiceReturnService ptServiceReturnService;

    /**
     * 查询返回数据列表
     */
    @GetMapping("/list")
    public AjaxResult list(PtServiceReturn ptServiceReturn) {
        if (StringUtils.isEmpty(ptServiceReturn.getSrvId())) return error("服务id不可为空");
        List<PtServiceReturn> list = ptServiceReturnService.selectPtServiceReturnList(ptServiceReturn);
        return success(list);
    }

    /**
     * 导出返回数据列表
     */
    @Log(title = "返回数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PtServiceReturn ptServiceReturn) {
        List<PtServiceReturn> list = ptServiceReturnService.selectPtServiceReturnList(ptServiceReturn);
        ExcelUtil<PtServiceReturn> util = new ExcelUtil<PtServiceReturn>(PtServiceReturn.class);
        util.exportExcel(response, list, "返回数据数据");
    }

    /**
     * 获取返回数据详细信息
     */
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(ptServiceReturnService.selectPtServiceReturnByID(ID));
    }

    /**
     * 新增返回数据
     */
    @Log(title = "返回数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtServiceReturn ptServiceReturn) {
        return toAjax(ptServiceReturnService.insertPtServiceReturn(ptServiceReturn));
    }

    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<PtServiceReturn> list) {
        List<PtServiceReturn> collect = list.stream().filter(p -> StringUtils.isNotNull(p.getSrvId()))
                .filter(p -> StringUtils.isNotNull(p.getParentId()))
                .filter(p -> StringUtils.isNotNull(p.getParamCode()))
                .filter(p -> StringUtils.isNotNull(p.getParamType())).collect(Collectors.toList());
        if (StringUtils.isEmpty(collect)) return error("数据为空");

        ptServiceReturnService.deletePtServiceReturnBySrvID(collect.get(0).getSrvId());

        for (int i = 0; i < collect.size(); i++) {
            collect.get(i).setSort(i);
        }

        collect.stream().forEach(ptServiceReturnService::insertPtServiceReturn);
        return success();
    }

    /**
     * 修改返回数据
     */
    @Log(title = "返回数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtServiceReturn ptServiceReturn) {
        return toAjax(ptServiceReturnService.updatePtServiceReturn(ptServiceReturn));
    }

    /**
     * 删除返回数据
     */
    @Log(title = "返回数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(ptServiceReturnService.deletePtServiceReturnByIDs(IDs));
    }
}
