package com.jsowell.pile.transaction.service;

import com.google.common.collect.Lists;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.PileBasicInfo;
import com.jsowell.pile.domain.PileConnectorInfo;
import com.jsowell.pile.mapper.*;
import com.jsowell.pile.service.ClearingBillDetailService;
import com.jsowell.pile.service.ClearingBillInfoService;
import com.jsowell.pile.service.IOrderBasicInfoService;
import com.jsowell.pile.transaction.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用来入库的service
 */
@Slf4j
@Service
public class TransactionService {
	@Resource
	private PileBasicInfoMapper pileBasicInfoMapper;

	@Resource
	private PileConnectorInfoMapper pileConnectorInfoMapper;

	@Resource
	private PileBillingTemplateMapper pileBillingTemplateMapper;

	@Autowired
	private OrderBasicInfoMapper orderBasicInfoMapper;

	@Autowired
	private MemberBasicInfoMapper memberBasicInfoMapper;

	@Autowired
	private MemberWalletInfoMapper memberWalletInfoMapper;

	@Autowired
	private IOrderBasicInfoService orderBasicInfoService;

	@Autowired
	private ClearingBillInfoService clearingBillInfoService;

	@Autowired
	private ClearingBillDetailService clearingBillDetailService;

	/**
	 * 批量新增充电桩和充电桩接口
	 * @param dto
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int doCreatePileTransaction(PileTransactionDTO dto) {
		int maxNum = 100;
		int result = 0;
		List<PileBasicInfo> pileBasicInfoList = dto.getPileBasicInfoList();
		if (CollectionUtils.isNotEmpty(pileBasicInfoList)) {
			List<List<PileBasicInfo>> partition = Lists.partition(pileBasicInfoList, maxNum);
			for (List<PileBasicInfo> basicInfoList : partition) {
				result += pileBasicInfoMapper.batchInsertPileBasicInfo(basicInfoList);
			}
		}
		List<PileConnectorInfo> pileConnectorInfoList = dto.getPileConnectorInfoList();
		if (CollectionUtils.isNotEmpty(pileConnectorInfoList)) {
			List<List<PileConnectorInfo>> partition = Lists.partition(pileConnectorInfoList, maxNum);
			for (List<PileConnectorInfo> connectorInfoList : partition) {
				pileConnectorInfoMapper.batchInsertConnectorInfo(connectorInfoList);
			}
		}
		return result;
	}

	public static void main(String[] args) {
		List<String> list = Lists.newArrayList();
		for (int i = 0; i < 555; i++) {
			list.add(String.valueOf(i + 1));
		}
		System.out.println("原list：" + list);
		int maxNum = 100;
		List<List<String>> partition = Lists.partition(list, maxNum);
		for (int i = 0; i < partition.size(); i++) {
			System.out.println("第" + (i + 1) +"批次：" + partition.get(i));
		}
	}

	/**
	 * 新增计费模板和详情
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void doCreateBillingTemplate(BillingTemplateTransactionDTO dto) {
		log.info("新增计费模板和详情 param:{}", dto.toString());
		if (dto.getBillingTemplate() == null || CollectionUtils.isEmpty(dto.getDetailList())) {
			return;
		}
		// 保存计费模板基本信息
		pileBillingTemplateMapper.insertPileBillingTemplate(dto.getBillingTemplate());
		// log.info("自增id：{}", dto.getBillingTemplate().getId());
		// 保存计费模板详情
		pileBillingTemplateMapper.batchPileBillingDetail(dto.getDetailList());
	}

	/**
	 * 更新计费模板
	 * @param dto
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void doUpdateBillingTemplate(BillingTemplateTransactionDTO dto) {
		if (dto.getBillingTemplate() == null || CollectionUtils.isEmpty(dto.getDetailList())) {
			return;
		}
		// 保存计费模板基本信息
		pileBillingTemplateMapper.updatePileBillingTemplate(dto.getBillingTemplate());
		// 计费模板 先删后插
		pileBillingTemplateMapper.deletePileBillingDetailByTemplateCode(dto.getBillingTemplate().getTemplateCode());
		// 保存计费模板详情
		pileBillingTemplateMapper.batchPileBillingDetail(dto.getDetailList());
	}

	/**
	 * 插入订单
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void doCreateOrder(OrderTransactionDTO dto) {
		Date nowDate = DateUtils.getNowDate();
		if (Objects.nonNull(dto.getOrderBasicInfo())) {
			dto.getOrderBasicInfo().setCreateTime(nowDate);
			orderBasicInfoMapper.insertOrderBasicInfo(dto.getOrderBasicInfo());
		}
		if (Objects.nonNull(dto.getOrderDetail())) {
			dto.getOrderDetail().setCreateTime(nowDate);
			orderBasicInfoMapper.batchOrderDetail(Lists.newArrayList(dto.getOrderDetail()));
		}
	}

	/**
	 * 更新订单信息
	 * @param dto
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void doUpdateOrder(OrderTransactionDTO dto) {
		String orderCode = null;
		String transactionCode = null;
		Date nowDate = DateUtils.getNowDate();
		if (Objects.nonNull(dto.getOrderBasicInfo())) {
			orderCode = dto.getOrderBasicInfo().getOrderCode();
			transactionCode = dto.getOrderBasicInfo().getTransactionCode();
			dto.getOrderBasicInfo().setUpdateTime(nowDate);
			orderBasicInfoMapper.updateOrderBasicInfo(dto.getOrderBasicInfo());
		}
		if (Objects.nonNull(dto.getOrderDetail())) {
			orderCode = dto.getOrderDetail().getOrderCode();
			dto.getOrderDetail().setUpdateTime(nowDate);
			orderBasicInfoMapper.updateOrderDetail(dto.getOrderDetail());
		}
		// 清缓存
		try {
			orderBasicInfoService.cleanCacheByOrderCode(orderCode, transactionCode);
		} catch (Exception e) {
			log.error("更新订单信息清缓存异常", e);
		}
	}

	/**
	 * 新建会员
	 * @param dto
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void createMember(MemberTransactionDTO dto) {
		if (Objects.nonNull(dto.getMemberBasicInfo())) {
			memberBasicInfoMapper.insertMemberBasicInfo(dto.getMemberBasicInfo());
		}
		if (Objects.nonNull(dto.getMemberWalletInfo())) {
			memberWalletInfoMapper.insertSelective(dto.getMemberWalletInfo());
		}
	}

	/**
	 * 保存清分账单
	 */
	public void saveClearingBill(ClearingBillTransactionDTO dto) {
		String clearingBillCode = null;
		if (Objects.nonNull(dto.getClearingBillInfo())) {
			if (dto.getClearingBillInfo().getId() != null) {
				// 如果id不为空，就给clearingBillCode赋值
				clearingBillCode = dto.getClearingBillInfo().getClearingBillCode();
			}
			clearingBillInfoService.insertOrUpdateSelective(dto.getClearingBillInfo());
		}
		if (CollectionUtils.isNotEmpty(dto.getBillDetailList())) {
			if (StringUtils.isNotBlank(clearingBillCode)) {
				// clearingBillCode不为空，就根据clearingBillCode删除后新增数据
				clearingBillDetailService.deleteByClearingBillCode(clearingBillCode);
			}
			clearingBillDetailService.batchInsert(dto.getBillDetailList());
		}
	}

}
