package org.cs.mgr.product.service.impl;

import java.util.Date;
import java.util.Map;

import org.cs.core.service.impl.BaseService;
import org.cs.mgr.admin.model.Admin;
import org.cs.mgr.product.dao.IMgmIntentOrderDao;
import org.cs.mgr.product.model.MgmIntentOrder;
import org.cs.mgr.product.service.IMgmIntentOrderService;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: IMgmProductFollowService
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:10
 */
@Service("mgmIntentOrderService")
public class MgmIntentOrderService extends BaseService<MgmIntentOrder> implements IMgmIntentOrderService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IMgmIntentOrderDao mgmIntentOrderDao;
	
	
	@Override
	@Transactional(readOnly=false)
	public JSONObject addMgmIntentOrder(Map<String, String> map){
		String customerName = map.get("customername");
		String customerPhone = map.get("customerphone");
		String productId = map.get("productid");
		if(StringUtil.isBlank(customerName)){
			return getJson(0, "", "客户姓名不能为空");
		}

		if(StringUtil.isBlank(customerPhone)){
			return getJson(0, "", "客户联系电话不能为空");
		}
		if(StringUtil.isBlank(productId)){
			return getJson(0, "", "产品信息不能为空");
		}
		MgmIntentOrder mgmIntentOrder = new MgmIntentOrder();
		mgmIntentOrder.setCreateDate(new Date());
		mgmIntentOrder.setCustomerName(customerName);
		mgmIntentOrder.setCustomerPhone(customerPhone);
		mgmIntentOrder.setIntentTime(new Date());
		mgmIntentOrder.setIsDelete(false);
		mgmIntentOrder.setJobNumber(map.get("jobnumber"));
		mgmIntentOrder.setProductIntent(map.get("productintent"));
		mgmIntentOrder.setModifyDate(new Date());
		mgmIntentOrder.setProductId(Integer.valueOf(productId));
		mgmIntentOrder.setUserId(Integer.valueOf(map.get("userid")));
		mgmIntentOrder = this.mgmIntentOrderDao.insert(mgmIntentOrder);
		
		return getJson(1, "", "意向购买提交成功");
	}
	@Override
	@Transactional(readOnly=false)
	public JSONObject addMgmIntentOrder(MgmIntentOrder mgmIntentOrder, Map<String, String> user){
		
		if(StringUtil.isBlank(mgmIntentOrder.getCustomerName())){
			return getJson(0, "", "客户姓名不能为空");
		}

		if(StringUtil.isBlank(mgmIntentOrder.getCustomerPhone())){
			return getJson(0, "", "客户联系电话不能为空");
		}
		if(StringUtil.isBlank(mgmIntentOrder.getProductIntent())){
			return getJson(0, "", "产品信息不能为空");
		}
		mgmIntentOrder.setCreateDate(new Date());
		mgmIntentOrder.setIntentTime(new Date());
		mgmIntentOrder.setIsDelete(false);
		mgmIntentOrder.setModifyDate(new Date());
		mgmIntentOrder.setUserId(Integer.valueOf(user.get("id").toString()));
		this.mgmIntentOrderDao.insert(mgmIntentOrder);
		return getJson(1, "", "意向购买提交成功");
	}

	//	客戶端調用訂單保存方法
	@Override
	@Transactional(readOnly=false)
	public JSONObject addMgmIntentOrderFromApp(Map<String,String> map, Map<String, Object> user){

		if(StringUtil.isBlank(map.get("customerName"))){
			return getJson(0, "", "客户姓名不能为空");
		}

		if(StringUtil.isBlank(map.get("customerPhone"))){
			return getJson(0, "", "客户联系电话不能为空");
		}
		if(StringUtil.isBlank(map.get("productIntent"))){
			return getJson(0, "", "产品信息不能为空");
		}
		MgmIntentOrder mgmIntentOrder = new MgmIntentOrder();
		mgmIntentOrder.setCustomerName(map.get("customerName"));
		mgmIntentOrder.setCustomerPhone(map.get("customerPhone"));
		mgmIntentOrder.setJobNumber(map.get("jobNumber"));
		mgmIntentOrder.setProductId(Integer.parseInt(map.get("productId")));
		mgmIntentOrder.setProductIntent(map.get("productIntent"));
		mgmIntentOrder.setCreateDate(new Date());
		mgmIntentOrder.setIntentTime(new Date());
		mgmIntentOrder.setIsDelete(false);
		mgmIntentOrder.setModifyDate(new Date());
		mgmIntentOrder.setUserId(Integer.valueOf(user.get("id").toString()));
		this.mgmIntentOrderDao.insert(mgmIntentOrder);
		return getJson(1, mgmIntentOrder.getId(), "意向购买提交成功");
	}

	@Override
	public Pager queryIntentPager(Pager pager, Map<String, String> map){
		return this.mgmIntentOrderDao.queryIntentPager(pager, map);
	}
	
	@Override
	@Transactional(readOnly=false)
	public void delIntentById(String ids){
		for(String id:ids.split(",")){
			if(StringUtil.isNotBlank(id)){
				MgmIntentOrder mio = this.mgmIntentOrderDao.findById(Integer.valueOf(id));
				mio.setIsDelete(false);
				this.mgmIntentOrderDao.update(mio);
			}
		}
	}
	@Override
	public Map<String, Object> queryIntent(Map<String, String> map){
	return this.mgmIntentOrderDao.queryIntent(map);	
	}
}
