package com.bonus.sgzb.base.controller;

import cn.hutool.core.collection.CollUtil;
import com.bonus.sgzb.base.domain.DataReceiveDetail;
import com.bonus.sgzb.base.domain.DataReceiveInfo;
import com.bonus.sgzb.base.service.MaReceiveService;
import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author：liang.chao
 * @Date：2024/7/24 - 10:47
 */

@RestController
@RequestMapping("/base/receive")
public class MaReceiveController extends BaseController {

    @Resource
    private MaReceiveService maReceiveService;

    /**
     * 数据推送接收
     */
    @ApiOperation(value = "获取推送数据")
    @PostMapping("/dataReceive")
    public AjaxResult getProjectInfoAll(@RequestBody List<DataReceiveDetail> dataReceiveDetails) {
        if (CollUtil.isEmpty(dataReceiveDetails)) {
            return AjaxResult.error("推送数据为空");
        }
        DataReceiveInfo dataReceiveInfo = new DataReceiveInfo();
        dataReceiveInfo.setPushNum(dataReceiveDetails.size());
        int id = maReceiveService.saveDataReceiveInfo(dataReceiveInfo);
        if (id == 0) {
            return AjaxResult.error("推送数据失败");
        }
        for (DataReceiveDetail dataReceiveDetail : dataReceiveDetails) {
            dataReceiveDetail.setReceiveId(id);
            maReceiveService.saveDataReceiveDetails(dataReceiveDetail);
        }
        return AjaxResult.success("数据推送成功");
    }


    @ApiOperation(value = "获取推送数据")
    @GetMapping("/getDataReceive")
    public TableDataInfo getDataReceive(DataReceiveInfo dataReceiveInfo) {
        startPage();
        List<DataReceiveInfo> list = maReceiveService.getDataReceive(dataReceiveInfo);
        return getDataTable(list);
    }

    @ApiOperation(value = "导出推送数据")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataReceiveInfo dataReceiveInfo) {
        List<DataReceiveInfo> list = maReceiveService.getDataReceive(dataReceiveInfo);
        ExcelUtil<DataReceiveInfo> util = new ExcelUtil<>(DataReceiveInfo.class);
        util.exportExcel(response, list, "推送数据列表");
    }

    @ApiOperation(value = "获取推送详情")
    @GetMapping("/getDataReceiveDetails")
    public TableDataInfo getDataReceiveDetails(DataReceiveDetail dataReceiveDetail) {
        startPage();
        List<DataReceiveDetail> dataReceiveDetails = maReceiveService.getDataReceiveDetails(dataReceiveDetail);
        return getDataTable(dataReceiveDetails);
    }

    @ApiOperation(value = "数据接收")
    @PostMapping("/saveMachine")
    public AjaxResult saveMachine(@RequestBody DataReceiveInfo dataReceiveInfo) {
        if (CollUtil.isEmpty(dataReceiveInfo.getDataReceiveDetailList())) {
            return AjaxResult.error("接收数据为空");
        }
        int res = maReceiveService.saveMachine(dataReceiveInfo);
        if (res == 0) {
            return AjaxResult.error("接收失败");
        } else {
            return AjaxResult.success("接收成功");
        }
    }


    @ApiOperation(value = "数据接收")
    @GetMapping("/getDateReceiveMachine")
    public TableDataInfo getDateReceiveMachine(DataReceiveDetail dataReceiveDetail) {
        startPage();
        List<DataReceiveDetail> dateReceiveMachine = maReceiveService.getDateReceiveMachine(dataReceiveDetail);
        return getDataTable(dateReceiveMachine);
    }
}
