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

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.model.EntityFactory;
import com.dev.energy.server.service.pojo.DefaultBaseManager;
import com.dev.energy.server.web.security.UserHolder;
import com.dev.swms.server.model.base.Address;
import com.dev.swms.server.model.base.BillType;
import com.dev.swms.server.model.base.City;
import com.dev.swms.server.model.base.Contact;
import com.dev.swms.server.model.base.Item;
import com.dev.swms.server.model.base.Line;
import com.dev.swms.server.model.base.Location;
import com.dev.swms.server.model.base.NewLotInfo;
import com.dev.swms.server.model.base.Store;
import com.dev.swms.server.model.base.Structure;
import com.dev.swms.server.service.base.StoreManager;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class DefaultStoreManager extends DefaultBaseManager implements StoreManager {

	// 添加门店
	@Override
	public void saveStore(Store s) {
		/*
		 * if(s.getCode().equals("")) { String
		 * code=PinYinConvert.ToFirstChar(s.getName()); s.setCode(code); }
		 */

		s.setBeDisabled(false);// 默认否
		s.setCreatDate(new Date());
		s.setCreatPeople(UserHolder.getUser().getName());
		commonDao.store(s);

	}

	// 删除门店
	@Override
	public void deleteStore(List<Long> ids) {
		for (Long id : ids) {
			Store s = this.load(Store.class, id);
			this.delete(s);
		}

	}

	// 失效
	@Override
	public void storeInvalid(List<Long> ids) {
		for (Long id : ids) {
			Store s = this.load(Store.class, id);
			s.setBeDisabled(true);
		}

	}

	// 恢复
	@Override
	public void storerestore(List<Long> ids) {
		for (Long id : ids) {
			Store s = this.load(Store.class, id);
			s.setBeDisabled(false);
		}

	}

	@Override
	public void saveLines(Long sequenceNo, Long sid, Long lid) {
		Store s = this.load(Store.class, sid);// 门店
		s.setSequenceNo(sequenceNo.intValue());
		Line l = this.load(Line.class, lid);
		s.setLine(l);
		commonDao.store(s);

	}

	@Override
	public void imporStore(File file) {
		try {
			Workbook workbook = Workbook.getWorkbook(file);
			Sheet sheet = workbook.getSheet(0);// 得到excel第一页的内容
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
			for (int i = 1; i < sheet.getRows(); i++) {
				//货主代码 门店号 门店名称 地址 联系人 电话 传真 所属城市 所属线路 顺序号
				
				String hzdm = sheet.getCell(0, i).getContents().trim();
				String mdh = sheet.getCell(1, i).getContents().trim();
				String mdmc = sheet.getCell(2, i).getContents().trim();
				String dz = sheet.getCell(3, i).getContents().trim();
				String lxr = sheet.getCell(4, i).getContents().trim();
				String dh = sheet.getCell(5, i).getContents().trim();
				String fax = sheet.getCell(6, i).getContents().trim();
				String cs = sheet.getCell(7, i).getContents().trim();
				String xl = sheet.getCell(8, i).getContents().trim();
				String sxh = sheet.getCell(9, i).getContents().trim();
				Store store=null;
				Line line=null;
				if(null==hzdm || hzdm.equals("")){
		    		return;
		    	}
				if(null==mdh || mdh.equals("")){
		    		throw new BusinessException(i+1+"行,门店代码不能为空!");
		    	}
				if(null==mdmc || mdmc.equals("")){
		    		throw new BusinessException(i+1+"行,门店名称不能为空!");
		    	}
				//门店  
				List<Store> stores=commonDao.findByQuery(" from Store s where s.code=:code ",
						new String[] { "code"},new Object[] {mdh});
				if(stores.size()>0) {
					store=stores.get(0);
				}else {
					store=EntityFactory.getEntity(Store.class);
				}
				//货主
				List<Structure> org=commonDao.findByQuery(" from Structure s where s.code=:code and s.beCompany=1",
						new String[] { "code"},new Object[] {hzdm});
				if(org.size()>0) {
					Structure company = org.get(0);
					store.setCompany(company);
				}else {
					throw new BusinessException("没有该货主，需要维护！");
				}
				//城市
				List<City> citys=commonDao.findByQuery(" from City c where c.name=:name ",
						new String[] { "name"},new Object[] {cs});
				if(citys.size()>0) {
					City city=citys.get(0);
					store.setCity(city);
				}
				//线路
				 line=EntityFactory.getEntity(Line.class);
				List<Line> lines =commonDao.findByQuery(" from Line l where l.lineName=:name ",
						new String[] { "name"},new Object[] {xl});
				if(lines.size()>0) {
					 line = lines.get(0);
					store.setLine(line);
//					 store.setLineSunday(line);
				}else {
					throw new BusinessException("没有该线路，需要维护！");
				}
				store.setCode(mdh);
				store.setName(mdmc);
				Address address = new Address();
				address.setAddress(dz);
				store.setAddress(address);
				/*if(store.getAddress()!=null) {
					store.getAddress().setAddress(dz);
				}else if(store.getContact()!=null) {
					store.getContact().setPerson(lxr);
					store.getContact().setTelephone(dh);
					store.getContact().setFax(fax);
				}*/
				Contact contact = new Contact();
				contact.setPerson(lxr);
				contact.setTelephone(dh);
				contact.setFax(fax);
				store.setContact(contact);
				store.setBeDisabled(false);// 默认否
				store.setCreatDate(new Date());
				store.setCreatPeople(UserHolder.getUser().getName());
				store.setSequenceNo(Integer.parseInt(sxh));
				commonDao.store(store);
				System.out.println(i);
			}
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
