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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bonus.canteen.core.allocation.canteen.vo.AllocCanteenTreeVO;
import com.bonus.canteen.core.auth.api.MgrAuthApi;
import com.bonus.canteen.core.common.page.PageDTO;
import com.bonus.canteen.core.common.page.PageVO;
import com.bonus.canteen.core.common.page.QueryParseHelper;
import com.bonus.canteen.core.device.manage.controller.deprecated.vo.MachineCounterTypeDto;
import com.bonus.canteen.core.device.manage.controller.deprecated.vo.MachineCounterVO;
import com.bonus.canteen.core.device.manage.dto.DeviceSearchDTO;
import com.bonus.canteen.core.device.manage.model.DeviceInfo;
import com.bonus.canteen.core.device.manage.service.DeviceInfoService;
import com.bonus.canteen.core.device.manage.vo.DeviceFullInfoVO;
import com.bonus.canteen.core.device.utils.DeviceDtoConverter;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.common.utils.BaseTreeNode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(
   tags = {"设备管理模块"}
)
@RequestMapping({"/api/v1/machine"})
public class DeviceInfoController {
   private static final Logger log = LoggerFactory.getLogger(DeviceInfoController.class);
   @Autowired
   private DeviceInfoService deviceInfoService;
   @Autowired
   private MgrAuthApi mgrAuthApi;

//   @ApiOperation(
//      value = "添加新设备",
//      notes = "必传参数商家id、食堂id,设备编号、设备名称、设备类型、设备sn码、设备ip"
//   )
//   @PostMapping({"/postMachine"})
//   @RequiresAuthentication
//   public LeResponse<?> postMachine(@RequestBody @Valid LeRequest<MachineCounterDto> request) {
//      this.deviceInfoService.insert(DeviceDtoConverter.toDeviceDto((MachineCounterDto)request.getContent()));
//      return LeResponse.succ();
//   }
//
//   @ApiOperation(
//      value = "更新设备信息",
//      notes = "设备绑定档口、更换食堂、档口信息;参数:设备sn码"
//   )
//   @PutMapping({"/putMachins"})
//   @RequiresAuthentication
//   public LeResponse putMachins(@RequestBody LeRequest<MachineCounterDto> request) {
//      this.deviceInfoService.update(DeviceDtoConverter.toDeviceDto((MachineCounterDto)request.getContent()));
//      return LeResponse.succ();
//   }
//
//   @PutMapping({"/updateMacIssue"})
//   @RequiresAuthentication
//   @ApiOperation(
//      value = "统一更新发布屏字体配置",
//      notes = "字体大小和字体颜色，颜色使用16进制"
//   )
//   public LeResponse<?> updateMacIssue(@RequestBody LeRequest<List<MacIssue>> request) {
//      List<MacIssue> macIssueList = (List)request.getContent();
//      this.deviceInfoService.updateIssueConfig(DeviceDtoConverter.toDeviceIssueDTO(macIssueList));
//      return LeResponse.succ();
//   }
//
//   @ApiOperation("删除设备信息")
//   @DeleteMapping({"/delMachine"})
//   @RequiresAuthentication
//   public LeResponse<?> delMachine(@RequestBody LeRequest<MacMachine> request) {
//      MacMachine macMachine = (MacMachine)request.getContent();
//      if (null != macMachine.getMachineId() && null != macMachine.getMchType()) {
//         this.deviceInfoService.delete(macMachine.getMachineId());
//         return LeResponse.succ();
//      } else {
//         return LeResponse.fail("[设备信息]删除设备：设备id或设备类型为空");
//      }
//   }
//
//   @ApiOperation("删除打印机信息")
//   @DeleteMapping({"/delPrinter"})
//   @RequiresAuthentication
//   public LeResponse<?> delPrinter(@RequestBody LeRequest<MacMachine> request) {
//      MacMachine macMachine = (MacMachine)request.getContent();
//      if (null == macMachine.getMachineId()) {
//         return LeResponse.fail("[设备信息]删除打印机设备参数为空");
//      } else {
//         this.deviceInfoService.delete(macMachine.getMachineId());
//         return LeResponse.succ();
//      }
//   }
//
//   @GetMapping({"/getMachineBySn"})
//   @RequiresGuest
//   @ApiOperation("设备sn码获取设备配置信息")
//   public MachineCounterVO getMachineBySn(LeRequest<String> request) {
//      String machineSn = (String)QueryParseHelper.getValueFromContent((String)request.getContent(), "machineSn", (Object)"");
//      if (StrUtil.isEmpty(machineSn)) {
//         throw new LeException("sn查询参数为空");
//      } else {
//         DeviceInfo deviceInfo = this.deviceInfoService.getBySn(machineSn);
//         List<DeviceFullInfoVO> deviceFullInfoVOList = this.deviceInfoService.fillFullVo(CollUtil.toList(new DeviceInfo[]{deviceInfo}));
//         return DeviceDtoConverter.toMachineCounterVO((DeviceFullInfoVO)deviceFullInfoVOList.get(0));
//      }
//   }

   @ApiOperation(
      value = "查询设备",
      notes = "分页查询设备信息"
   )
   @PostMapping({"/getMachineByPage", "getMachineTypeByPage"})
//   @RequiresAuthentication
   public PageVO<MachineCounterVO> getMachineByPage(@RequestParam String content) {
      MachineCounterTypeDto oldDto = QueryParseHelper.getParamFromContent(content, MachineCounterTypeDto.class);
      DeviceSearchDTO param = DeviceDtoConverter.toDeviceSearchDTO(oldDto);
      PageDTO page = QueryParseHelper.getPageFromContent(content);
      List<AllocCanteenTreeVO> systemCanteen = this.mgrAuthApi.getTenantCanteen(LeConstants.COMMON_YES,
              DelFlagEnum.DEL_FALSE.key(), true);
      Collection<Long> canteenStallIdIdList = systemCanteen.stream().filter((node) -> {
         return node.ifCanteen() || node.ifStall();
      }).map(BaseTreeNode::getId).collect(Collectors.toList());
      if (ObjectUtil.isNotEmpty(canteenStallIdIdList)) {
         param.setCanteenStallIdListPermission(canteenStallIdIdList);
      }

      PageHelper.startPage(page);
      Page<DeviceInfo> dataPage = (Page)this.deviceInfoService.listPermission(param);
      List<DeviceFullInfoVO> voList = this.deviceInfoService.fillFullVo(dataPage);
      return PageVO.of(dataPage, DeviceDtoConverter.toMachineCounterVO(voList));
   }

//   @ApiOperation("查询支付设备列表")
//   @PostMapping({"/getPayMachineList"})
//   @RequiresAuthentication
//   public List<PayOrderMachineVO> getPayMachineList(@RequestBody LeRequest<String> request) {
//      List<DeviceInfo> deviceInfoList = this.deviceInfoService.listPaymentDevice();
//      return DeviceDtoConverter.toPayOrderMachineVoList(deviceInfoList);
//   }
//
//   /** @deprecated */
//   @ApiOperation("查看餐线设备")
//   @PostMapping({"/meal/list"})
//   @RequiresAuthentication
//   public List<IssueRecipeDTO> queryMealNumList(@RequestBody LeRequest<MachineRecipeRelease> request) {
//      MachineRecipeRelease machineRecipeRelease = (MachineRecipeRelease)request.getContent();
//      if (ObjectUtil.isEmpty(machineRecipeRelease.getCanteenId()) && ObjectUtil.isEmpty(machineRecipeRelease.getShopstallId())) {
//         throw new LeException("食堂档口ID不可同时为空");
//      } else {
//         DeviceSearchDTO param = DeviceDtoConverter.toDeviceSearchDTO(machineRecipeRelease);
//         param.setDeviceTypeList(ListUtil.toList(new Integer[]{SourceTypeEnum.PRODUCTION.getKey(), SourceTypeEnum.AUTONOMOUS_WEIGHING.getKey()}));
//         List<DeviceInfo> deviceInfoList = this.deviceInfoService.list(param);
//         List<DeviceInfoVO> deviceInfoVOList = this.deviceInfoService.fillVo(deviceInfoList);
//         return DeviceDtoConverter.toIssueRecipeDTO(deviceInfoVOList);
//      }
//   }
//
//   @ApiOperation("设备激活收钱吧")
//   @PostMapping({"/shouqianbaActivate"})
//   @RequiresPermissions(
//      value = {"machine:activate:shouqianbaActivate", "AIcaipingshibie:activate:shouqianbaActivate", "androidbind:activate:shouqianbaActivate", "androidguashiji:activate:shouqianbaActivate", "cantaimachine:activate:shouqianbaActivate", "diancanji:activate:shouqianbaActivate", "saomashoufeiji:activate:shouqianbaActivate", "shuangpingxiaofeiji:activate:shouqianbaActivate", "zhifupos:activate:shouqianbaActivate"},
//      logical = Logical.OR
//   )
//   @RequiresAuthentication
//   public SqbBizResponse shouqianbaActivate(@RequestBody LeRequest<SqbActivationDTO> request) {
//      SqbActivationDTO sqbActivationDTO = (SqbActivationDTO)request.getContent();
//      return ObjectUtil.isEmpty(sqbActivationDTO) ? null : this.deviceInfoService.shouqianbaActivate(sqbActivationDTO);
//   }
}
