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 io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
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 String maMachineRequest) {
        if (StringUtils.isEmpty(maMachineRequest)) {
            return AjaxResult.error("推送数据为空");
        }

        int id = maReceiveService.saveDataReceiveInfo(maMachineRequest);
        if (id == 0) {
            return AjaxResult.error("推送数据失败");
        }
        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);
    }

    @ApiOperation(value = "获取租赁设备状态")
    @GetMapping("/getItemStatus")
    public AjaxResult getDataReceiveDetailsById(@RequestBody String maMachineRequest) {
        Long itemStatus = maReceiveService.getItemStatus(maMachineRequest);
        return success(itemStatus);
    }

    @ApiOperation(value = "修改租赁设备状态")
    @PostMapping("/updateItemStatus")
    public AjaxResult updateItemStatus(@RequestBody String maMachineRequest) {
        Integer itemStatus = maReceiveService.updateItemStatus(maMachineRequest);
        return success(itemStatus);
    }
}
