package com.bonus.canteen.core.order.deprecated.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.account.v4.api.CardInfoServiceV4Api;
import com.bonus.canteen.core.device.api.DeviceApi;
import com.bonus.canteen.core.device.constants.MacSelectOrdStatus;
import com.bonus.canteen.core.device.manage.vo.DeviceInfoVO;
import com.bonus.canteen.core.menu.model.AllocMealtimeModel;
import com.bonus.canteen.core.order.client.OrderModuleClient;
import com.bonus.canteen.core.order.client.po.MealtimeQueryParam;
import com.bonus.canteen.core.order.common.constants.OrderStateEnum;
import com.bonus.canteen.core.order.common.service.OrderDeliveryService;
import com.bonus.canteen.core.order.common.service.OrderDetailService;
import com.bonus.canteen.core.order.common.service.OrderInfoService;
import com.bonus.canteen.core.order.deprecated.constants.OrdIfWriteOffEnum;
import com.bonus.canteen.core.order.deprecated.constants.OrderStateOldEnum;
import com.bonus.canteen.core.order.deprecated.dto.MacMarketNotWriteOffDTO;
import com.bonus.canteen.core.order.deprecated.dto.MacMarketOrdDTO;
import com.bonus.canteen.core.order.deprecated.dto.OrdInfoMachineDto;
import com.bonus.canteen.core.order.deprecated.dto.OrdLeCodeDTO;
import com.bonus.canteen.core.order.deprecated.model.OrdAppInfo;
import com.bonus.canteen.core.order.deprecated.param.OrdMachineParam;
import com.bonus.canteen.core.order.deprecated.service.OrdAppInfoService;
import com.bonus.canteen.core.order.deprecated.service.OrdInfoMachineBusiness;
import com.bonus.canteen.core.order.deprecated.vo.MacMarketOrdInfoVO;
import com.bonus.canteen.core.order.deprecated.vo.OrdDetailMachineVO;
import com.bonus.canteen.core.order.deprecated.vo.OrdDetaillVO;
import com.bonus.canteen.core.order.deprecated.vo.OrdInfoVO;
import com.bonus.common.houqin.utils.LeBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrdInfoMachineBusinessImpl implements OrdInfoMachineBusiness {
   private static final Logger log = LoggerFactory.getLogger(OrdInfoMachineBusinessImpl.class);
   @Autowired
   @Lazy
   private OrdAppInfoService ordAppInfoService;
   @Autowired
   @Lazy
   private DeviceApi deviceApi;
   @Autowired
   @Lazy
   private OrderModuleClient orderModuleClient;
   @Lazy
   @Autowired
   private OrderInfoService orderInfoService;
   @Lazy
   @Autowired
   private OrderDetailService orderDetailService;
   @Lazy
   @Autowired
   private OrderDeliveryService orderDeliveryService;
   @Autowired
   @Lazy
   private CardInfoServiceV4Api cardInfoServiceV4Api;

   public Page<OrdInfoVO> queryPageOrdInfoMachine(Page<OrdInfoVO> pageParam, OrdInfoMachineDto ordInfoMachineDto) {
      if (ObjectUtil.isNull(ordInfoMachineDto.getSearchDate())) {
         ordInfoMachineDto.setSearchDate(LocalDate.now());
      }

      OrdMachineParam ordMachineParam = (OrdMachineParam) LeBeanUtil.copyCreateProperties(ordInfoMachineDto, OrdMachineParam.class);
      ordMachineParam.setLocaldate(ordInfoMachineDto.getSearchDate());
      if (MacSelectOrdStatus.INTERVAL.key().equals(ordInfoMachineDto.getType())) {
         DeviceInfoVO macMachine = this.deviceApi.getDeviceInfoVoBySnCacheable(ordInfoMachineDto.getMachineSn());
         AllocMealtimeModel mealtime = this.orderModuleClient.mealtimeQuery(MealtimeQueryParam.ofAssignTime(LocalTime.now(), macMachine.getFirstCanteenId(), macMachine.getFirstShopstallId()));
         if (ObjectUtil.isNull(mealtime)) {
            return new Page();
         }

         ordMachineParam.setMealtimeType(mealtime.getMealtimeType());
      }

      Page<OrdInfoVO> ordInfoVoPage = this.ordAppInfoService.queryPageOrdInfoMachine(pageParam, ordMachineParam);
      if (CollUtil.isEmpty(ordInfoVoPage.getRecords())) {
         return ordInfoVoPage;
      } else {
         Map<Long, List<OrdDetailMachineVO>> orderDetailGroup = this.orderDetailService.getOrderDetailList(ordInfoVoPage.getRecords()
                 .stream().map(OrdAppInfo::getOrdId).collect(Collectors.toList()),
                 ordInfoVoPage.getRecords().stream().map(OrdAppInfo::getOrdGoodsDate)
                         .collect(Collectors.toList())).stream().map(OrdDetailMachineVO::of)
                 .collect(Collectors.groupingBy(OrdDetailMachineVO::getOrderId));
         Iterator var6 = ordInfoVoPage.getRecords().iterator();

         while(var6.hasNext()) {
            OrdInfoVO ordInfoVO = (OrdInfoVO)var6.next();
            ordInfoVO.setIfWriteOff(OrderStateEnum.isFinishState(ordInfoVO.getOrdState()) ? OrdIfWriteOffEnum.WRITE_OFF_FINISH.getKey() : OrdIfWriteOffEnum.UN_WRITE_OFF.getKey());
            ordInfoVO.setOrdState(OrderStateOldEnum.convertByNewOrderState(ordInfoVO.getOrdState(), ordInfoVO.getOrdRefundState(), ordInfoVO.getPayState()));
            ordInfoVO.setOrdDetailList(orderDetailGroup.getOrDefault(ordInfoVO.getOrdId(), CollUtil.newArrayList(new OrdDetailMachineVO[0])));
         }

         return ordInfoVoPage;
      }
   }

   public List<OrdDetaillVO> queryOrdInfoDetail(OrdAppInfo ordAppInfo) {
      List<OrdDetailMachineVO> ordDetailList = this.orderDetailService.getOrderDetailList(CollUtil.toList(ordAppInfo.getOrdId()), ListUtil.empty()).stream().map(OrdDetailMachineVO::of).collect(Collectors.toList());
      return BeanUtil.copyToList(ordDetailList, OrdDetaillVO.class);
   }

   public Page<MacMarketOrdInfoVO> pageMacOrdInfoDetailByConditon(MacMarketOrdDTO marketOrdDTO) {
      return this.ordAppInfoService.pageMacOrdInfoDetailByConditon(marketOrdDTO);
   }

   public Page<MacMarketOrdInfoVO> pageMacOrdInfoNotWriteOff(MacMarketNotWriteOffDTO marketOrdDTO) {
      marketOrdDTO.setLocaldate(LocalDate.now());
      if (ObjectUtil.isNotEmpty(marketOrdDTO.getSerialNum())) {
         marketOrdDTO.setCustId(this.cardInfoServiceV4Api.queryCustIdBySerialNum(marketOrdDTO.getSerialNum()));
      }

      if (StrUtil.isNotBlank(marketOrdDTO.getAuthCode())) {
         String leniucode = marketOrdDTO.getAuthCode().substring(4);
         OrdLeCodeDTO ordLeCodeDto = (OrdLeCodeDTO)JSON.parseObject(leniucode, OrdLeCodeDTO.class);
         marketOrdDTO.setOrdId(ordLeCodeDto.getP());
      }

      return this.ordAppInfoService.pageMacOrdInfoNotWriteOff(marketOrdDTO);
   }
}
