package com.iamberry.wechat.service.giftCard;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jpush.api.utils.StringUtils;

import com.iamberry.wechat.core.entity.giftCard.AgentInfo;
import com.iamberry.wechat.core.entity.giftCard.AgentMachine;
import com.iamberry.wechat.core.entity.product.ProductColor;
import com.iamberry.wechat.face.giftCard.AgentMachineService;
import com.iamberry.wechat.service.mapper.AgentMachineMapper;
import com.iamberry.wechat.service.mapper.AgentMapper;
import com.iamberry.wechat.service.mapper.ProductColorMapper;

/**
 * 物流信息Service实现类
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	LJK
 * @date	2017年2月16日
 */
@Service
public class AgentMachineServiceImpl implements AgentMachineService {
	@Autowired
	private AgentMachineMapper agentMachineMapper;
	@Autowired
	private AgentMapper agentMapper;
	@Autowired
	private ProductColorMapper productColorMapper;
	
	@Override
	public Integer insertAgentMachine(AgentMachine agentMachine) {
		// TODO Auto-generated method stub
		return agentMachineMapper.insertAgentMachine(agentMachine);
	}

	@Override
	public AgentMachine selectAgentMachine(Integer agentMachineId) {
		// TODO Auto-generated method stub
		return agentMachineMapper.selectAgentMachine(agentMachineId);
	}

	@Override
	public Integer updateAgentMachine(AgentMachine agentMachine) {
		// TODO Auto-generated method stub
		return agentMachineMapper.updateAgentMachine(agentMachine);
	}

	@Override
	public List<AgentMachine> selectAllAgentMachine(AgentMachine agentMachine) {
		// TODO Auto-generated method stub
		return agentMachineMapper.selectAllAgentMachine(agentMachine);
	}

	@Override
	public Integer selectAllAgentMachineCount(AgentMachine agentMachine) {
		// TODO Auto-generated method stub
		return agentMachineMapper.selectAllAgentMachineCount(agentMachine);
	}

	@Override
	public String addAgentLogisticsMachine(AgentMachine agentMachine) {
		// TODO Auto-generated method stub
		//当前代理商记录id
		String str = "";
		if(agentMachine == null || 
				StringUtils.isEmpty(agentMachine.getAgentMachineOpenId()) || 
				agentMachine.getAgentMachineProductId() == null ||
				agentMachine.getAgentMachineColorId() == null){
			return str;
		}
		AgentInfo agent = agentMapper.selectAgentByOpenId(agentMachine.getAgentMachineOpenId());		
		if(agent == null){	return str;	}
		ProductColor productColor = productColorMapper.selectProductColorById(agentMachine.getAgentMachineColorId());
		
		agentMachine.setAgentMachineColorName(productColor.getColorName());
		agentMachine.setAgentMachineProductName(productColor.getColorProductName());
		agentMachine.setAgentMachine69code(productColor.getColor69Code());
		agentMachine.setAgentMachineStatus(0);
		agentMachine.setAgentMachineType(1);
		//插入当前代理商物流信息
		Integer i = agentMachineMapper.insertAgentMachine(agentMachine);
		if(i != null && i > 0){
			str = agentMachine.getAgentMachineId() + "";
		}
		return str;
	}

	@Override
	public List<AgentMachine> selectAllAgentMachineById(
			AgentMachine agentMachine) {
		// TODO Auto-generated method stub
		return agentMachineMapper.selectAllAgentMachineById(agentMachine);
	}

	@Override
	public Integer selectAllAgentMachineByIdCount(AgentMachine agentMachine) {
		// TODO Auto-generated method stub
		return agentMachineMapper.selectAllAgentMachineByIdCount(agentMachine);
	}

	@Override
	public Integer selectAgentMachineByOpenId(AgentMachine agentMachine) {
		return agentMachineMapper.selectAgentMachineByOpenId(agentMachine);
	}

}
