package com.shcd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanghaigeography.Util.DateUtils;
import com.shanghaigeography.Util.SnowflakeConfig;
import com.shcd.entity.ShorePowerDeviceUsage;
import com.shcd.enums.AreaSortEnum;
import com.shcd.mapper.ShorePowerDeviceUsageMapper;
import com.shcd.service.ShorePowerDeviceUsageService;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class ShorePowerDeviceUsageServiceImpl extends
    ServiceImpl<ShorePowerDeviceUsageMapper, ShorePowerDeviceUsage> implements
    ShorePowerDeviceUsageService {

  @Resource
  private SnowflakeConfig snowflakeConfig;

  @Override
  public void generateDeviceUsageNextMonth(String shorePowerNextMonthDate) throws ParseException {
    this.remove(Wrappers.<ShorePowerDeviceUsage>lambdaQuery()
        .eq(ShorePowerDeviceUsage::getReportDate,
            DateUtils.stringToDate(shorePowerNextMonthDate, DateUtils.DATE_FORMAT_12)));
    List<ShorePowerDeviceUsage> shorePowerDeviceUsageList = new ArrayList<>();
    for (AreaSortEnum value : AreaSortEnum.values()) {
      ShorePowerDeviceUsage shorePowerDeviceUsage = new ShorePowerDeviceUsage();
      shorePowerDeviceUsage.setId(snowflakeConfig.snowflakeStringId());
      shorePowerDeviceUsage.setDistrict(value.getArea());
      shorePowerDeviceUsage.setOperateMarinaNumber(value.getOperateMarinaNumber());
      shorePowerDeviceUsage.setShorePowerBoatUseNumber(BigDecimal.ZERO);
      shorePowerDeviceUsage.setPowerConnHours(BigDecimal.ZERO);
      shorePowerDeviceUsage.setElectricityConsumption(BigDecimal.ZERO);
      shorePowerDeviceUsage.setCreateUser("5043");
      shorePowerDeviceUsage.setReportDate(
          DateUtils.stringToDate(shorePowerNextMonthDate, DateUtils.DATE_FORMAT_12));
      shorePowerDeviceUsage.setSort(String.valueOf(value.getSort()));
      shorePowerDeviceUsage.setOwningArea(String.valueOf(value.getOwningArea()));
      shorePowerDeviceUsageList.add(shorePowerDeviceUsage);
    }
    super.saveBatch(shorePowerDeviceUsageList);
  }
}
