package com.ym.mec.biz.service.impl;


import com.ym.mec.biz.dal.dao.StudentGoodsSellDao;
import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dto.StudentGoodsSellDto;
import com.ym.mec.biz.dal.dto.StudentPaymentOrderDto;
import com.ym.mec.biz.dal.entity.StudentGoodsSell;
import com.ym.mec.biz.dal.page.GoodsSellQueryInfo;
import com.ym.mec.biz.service.StudentGoodsSellService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class StudentGoodsSellServiceImpl extends BaseServiceImpl<Integer, StudentGoodsSell> implements StudentGoodsSellService {
	
	@Autowired
	private StudentGoodsSellDao studentGoodsSellDao;
	@Autowired
	private SysConfigDao sysConfigDao;

	@Override
	public BaseDAO<Integer, StudentGoodsSell> getDAO() {
		return studentGoodsSellDao;
	}

	@Override
	public PageInfo<StudentGoodsSellDto> queryStudentGoodsOrders(GoodsSellQueryInfo queryInfo) {
		//根据订单状态和教务老师获取订单列表
		PageInfo<StudentGoodsSellDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);
		List<StudentGoodsSellDto> dataList = null;
		int count = studentGoodsSellDao.countStudentGoodsOrders(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = studentGoodsSellDao.queryStudentGoodsOrders(params);
			Integer autoAffirmReceiveTime = Integer.parseInt(sysConfigDao.findConfigValue("auto_affirm_receive_time"));
			dataList.forEach(e->{
				e.setAutoAffirmReceiveTime(autoAffirmReceiveTime);
			});
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void affirmReceive(String orderNo) {
		String str = "MANUAL_RECEIVE";
		//如果订单编号为空，那么自动确认
		if(StringUtils.isEmpty(orderNo)){
			String autoAffirmReceiveTime = sysConfigDao.findConfigValue("auto_affirm_receive_time");
			//获取到期的订单编号
			orderNo = studentGoodsSellDao.queryNoAffirmOrderNo(autoAffirmReceiveTime);
			str = "AUTO_RECEIVE";
		}
		if(StringUtils.isNotEmpty(orderNo)){
			studentGoodsSellDao.autoAffirmReceive(orderNo,str);
		}
	}

	@Override
	public PageInfo<StudentPaymentOrderDto> queryStudentPaymentOrders(GoodsSellQueryInfo queryInfo) {
		PageInfo<StudentPaymentOrderDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);
		List<StudentPaymentOrderDto> dataList = null;
		int count = studentGoodsSellDao.countStudentPaymentOrders(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = studentGoodsSellDao.queryStudentPaymentOrders(params);
			Integer autoAffirmReceiveTime = Integer.parseInt(sysConfigDao.findConfigValue("auto_affirm_receive_time"));
			dataList.forEach(e->{
				e.setAutoAffirmReceiveTime(autoAffirmReceiveTime);
			});
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

}