package com.palmnest.service.washcar.manager.impl;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.washcar.CustomerDao;
import com.palmnest.model.washcar.Customer;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.washcar.manager.CustomerManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.*;


/**
 * 用户manager实现类
 *
 * @author longmeizhen
 */
@Slf4j
@SuppressWarnings("rawtypes")
@Service("customerManager")
public class CustomerManagerImpl extends GenericManagerImpl<Customer, Long> implements CustomerManager {


	private CustomerDao customerDao;

	@Autowired
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
		this.dao = customerDao;
	}

	@Override
	public Map<String, Object> importUserInfo(InputStream ins, String userId) throws Exception {
		Map<String, Object> retMap   = new HashMap<String, Object>();
		List<Object>        userList = new ArrayList<Object>();
		int                 bugCount = 0;
		StringBuffer        bugStr   = new StringBuffer("");
		Sheet               sheet    = null;
		try {
			Workbook workbook = WorkbookFactory.create(ins);
			sheet = workbook.getSheet("VirtualArchitecture");
		} catch (Exception e) {
			bugStr.append("模板格式有问题，请下载最新的模板");
		}

		try {
			if (sheet == null) {
				bugStr.append("模板格式有问题，请下载最新的模板");
			} else {
				int rows = sheet.getLastRowNum();
				//如果大于2000行数据
				if (rows > 20000) {
					bugStr.append("数据量太大，不能大于2w行");
				} else if (rows < 1) {
					bugStr.append("数据为空，请录入账单信息");
				} else {
					for (int i = 1; i <= rows; i++) {
						if (sheet.getRow(i) == null) {
							continue;
						}
						//设置对应的值
					}
				}
			}
			retMap.put("bugCount", bugCount);
			retMap.put("bugStr", bugStr.toString());
			retMap.put("userList", userList);
			retMap.put("okCount", userList.size());
			return retMap;

		} catch (Exception e) {
			log.error(e.getMessage());
			retMap.put("bugCount", bugCount);
			retMap.put("bugStr", bugStr.toString());
			retMap.put("userList", userList);
			retMap.put("okCount", userList.size());
			e.printStackTrace();
			return retMap;
		}
	}

	@Transactional
	public String saveImportUser(List<Customer> list) throws Exception {
		String res = "";
		try {
			for (int i = 0; i < list.size(); i++) {
				Customer customer = list.get(i);
				customerDao.save(customer);
			}
			res = "SUCCESS";
		} catch (Exception e) {
			log.error("CustomerManagerImpl/saveImportUser:" + e.getMessage());
			e.printStackTrace();
			res = "CustomerManagerImpl/saveImportUser:" + e.getMessage();
			throw new Exception(e);
		}
		return res;
	}

	@Transactional
	public String saveCustomer(Customer customer, String userId) throws Exception {
		try {
			if (customer.getId() == null || customer.getId() == 0) {
				customer.setCreateTime(new Date());
				customer.setCreator(userId);
				customerDao.save(customer);
				return "save";
			} else {
				customer.setModifyTime(new Date());
				customer.setModifier(userId);
				customerDao.updateObj(customer);
				return "update";
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		}
	}

	@Override
	public String deleteCustomer(Long custId, String userId) throws Exception {
		return customerDao.deleteCustomer(custId, userId);
	}

	@Override
	public List<Customer> getAll() {

		return customerDao.getAll();
	}

	@Override
	public Customer get(Long id) {

		return customerDao.get(id);
	}

	@Override
	public Page getCustList(String orgIds, String mobile, String userName, String carNumber, String searchStr, int pageNo, int pageSize)
			throws Exception {

		return customerDao.getCustList(orgIds, mobile, userName, carNumber, searchStr, pageNo, pageSize);
	}

	@Override
	public Customer getCustByMobile(String orgId, String mobile) throws Exception {
		return customerDao.getCustByMobile(orgId, mobile);
	}

	@Override
	public void updateModel(Customer object) {
		customerDao.updateObj(object);
	}

	@Override
	public Customer getCustByOpenId(String orgId, String openId) throws Exception {
		return customerDao.getCustByOpenId(orgId, openId);
	}
}
