package com.pj.tb_collage_orders;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.pj.current.config.SystemObject;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.project4sp.SP;
import com.pj.tb_collage_orders.param.TbCollageOrdersParam;
import com.pj.tb_order.TbOrder;
import com.pj.tb_order.TbOrderService;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.pj.utils.sg.*;

/**
 * Service: tb_collage_orders -- 拼单订单表
 * @author linbl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbCollageOrdersService extends ServiceImpl<TbCollageOrdersMapper, TbCollageOrders> implements IService<TbCollageOrders>{

	/** 底层 Mapper 对象 */
	@Autowired
	TbCollageOrdersMapper tbCollageOrdersMapper;
	@Autowired
	TbOrderService tbOrderService;

	/** 增 */
	void add(TbCollageOrders t){
		save(t);
	}

	/** 删 */
	void delete(Long id){
		removeById(id);
	}

	/** 改 */
	void update(TbCollageOrders t){
		updateById(t);

	}

	/** 查 */
	TbCollageOrders getById(Long id){
		return super.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	List<TbCollageOrders> getList(SoMap so) {
		return tbCollageOrdersMapper.getList(so);
	}

	/** 增 app合作社新增拼单订单 */
	boolean addByCooperative(TbCollageOrdersParam param){
		//拼单订单编号
		String collageOrdersNo = SystemObject.getREFcode("PDDD",new Date());
		param.setOrderNo(collageOrdersNo);
		//1、TODO: 调用航通接口，把他们需要的拼单订单信息发送过去，接口返回的状态码为成功则走接下来的步骤，否则不生成拼单订单，目前暂时省略

		//获取当前登陆人
		APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();

		//2、生成拼单订单
		TbCollageOrders collageOrders = new TbCollageOrders();
		collageOrders.setOrderNo(collageOrdersNo);
		collageOrders.setCooperativeId(param.getCooperativeId());
		collageOrders.setCooperativeName(param.getCooperativeName());

		collageOrders.setTradeAreaId(appLoginInfo.getTradeAreaId());
		collageOrders.setTradeAreaName(appLoginInfo.getTradeAreaName());

		collageOrders.setCreateBy(appLoginInfo.getLoginId());
		collageOrders.setCreateName(appLoginInfo.getLoginName());
		collageOrders.setCreateTime(new Date());

		this.add(collageOrders);
		//获取单订单主键Id
		Long collageOrdersId = SP.publicMapper.getPrimarykey();

		//3、回写边民订单的拼单订单ID
		BigDecimal totalPrice = new BigDecimal(0);
		List<Long> orderList = param.getOrderList();
		for (Long orderId : orderList) {
			TbOrder tbOrder = tbOrderService.getById(orderId);
			//获取边民订单的金额并计算总金额
			totalPrice = totalPrice.add(new BigDecimal(tbOrder.getTotalPrice()));

			tbOrder.setCollageOrdersId(collageOrdersId);
			boolean b = tbOrderService.updateById(tbOrder);
			if(!b) throw new RuntimeException("边民订单表回写拼单订单ID失败!");
		}
		//4、回写拼单订单的总金额
		collageOrders.setTotalPrice(totalPrice.doubleValue());
		collageOrders.setId(collageOrdersId);
		boolean result = this.updateById(collageOrders);

		return result;
	}


}
