package com.dev.swms.server.service.rule.pojo;

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

import com.dev.swms.client.ui.page.CustomEntity;
import com.dev.swms.client.ui.page.DisplayEntity;
import com.dev.swms.server.model.base.BillType;
import com.dev.swms.server.model.base.Item;
import com.dev.swms.server.model.base.Location;
import com.dev.swms.server.model.base.Structure;
import com.dev.swms.server.model.base.Position;
import com.dev.swms.server.model.rules.PickItemRule;
import com.dev.swms.server.service.rule.PickItemRuleManager;
import com.dev.swms.server.tools.format.Format;
import com.dev.swms.server.web.filter.WarehouseHolder;
import com.dev.energy.client.utils.StringUtils;
import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.service.pojo.DefaultBaseManager;
import com.dev.energy.server.util.LocalizedMessage;
import com.dev.energy.server.web.security.UserHolder;

@SuppressWarnings("unchecked")
public class DefaultPickItemRuleManager extends DefaultBaseManager implements
		PickItemRuleManager {

	public void storePickItemRule(PickItemRule pickItemRule) {
		//上架规则的库位定位分类和默认上架库位必须填写一个或两个都填写
		if((pickItemRule.getPosition()==null || pickItemRule.getPosition().getId()==0)
				&& ((pickItemRule.getPickLocation()==null) || pickItemRule.getPickLocation().getId()==0)){
			throw new BusinessException("PickItemRule.no.StowageAndDefaultLocation");
		}
		//当定位分类和库位均指定时，只存放具体库位
		if((pickItemRule.getPosition()!=null && pickItemRule.getPosition().getId()!=0)
				&& ((pickItemRule.getPickLocation()!=null) && pickItemRule.getPickLocation().getId()!=0)){
			pickItemRule.setPosition(null);
		}
		if (pickItemRule.getItemBigType()==null || pickItemRule.getItemBigType().equals("")) {
			pickItemRule.setItemBigType(null);
		}
		if (pickItemRule.getItemMiddleType()==null || pickItemRule.getItemMiddleType().equals("")) {
			pickItemRule.setItemMiddleType(null);
		}
		if (pickItemRule.getItemLittleType()==null || pickItemRule.getItemLittleType().equals("")) {
			pickItemRule.setItemLittleType(null);
		}
		if (pickItemRule.getItemPickType()==null || pickItemRule.getItemPickType().equals("")) {
			pickItemRule.setItemPickType(null);
		}
		if (pickItemRule.getAbcType()==null || pickItemRule.getAbcType().equals("")) {
			pickItemRule.setAbcType("");
		}
		
		pickItemRule.setWarehouse(WarehouseHolder.getWarehouse());
		if(pickItemRule.isNew()){
			this.store(pickItemRule);
		}
		else{
			this.commonDao.store(mdfPickItemRuleForUpdate(pickItemRule));
		}
	}
	/**
	 * 用于commDao的保存
	 * @param pickItemRule
	 */
	private PickItemRule mdfPickItemRuleForUpdate(PickItemRule pickItemRule){
		int sortIndex = pickItemRule.getSortIndex(); //优先级
		
		Structure company = null;
		if(pickItemRule.getCompany()!=null && pickItemRule.getCompany().getId()!=0)
			company = this.commonDao.load(Structure.class,pickItemRule.getCompany().getId());//货主
		
		BillType billType = null;
		if(pickItemRule.getBillType()!=null && pickItemRule.getBillType().getId()!=0)
			billType = this.commonDao.load(BillType.class,pickItemRule.getBillType().getId());//订单类型
		
		String itemPutawayType = pickItemRule.getItemPickType();
		
		Item item = null;
		if(pickItemRule.getItem()!=null && pickItemRule.getItem().getId()!=0)
			item = this.commonDao.load(Item.class,pickItemRule.getItem().getId());//货品
		
		int packageLineNo = pickItemRule.getPackageLineNo();//包装
		
		Position position = null;
		if(pickItemRule.getPosition()!=null && pickItemRule.getPosition().getId()!=0){
			position = this.commonDao.load(Position.class,pickItemRule.getPosition().getId());
		}
		
		Location pickLocation = null;
		if(pickItemRule.getPickLocation()!=null && pickItemRule.getPickLocation().getId()!=0){
			pickLocation = this.commonDao.load(Location.class,pickItemRule.getPickLocation().getId());
		}
		
		double minQty = pickItemRule.getMinQty();
		double maxQty = pickItemRule.getMaxQty();
		String abcType = pickItemRule.getAbcType()==null?"":pickItemRule.getAbcType();
		String pickMethod = pickItemRule.getPickMethod();
		String itemBigType = pickItemRule.getItemBigType();
		String itemMiddleType = pickItemRule.getItemMiddleType();
		String itemLittleType = pickItemRule.getItemLittleType();
		
		pickItemRule = this.commonDao.load(PickItemRule.class,pickItemRule.getId());
		
		pickItemRule.setSortIndex(sortIndex);
		pickItemRule.setCompany(company);
		pickItemRule.setBillType(billType);
		pickItemRule.setItemPickType(itemPutawayType);
		pickItemRule.setItemBigType(itemBigType);
		pickItemRule.setItemMiddleType(itemMiddleType);
		pickItemRule.setItemLittleType(itemLittleType);
		pickItemRule.setItem(item);
		pickItemRule.setPackageLineNo(packageLineNo);
		pickItemRule.setPosition(position);
		pickItemRule.setPickLocation(pickLocation);
		pickItemRule.setMinQty(minQty);
		pickItemRule.setMaxQty(maxQty);
		pickItemRule.setAbcType(abcType);
		pickItemRule.setPickMethod(pickMethod);
		return pickItemRule;
	}
	public void deletePickItemRule(List<Long> ids) {
		for(Long id:ids){
			this.delete(this.commonDao.load(PickItemRule.class,id));
		}
	}
	/**
	 * 初始化树的时候查当前仓库下策略的所有客户信息
	 */
	public Map getPutRackRuleByWareHouseId(Map param) {
		Map result = new HashMap();
		List<CustomEntity> results = new ArrayList<CustomEntity>();
		Long id = WarehouseHolder.getWarehouse().getId();
		List<Structure> orgs = this.commonDao.findByNamedQuery("getPickItemRuleByWareHouseId",
				new String[]{"warehouseId"},new Object[]{id});
		for(Structure org:orgs){
			CustomEntity en = new CustomEntity();
			if(org!=null){
				en.setId(org.getId());
				en.setName(Format.format("<font style='color:black;font:normal 12px tahoma,arial,helvetica;'>{0}</font> "
						,new String[]{org.getName()+"策略"}));
				en.setCompanyId(org.getId());
				en.setType(1);
				en.setRandomNum(Math.random());
				
				results.add(en);
			}else{
				en.setId(null);
				en.setName(Format.format("<font style='color:black;font:normal 12px tahoma,arial,helvetica;'>{0}</font> "
						,new String[]{"默认客户策略"}));
				en.setCompanyId(null);
				en.setType(1);
				en.setRandomNum(Math.random());
			}
		}
		result.put("result",results);
		return result;
	}
	/**
	 * 展开客户树的时候根据仓库和客户查订单类型信息
	 */
	public Map getBillTypeByCompanyId(Map param) {
		Long companyId = (Long)param.get("id");
		Map result = new HashMap();
		List<CustomEntity> results = new ArrayList<CustomEntity>();
		Long id = WarehouseHolder.getWarehouse().getId();
		List<BillType> bts = new ArrayList<BillType>();
		if(companyId!=null){
			bts = this.commonDao.findByNamedQuery("getPickItemRuleByCompanyId",
					new String[]{"warehouseId","companyId"},new Object[]{id,companyId});
		}else{
			bts = this.commonDao.findByNamedQuery("getPickItemRuleByCompanyIdNull",
					new String[]{"warehouseId"},new Object[]{id});
		}
		CustomEntity tempen = new CustomEntity();//默认订单类型显示在后面	
		for(BillType bt:bts){
			CustomEntity en = new CustomEntity();
			if(bt!=null){
				en.setId(bt.getId());

//				en.setName(bt.getName()+"策略");
				en.setName(Format.format("<font style='color:black;font:normal 12px tahoma,arial,helvetica;'>{0}</font> "
						,new String[]{bt.getName()+"策略"}));
				en.setCompanyId(companyId);
				en.setBillTypeId(bt.getId());
				en.setType(2);
				en.setRandomNum(Math.random());
				
				results.add(en);
			}else{
				en.setId(null);
				en.setName(Format.format("<font style='color:black;font:normal 12px tahoma,arial,helvetica;'>{0}</font> "
						,new String[]{"默认订单类型策略"}));
				en.setCompanyId(companyId);
				en.setBillTypeId(null);
				en.setType(2);
				en.setRandomNum(Math.random());
				
				tempen = en;
			}
		}
		if(tempen != null && tempen.getName() != null)
			results.add(tempen);
		result.put("billTypes",results);
		return result;
	}
	/**
	 * 点击树节点的时候现实节点下所有的策略
	 */
	public Map getPutRackRules(Map param) {
		Map result = new HashMap();
		CustomEntity nodeObj = (CustomEntity)param.get("nodeObj");
		List<PickItemRule> rules = getRulesByObj(nodeObj);
		List<DisplayEntity> des = new ArrayList<DisplayEntity>();
		for(PickItemRule rule:rules){
			DisplayEntity de = new DisplayEntity();
			de.setId(rule.getId());
			de.setLeftDis(rule.toStringForDis(nodeObj));
			if(rule.getPickLocation()!=null)
				de.setDefaultLocation(rule.getPickLocation().getCode());
			if(rule.getPosition()!=null)
				de.setPosition(rule.getPosition().getName());
			if (!StringUtils.isEmpty(rule.getPickMethod())) {
				if(StringUtils.isEmpty(rule.getPickMethod())){
					de.setMothed("");
				}else{
					de.setMothed(LocalizedMessage.getLocalizedMessage("PickMethod."+rule.getPickMethod(),UserHolder.getReferenceModel(),UserHolder.getUser().getLocale().toString()));
				}
			}
			des.add(de);
		}
		result.put("result",des);
		return result;
	}
	
	private List<PickItemRule> getRulesByObj(CustomEntity nodeObj){
		Long id = WarehouseHolder.getWarehouse().getId();
		List<PickItemRule> rules = new ArrayList<PickItemRule>();
		if(nodeObj.getType()==1){//点击客户节点
			if(nodeObj.getId()==null){
				rules = this.commonDao.findByNamedQuery("getPickItemRuleForCompanyNull",
						new String[]{"warehouseId"},new Object[]{id});
			}else{
				rules = this.commonDao.findByNamedQuery("getPickItemRuleForCompany",
						new String[]{"warehouseId","companyId"},new Object[]{id,nodeObj.getId()});
			}
		}else{
			if(nodeObj.getId()==null){
				if(nodeObj.getCompanyId()==null){
					rules = this.commonDao.findByNamedQuery("getPickItemRuleForAllNull",
							new String[]{"warehouseId"},new Object[]{id});
				}else{
					rules = this.commonDao.findByNamedQuery("getPickItemRuleForBillTypeNull",
							new String[]{"warehouseId","companyId"},new Object[]{id,nodeObj.getCompanyId()});
				}
			}else{
				rules = this.commonDao.findByNamedQuery("getPickItemRuleForBillType",
						new String[]{"warehouseId","companyId","billTypeId"},
						new Object[]{id,nodeObj.getCompanyId(),nodeObj.getId()});
			}
		}
		return rules;
	}
	public void deletePickItemRule(Map params) {
		List<Long> ids = (List)params.get("ids");
		this.deletePickItemRule(ids);
	}

}
