package com.goafanti.order.service.impl;

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

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.common.dao.AdminMapper;
import com.goafanti.common.dao.TOrderMonthReportMapper;
import com.goafanti.common.model.Admin;
import com.goafanti.common.model.TOrderMonthReport;
import com.goafanti.common.utils.ExportExcelUtil;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.order.bo.OrderReportBo;
import com.goafanti.order.service.OrderReportService;

@Service
public class OrderReportServiceImpl extends BaseMybatisDao<TOrderMonthReportMapper> implements OrderReportService {
	
	@Autowired
	private TOrderMonthReportMapper	monthReportMapper;
	@Autowired
	private AdminMapper adminMapper;
	
	@Override
	public int insertOrderStatisticsByMonthTask() {
		return monthReportMapper.insertLastMonthOrderData();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<OrderReportBo> getOrderReportData(
			OrderReportBo orderReportBo, Integer pageNo, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("r", orderReportBo);
		Pagination<OrderReportBo> data = (Pagination<OrderReportBo>)findPage("getOrderReportDataList", "getOrderReportDataCount", params, pageNo, pageSize);
		return data;
	}

	@Override
	public XSSFWorkbook exportMonthData(OrderReportBo orderReportBo, String date) {
		List<OrderReportBo> list = monthReportMapper.exportMonthData(orderReportBo);
		if(null == list || list.size() == 0){
			return null;
		}
		return ExportExcelUtil.exportOrderInfo(list, date);
	}

	@Override
	public void checkOrderReport(Set<OrderReportBo> rbos, Set<Integer> existRows, Set<Integer> fiterRows) {
		OrderReportBo[] bos = (OrderReportBo[]) rbos.toArray(new OrderReportBo[] {});
		for (int i = 0; i < bos.length; i++) {
			OrderReportBo or = bos[i];
			// 首先通过手机号码获得adminid
			Admin admin = adminMapper.selectByMobile(or.getMobile());
			if(null == admin){
				fiterRows.add(or.getRowNumber());
			}else{
				// 检查名字和电话是否匹配
				if(admin.getName().equals(or.getSaleName())){
					// 检查数据是否存在
					or.setSalemansId(admin.getId());
					int count = monthReportMapper.checkData(or);
					if(count>0){
						existRows.add(or.getRowNumber());
					}
				}else{
					fiterRows.add(or.getRowNumber());
				}
				
			}
		}
	}

	@Override
	public int saveUploadData(Set<OrderReportBo> bos) {
		OrderReportBo[] datas = (OrderReportBo[]) bos.toArray(new OrderReportBo[] {});
		int count = 0;
		for (int i = 0; i < datas.length; i++) {
			monthReportMapper.insertSelective(datas[i]);
		}
		return count;
	}

	@Override
	public int updateYearReportData() {
		
		return monthReportMapper.updateYearReportData(null);
	}

	@Override
	public int saveLastYearReportData() {
		return monthReportMapper.insertLastYearOrderData();
	}
	
	@Override
	public int saveYearReportData() {
		return monthReportMapper.updateByYear();
	}

	@Override
	public int updateMonthDataById(TOrderMonthReport report) {
		return monthReportMapper.updateByPrimaryKeySelective(report);
	}
	
}
