package com.iamberry.wechat.service.agentInfo;

import com.iamberry.wechat.core.entity.OrderUtil;
import com.iamberry.wechat.core.entity.agentInfo.AgentConfig;
import com.iamberry.wechat.core.entity.agentInfo.AgentOrder;
import com.iamberry.wechat.core.entity.agentInfo.AgentOrderItem;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.core.entity.product.ProductColor;
import com.iamberry.wechat.face.agentInfo.AgentPurchaseOrderService;
import com.iamberry.wechat.service.mapper.AgentInfoMapper;
import com.iamberry.wechat.service.mapper.AgentPurchaseOrderMapper;
import com.iamberry.wechat.service.mapper.ProductInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuzhiwei on 2017/10/11.
 */
@Service
public class AgentPurchaseOrderServiceImpl implements AgentPurchaseOrderService {

    @Autowired
    private AgentPurchaseOrderMapper agentPurchaseOrderMapper;

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private AgentInfoMapper agentInfoMapper;

    @Override
    @Transactional
    public Map<String,Object> addAgentOrder(AgentOrder agentOrder, List<AgentOrderItem> agentOrderItemList) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("status","400");

        agentOrder.setAgentStatus(OrderUtil.ORDERSTAUTS_TOBEPAID);      //设置状态为：未支付
        int amount  = 0;    //订单总金额

        for(AgentOrderItem agentOrderItem : agentOrderItemList){
            ProductColor productColor = new ProductColor();
            productColor.setColorId(agentOrderItem.getColorId());
            productColor.setColorProductId(agentOrderItem.getProductId());
            productColor = productInfoMapper.getProductColor(productColor);
            Product product = productInfoMapper.getProductByProductId(agentOrderItem.getProductId(),agentOrderItem.getColorId());
            if(productColor == null || product == null){
                map.put("msg","查询产品出错!");
                return map;
            }
            agentOrderItem.setProductId(product.getProductId());            //产品id
            agentOrderItem.setItemProductName(product.getProductName());    //产品名称
            agentOrderItem.setColorId(productColor.getColorId());           //颜色id
            agentOrderItem.setItemColorName(productColor.getColorName());   //颜色名称
            agentOrderItem.setProductType(product.getProductType().getTypeId());    //产品类型id
            agentOrderItem.setProductRentType(product.getProductRentType());        //销售类型
            agentOrderItem.setItemProductPrice(productColor.getColorPrice());       //商品原价
            agentOrderItem.setItemProductPic(product.getProductIntroduceImg());     //介绍图

            // -------- 计算金额 ---------
            Integer discount = 0;   //优惠价
            AgentConfig agentConfig = new AgentConfig();
            agentConfig.setColorId(productColor.getColorId());
            agentConfig.setAgentId(agentOrder.getAgentId());
            agentConfig = agentInfoMapper.getAgentConfig(agentConfig);
            if(agentConfig != null){
                discount = agentConfig.getConfigAmount();
            }else{
                discount = productColor.getColorDiscount();
            }
            agentOrderItem.setItemProductDiscount(discount); //商品优惠价

            Integer itemSummary = 0;
            if(agentOrderItem.getItemNum() == null || agentOrderItem.getItemNum() < 1){
                map.put("msg","产品数量不能小于1!");
                return map;
            }
            itemSummary = agentOrderItem.getItemNum() * discount;   //计算订单项小结
            amount += itemSummary;  //计算订单总金额
        }

        agentPurchaseOrderMapper.addAgentOrder(agentOrder);


        return null;
    }
}
