package com.bonus.canteen.core.android.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.android.dto.AppDTO;
import com.bonus.canteen.core.android.service.DeviceService;
import com.bonus.canteen.core.android.vo.CookRecipeCompressVO;
import com.bonus.canteen.core.android.vo.DeviceTimeIntervalVO;
import com.bonus.canteen.core.device.domain.DeviceSearchDTO;
import com.bonus.canteen.core.device.service.IDeviceInfoService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.utils.HeaderFetchUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @author 19814
 */
@Api(tags = "android-双屏消费机-设备信息")
@RestController
@RequestMapping("/api/android/device")
public class DeviceController extends BaseController {
    @Resource
    private DeviceService deviceService;
    @Resource
    private IDeviceInfoService deviceInfoService;


    @ApiOperation("获取设备信息")
    @RequestMapping("/getDeviceInfo")
    public AjaxResult getDeviceInfo(@RequestBody AppDTO dto) {
        if (ObjectUtil.isEmpty(dto.getDeviceSn())) {
            throw new ServiceException("设备sn不能为空");
        }
        return deviceService.getDeviceInfo(dto);
    }
    @ApiOperation("获取设备版本信息")
    @RequestMapping("/getAppVersion")
    public AjaxResult getAppVersion(@RequestBody AppDTO dto) {
        if (ObjectUtil.isEmpty(dto.getId())) {
            throw new ServiceException("Version不能为空");
        }
        return deviceService.getAppVersion(dto);
    }

    @ApiOperation(value = "获取参数")
    @PostMapping(value = "/getParamInfo")
    public AjaxResult getInfo() {
        return success(deviceService.getParamInfo());
    }


    @PostMapping({"/getMenuInfoBySn"})
    @ApiOperation("根据sn获取菜谱菜品信息")
    public CookRecipeCompressVO getMenuInfoBySn(@RequestHeader Map<String, String> headers) {
        String machineSn = HeaderFetchUtil.getMachineSn(headers);
        if (machineSn == null){
            throw new ServiceException("设备sn为空");
        }
        return this.deviceService.getMenuInfoBySn(machineSn);
    }

    @PostMapping({"/getMealTimeBySn"})
    @ApiOperation("根据sn码获餐次信息")
    public DeviceTimeIntervalVO intervalConfig(@RequestHeader Map<String, String> headers) {
        return this.deviceService.intervalConfig(HeaderFetchUtil.getMachineSn(headers));
    }

    @ApiOperation(value = "更新设备在线状态")
    @PostMapping({"/updateTimeBySn"})
    public AjaxResult updateTimeBySn(@RequestBody DeviceSearchDTO dto) {
        if (ObjectUtil.isEmpty(dto.getDeviceSn())) {
            throw new ServiceException("设备sn不能为空");
        }
        this.deviceInfoService.updateTimeBySn(dto.getDeviceSn());
        return AjaxResult.success();
    }

}
