package com.iamberry.wechat.service.order;

import java.util.ArrayList;
import java.util.List;

import com.iamberry.wechat.tools.NameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.order.Order;
import com.iamberry.wechat.core.entity.order.OrderItem;
import com.iamberry.wechat.core.entity.order.ProbationCode;
import com.iamberry.wechat.core.entity.order.ProbationOrder;
import com.iamberry.wechat.core.entity.order.ProbationOrderDto;
import com.iamberry.wechat.core.entity.probation.ProbationAwardRelu;
import com.iamberry.wechat.core.entity.probation.ProbationNewLogs;
import com.iamberry.wechat.core.entity.probation.ProbationUserInfo;
import com.iamberry.wechat.core.entity.probation.ProbationUserDto;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.face.order.ProbationShopOrderService;
import com.iamberry.wechat.service.mapper.AdminProbationOrderMapper;
import com.iamberry.wechat.service.mapper.OrderMapper;
import com.iamberry.wechat.service.mapper.ProbationShopOrderMapper;
import com.iamberry.wechat.tools.OrderNOUtil;

/**
 * 试用订单
 * 22016年11月15日17:29:03
 * @author LJK
 *
 */
@Service
public class ProbationShopOrderServiceImpl implements ProbationShopOrderService{
	@Autowired
	private ProbationShopOrderMapper probationShopOrderMapper;
	
	@Autowired
	private AdminProbationOrderMapper adminProbationOrderMapper;
	
	@Autowired
	private OrderMapper orderMapper;
	
	//根据用户openID 查询会员试用订单
	@Override
	public List<ProbationOrderDto> selectProbationOrderByOpenid(String openId) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProbationOrderByOpenid(openId);
	}
	
	//试用订单奖励规则
	@Override
	public List<ProbationAwardRelu> selectProbationAwardRelu() {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProbationAwardRelu();
	}

	//查询所有试用开始订单
	@Override
	public List<ProbationOrderDto> selectProbationStartOrder() {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProbationStartOrder();
	}
	//根据订单ID查询 发放奖励阶段
	@Override
	public ProbationNewLogs selectProbationNewLogsByOrderId(String logsOrderid) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProbationNewLogsByOrderId(logsOrderid);
	}
	//添加奖励推送日志
	@Override
	public Integer insertProbationLogs(ProbationNewLogs probationNewLogs) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.insertProbationLogs(probationNewLogs);
	}

	
	//修改奖励推送日志推送小时数
	@Override
	public Integer updatePauseLogsDate(ProbationNewLogs probationNewLogs) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.updatePauseLogsDate(probationNewLogs);
	}
	//根据手机号查询验证码信息
	@Override
	public ProbationCode selectProbationCodeByTel(String puserTel) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProbationCodeByTel(puserTel);
	}
	//添加奖励推送code
	@Override
	public Integer insertProbationCode(ProbationCode probationCode) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.insertProbationCode(probationCode);
	}
	//修改奖励推送code 
	@Override
	public Integer updatePauseCode(ProbationCode probationCode) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.updatePauseCode(probationCode);
	}

	//查询是否已存在申请记录
	@Override
	public ProbationUserInfo selectProbationOrderByTel(String puserTel) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProbationOrderByTel(puserTel);
	}

	@Override
	public Product selectProductInfo(Integer productId, Integer colorId) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProductInfo(productId,colorId);
	}

	@Override
	public Integer addProbationOrder(ProbationOrderDto probationOrderDto) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.addProbationOrder(probationOrderDto);
	}

	@Override
	public Integer addProbationUser(ProbationUserDto probationUserDto) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.addProbationUser(probationUserDto);
	}
	
	@Override
	public ProbationOrderDto selectProbationOrderByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProbationOrderByOrderId(orderId);
	}

	@Override
	public Integer insertSalesOrder(String probationOrderId, Integer userId) {
		// TODO Auto-generated method stub
		String sdId = NameUtils.getConfig("sd_id");
		if (probationOrderId == null || "".equals(probationOrderId)) {
			return 0;
		}
		ProbationOrder probationOrder =  new ProbationOrder();
		probationOrder.setProbationOrderid(probationOrderId);
		//查询试用订单信息和商品信息
		probationOrder = adminProbationOrderMapper.getProbationOrderByOrderId(probationOrder);
		
		if (probationOrder == null) {
			return 0;
		}
		//将使用订单封装到销售订单中
		Order order = new Order();
		//Efast升级新订单生成规则
		order.setSalesOrderid(OrderNOUtil.createNewOrderCode(userId,sdId));
		order.setSalesOpenid(probationOrder.getProbationOpenid());
		order.setSalesPorderid(probationOrder.getProbationOrderid());
		order.setSalesYetAmount(probationOrder.getProbationYetAmount());
		order.setSalesAmount(probationOrder.getProbationAmount());
		order.setSalesAddressInfo(probationOrder.getProbationAddressInfo());
		order.setSalesAddressTel(probationOrder.getProbationReceiveTel());
		order.setSalesAddressName(probationOrder.getProbationReceiveName());
		order.setSalesUserRemark(probationOrder.getProbationUserRemark());
		order.setSalesStatus(5);
		order.setSalesTransactionId(probationOrder.getProbationTransactionId());
		order.setSalesTransactionDate(probationOrder.getProbationTransactionTime());
		order.setSalesOrderType(2);
		order.setSalesSendDate(probationOrder.getProbationSendDate());
		order.setSalesAdminRemark(probationOrder.getProbationAdminRemark());
		order.setSalesColorType(probationOrder.getProbationColorType());
		//封装订单项数据
		List<OrderItem> itemList = new ArrayList<OrderItem>();
		OrderItem orderItem = new OrderItem();
		orderItem.setItemSalesOrderid(order.getSalesOrderid());
		orderItem.setItemProductId(probationOrder.getProbationProductId());
		orderItem.setItemProductType(probationOrder.getProbationProductType());
		orderItem.setItemNum(1);
		orderItem.setItemTotal(probationOrder.getProductDiscount());
		orderItem.setItemProductName(probationOrder.getProductName());
		orderItem.setItemProductDiscount(probationOrder.getProductDiscount());
		orderItem.setItemProductPrice(probationOrder.getProductPrice());
		orderItem.setItemProductPic(probationOrder.getProductIntroduceImg());
		orderItem.setItemColorId(probationOrder.getProbationColorId());
		itemList.add(orderItem);
		//添加到销售订单表中和订单项表中
		int orderNum = probationShopOrderMapper.insertSalesOrder(order);
		int itemNum = orderMapper.insertOrderItemList(itemList);
		if (orderNum > 0 && itemNum > 0) {
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public Integer selectProbationOrderCountByTel(String probationTel) {
		// TODO Auto-generated method stub
		return probationShopOrderMapper.selectProbationOrderCountByTel(probationTel);
	}

	/**
	 * 查询未拉取的订单
	 * @return
	 */
	public List<Order> selectProbationShopOrderList(){
		return (List<Order>)probationShopOrderMapper.selectProbationShopOrderList();
	}

	public void updateProbationShopOrder(Order order){
		probationShopOrderMapper.updateProbationShopOrder(order);
	}

}
