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

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.alloc.domain.AllocCanteen;
import com.bonus.canteen.core.alloc.domain.AllocStall;
import com.bonus.canteen.core.alloc.mapper.AllocCanteenMapper;
import com.bonus.canteen.core.alloc.mapper.AllocStallMapper;
import com.bonus.canteen.core.device.dto.ApkDTO;
import com.bonus.canteen.core.device.dto.DeviceDTO;
import com.bonus.canteen.core.device.dto.DeviceSearchDTO;
import com.bonus.canteen.core.device.service.IDoubleScreenMachineService;
import com.bonus.canteen.core.device.vo.DeviceFullInfoVO;
import com.bonus.common.core.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;

/**
 * 设备绑定多档口子Controller
 * 
 * @author xsheng
 * @date 2025-04-03
 */
@Api(tags = "后台-双屏消费机")
@RestController
@RequestMapping("/doubleScreenMachine")
public class DoubleScreenMachineController extends BaseController {
    @Autowired
    private IDoubleScreenMachineService screenMachineService;
    @Autowired
    private AllocCanteenMapper allocCanteenMapper;
    @Autowired
    private AllocStallMapper allocStallMapper;
    @ApiOperation(
            value = "分页查询设备信息",
            notes = "page：分页信息，object：canteenId:食堂ID, stallId:店铺档口id, deviceState:1在线2离线, "
    )
    @GetMapping({"/getDoubleScreenMachineList"})
    public TableDataInfo getDoubleScreenMachineList(DeviceSearchDTO dto) {
        List<Long> canteenIds;
        List<Long> stallIds;
        if (ObjectUtil.isNotEmpty(dto.getAreaId()) && ObjectUtil.isEmpty(dto.getCanteenId())) {
            AllocCanteen allocCanteen = new AllocCanteen();
            allocCanteen.setAreaId(dto.getAreaId());
            List<AllocCanteen> canteenList = this.allocCanteenMapper.selectAllocCanteenList(allocCanteen);
            if (canteenList != null && canteenList.size() > 0){
                canteenIds = canteenList.stream().map(AllocCanteen::getCanteenId).collect(Collectors.toList());
                dto.setCanteenIdList(canteenIds);
            }else{
                return getDataTable(new ArrayList<>());
            }
        }
        if (ObjectUtil.isNotEmpty(dto.getAreaId()) && ObjectUtil.isNotEmpty(dto.getCanteenId()) && ObjectUtil.isEmpty(dto.getStallId())) {
            AllocStall allocStall = new AllocStall();
            allocStall.setCanteenId(dto.getCanteenId());
            allocStall.setAreaId(dto.getAreaId());
            List<AllocStall> stallList = this.allocStallMapper.selectAllocStallList(allocStall);
            if (stallList != null && stallList.size() > 0){
                stallIds = stallList.stream().map(AllocStall::getStallId).collect(Collectors.toList());
                dto.setStallIdList(stallIds);
            }else {
                return getDataTable(new ArrayList<>());
            }
        }
        startPage();
        List<DeviceFullInfoVO> list = screenMachineService.getDoubleScreenMachineList(dto);
        return getDataTable(list);
    }

    @ApiOperation(
            value = "添加新设备",
            notes = "必传参数商家id、食堂id,设备编号、设备名称、设备类型、设备sn码、设备ip"
    )
    @PostMapping({"/insert"})
    public AjaxResult insertMachine(@RequestBody @Valid DeviceDTO dto) {
        this.screenMachineService.insert(dto);
        return AjaxResult.success();
    }

    @ApiOperation("更新设备信息")
    @PostMapping({"/update"})
    public AjaxResult updateDevice(@RequestBody DeviceDTO dto) {
        if (dto.getDeviceId() == null) {
            throw new ServiceException("设备id不能为空");
        } else {
            this.screenMachineService.update(dto);
            return AjaxResult.success();
        }
    }

    @ApiOperation(
            value = "删除设备信息",
            notes = "content：设备id"
    )
    @PostMapping({"/delete"})
    public AjaxResult delDevice(@RequestParam String deviceId) {
        if (ObjectUtil.isEmpty(deviceId)) {
            throw new ServiceException("设备id不能为空");
        }
        this.screenMachineService.delete(deviceId);
        return AjaxResult.success();
    }
    @ApiOperation(value = "更新设备在线状态")
    @PostMapping({"/updateTimeBySn"})
    public AjaxResult updateTimeBySn(@RequestBody DeviceSearchDTO dto) {
        if (ObjectUtil.isEmpty(dto.getDeviceSn())) {
            throw new ServiceException("设备sn不能为空");
        }
        this.screenMachineService.updateTimeBySn(dto.getDeviceSn());
        return AjaxResult.success();
    }

    @ApiOperation(value = "上传APK")
    @PostMapping({"/uploadApk"})
    public AjaxResult uploadApk(@RequestBody @Valid ApkDTO dto) {
        return this.screenMachineService.uploadApk(dto);
    }

}
