package com.jsowell.pile.transaction.service;

import com.google.common.collect.Lists;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.pile.domain.PileBasicInfo;
import com.jsowell.pile.domain.PileConnectorInfo;
import com.jsowell.pile.mapper.MemberBasicInfoMapper;
import com.jsowell.pile.mapper.MemberWalletInfoMapper;
import com.jsowell.pile.mapper.OrderBasicInfoMapper;
import com.jsowell.pile.mapper.PileBasicInfoMapper;
import com.jsowell.pile.mapper.PileBillingTemplateMapper;
import com.jsowell.pile.mapper.PileConnectorInfoMapper;
import com.jsowell.pile.service.IOrderBasicInfoService;
import com.jsowell.pile.transaction.dto.BillingTemplateTransactionDTO;
import com.jsowell.pile.transaction.dto.MemberTransactionDTO;
import com.jsowell.pile.transaction.dto.OrderTransactionDTO;
import com.jsowell.pile.transaction.dto.PileTransactionDTO;
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.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 RedisCache redisCache;

	/**
	 * 批量新增充电桩和充电桩接口
	 * @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) {
		if (Objects.nonNull(dto.getOrderBasicInfo())) {
			orderBasicInfoMapper.insertOrderBasicInfo(dto.getOrderBasicInfo());
		}

		if (Objects.nonNull(dto.getOrderDetail())) {
			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;
		if (Objects.nonNull(dto.getOrderBasicInfo())) {
			orderCode = dto.getOrderBasicInfo().getOrderCode();
			transactionCode = dto.getOrderBasicInfo().getTransactionCode();
			orderBasicInfoMapper.updateOrderBasicInfo(dto.getOrderBasicInfo());
		}

		if (Objects.nonNull(dto.getOrderDetail())) {
			orderCode = dto.getOrderDetail().getOrderCode();
			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());
		}
	}

}
