package com.goafanti.order.service.impl;

import com.goafanti.admin.service.DepartmentService;
import com.goafanti.common.dao.UserMidMapper;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.order.bo.*;
import com.goafanti.order.service.UserStaticticsService;
import groovy.util.logging.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Log4j
public class UserStatisticsServiceImpl extends BaseMybatisDao<UserMidMapper> implements UserStaticticsService {

	@Autowired
	private DepartmentService departmentService;

	@Override
	public Pagination<OutSignSummary> signSummary(InputSignSummary in) {
		Map<String, Object> map = new HashMap<>();
		addSignSummaryParam(in, map);
		return (Pagination<OutSignSummary>) findPage("selectSignSummaryList","selectSignSummaryCount",map,in.getPageNo(),in.getPageSize());
	}

	private void addSignSummaryParam(InputSignSummary in, Map<String, Object> map) {
		if (in.getUserName()!=null) map.put("userName", in.getUserName());
		if (StringUtils.isNotBlank(in.getAid())) map.put("aid", in.getAid());
		if (in.getFollowAid()!=null) map.put("followAid", in.getFollowAid());
		if (in.getMember()!=null) map.put("member", in.getMember());
		if (in.getChargeback()!=null) map.put("chargeback", in.getChargeback());
		if (in.getBigCustomer()!=null) map.put("bigCustomer", in.getBigCustomer());
		if (in.getLastFollowType()!=null) map.put("lastFollowType", in.getLastFollowType());
		if (in.getDepId()!=null) map.put("depId", in.getDepId());
		if (in.getLastFollowTimeStart()!=null) map.put("lastFollowTimeStart", in.getLastFollowTimeStart());
		if (in.getLastFollowTimeEnd()!=null) map.put("lastFollowTimeEnd", in.getLastFollowTimeEnd()+" 23:59:59");
		if (in.getFirstSigningTimeStart()!=null) map.put("firstSigningTimeStart", in.getFirstSigningTimeStart());
		if (in.getFirstSigningTimeEnd()!=null) map.put("firstSigningTimeEnd", in.getFirstSigningTimeEnd()+" 23:59:59");
		if (in.getLastSigningTimeStart()!=null) map.put("lastSigningTimeStart", in.getLastSigningTimeStart());
		if (in.getLastSigningTimeEnd()!=null) map.put("lastSigningTimeEnd", in.getLastSigningTimeEnd()+" 23:59:59");
		if (in.getSignNumber()!=null) map.put("signNumber", in.getSignNumber());
		if (in.getLastSalesType()!=null) map.put("lastSalesType", in.getLastSalesType());
	}

	@Override
	public List<OutSignSummary> signSummaryList(InputSignSummary in) {
		Map<String, Object> map = new HashMap<>();
		addSignSummaryParam(in, map);
		return (List<OutSignSummary>) findList("selectSignSummaryList",map,1,9999);
	}

	@Override
	public Pagination<OutSignStatistics> signStatistics(InputSignStatistics in) {
		Map<String, Object> map = new HashMap<>();
		addsignStatisticsParam(in, map);
		return (Pagination<OutSignStatistics>) findPage("selectsignStatisticsList","selectsignStatisticsCount",map,in.getPageNo(),in.getPageSize());
	}
	@Override
	public List<OutSignStatistics> signStatisticsGetList(InputSignStatistics in) {
		Map<String, Object> map = new HashMap<>();
		addsignStatisticsParam(in, map);
		return (List<OutSignStatistics>) findList("selectsignStatisticsList",map,1,9999);
	}


	private void addsignStatisticsParam(InputSignStatistics in, Map<String, Object> map) {
		if (in.getDeps()!=null){
			map.put("deps",departmentService.parseArray(in.getDeps()));
		}
		if (StringUtils.isNotBlank(in.getAid())) map.put("aid", in.getAid());
		if (StringUtils.isNotBlank(in.getUid())) map.put("uid", in.getUid());
		if (in.getAmountType()!=null)map.put("amountType",in.getAmountType());
		if (in.getSignTimeStart()!=null)map.put("signTimeStart",in.getSignTimeStart());
		if (in.getSignTimeEnd()!=null)map.put("signTimeEnd",in.getSignTimeEnd()+" 23:59:59");
		if (in.getOrderType()!=null)map.put("orderType",in.getOrderType());
	}

	@Override
	public Pagination<TOrderNewBo> signStatisticsOrderList(InputSignStatistics in) {
		Map<String, Object> map = new HashMap<>();
		addsignStatisticsParam(in, map);
		return (Pagination<TOrderNewBo>) findPage("selectsignStatisticsOrderList","selectsignStatisticsOrderCount",map,in.getPageNo(),in.getPageSize());
	}
}
