package com.huiju.estateagents.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.activity.entity.Activity;
import com.huiju.estateagents.activity.mapper.ActivityMapper;
import com.huiju.estateagents.activity.service.ActivityServiceI;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.HelpInitiateRecord;
import com.huiju.estateagents.entity.HelpRecord;
import com.huiju.estateagents.entity.TaActivityDynamicEnlist;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.entity.TaShareRecord;
import com.huiju.estateagents.mapper.HelpInitiateRecordMapper;
import com.huiju.estateagents.mapper.HelpRecordMapper;
import com.huiju.estateagents.mapper.TaActivityDynamicEnlistMapper;
import com.huiju.estateagents.mapper.TaShareRecordMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author FXF
 * @date 2019-10-21
 */
@Service
public class ActivityServiceImpl implements ActivityServiceI {
	
	@Autowired
	private ActivityMapper activityMapper;
	
	@Autowired
	private TaActivityDynamicEnlistMapper taActivityDynamicEnlistMapper;
	
	@Autowired
	private TaShareRecordMapper taShareRecordMapper;
	
	@Autowired
	private HelpRecordMapper helpRecordMapper;
	
	@Autowired
	private HelpInitiateRecordMapper helpInitiateRecordMapper;
	
	/**
	 * 获取三个活动
	 *
	 * @param pageNumber
	 * @param pageSize
	 * @param orgId
	 * @param person
	 * @return
	 */
	@Override
	public ResponseBean getActivityList(Integer pageNumber, Integer pageSize, Integer orgId, TaPerson person, Integer cityId) {
		//查询是否报名了普通活动
		QueryWrapper<TaActivityDynamicEnlist> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("org_id",orgId);
		queryWrapper.eq("person_id",person.getPersonId());
		List<TaActivityDynamicEnlist> taActivityDynamicEnlists = taActivityDynamicEnlistMapper.selectList(queryWrapper);
		//查询是否报名了拼团活动
		QueryWrapper<TaShareRecord> taShareRecordQueryWrapper = new QueryWrapper<>();
		taShareRecordQueryWrapper.eq("org_id",orgId);
		taShareRecordQueryWrapper.eq("person_id",person.getPersonId());
		List<TaShareRecord> taShareRecords = taShareRecordMapper.selectList(taShareRecordQueryWrapper);
		//判断是否发起了助力活动
		QueryWrapper<HelpInitiateRecord> helpInitiateRecordListQueryWrapper = new QueryWrapper<>();
		helpInitiateRecordListQueryWrapper.eq("org_id",orgId);
		helpInitiateRecordListQueryWrapper.eq("person_id",person.getPersonId());
		List<HelpInitiateRecord> HelpInitiateRecords = helpInitiateRecordMapper.selectList(helpInitiateRecordListQueryWrapper);
		
		//分页获取全部数据
		Page pg = new Page(pageNumber,pageSize);
		List<Activity> list = activityMapper.getActivityList(pg,orgId, CommConstant.STATUS_NORMAL,cityId);
		list.forEach(e -> {
			if (e.getType().equals("dynamic")){
				taActivityDynamicEnlists.forEach(dynamic -> {
					if (e.getId().equals(dynamic.getDynamicId())){
						e.setActivityStatus(CommConstant.ACTIVITY_STATUS_PARTNER);
					}
				});
				e.setSuccessNum(null);
			}else if (e.getType().equals("group")){
				taShareRecords.forEach(share -> {
					if (e.getId().equals(String.valueOf(share.getGroupActivityId()))){
						e.setActivityStatus(CommConstant.ACTIVITY_STATUS_PARTNER);
					}
				});
				//查看此活动已经完成的数量
				QueryWrapper<TaShareRecord> shareRecordQueryWrapper = new QueryWrapper<>();
				shareRecordQueryWrapper.eq("org_id",orgId);
				shareRecordQueryWrapper.eq("group_activity_id",e.getId());
				shareRecordQueryWrapper.eq("status",CommConstant.STATUS_UNACCALIMED);
				List<TaShareRecord> taShareRecordsList = taShareRecordMapper.selectList(shareRecordQueryWrapper);
				e.setSuccessNum(taShareRecordsList.size());
			}else if (e.getType().equals("help")){
				HelpInitiateRecords.forEach(help -> {
					if (e.getId().equals(help.getHelpActivityId())){
						e.setActivityStatus(CommConstant.ACTIVITY_STATUS_PARTNER);
					}
				});
				//查看此活动已经完成的数量
				QueryWrapper<HelpInitiateRecord> helpInitiateRecordQueryWrapper = new QueryWrapper<>();
				helpInitiateRecordQueryWrapper.eq("org_id",orgId);
				helpInitiateRecordQueryWrapper.eq("help_activity_id",e.getId());
				helpInitiateRecordQueryWrapper.eq("status",CommConstant.STATUS_UNACCALIMED);
				List<HelpInitiateRecord> helpInitiateRecordList = helpInitiateRecordMapper.selectList(helpInitiateRecordQueryWrapper);
				e.setSuccessNum(helpInitiateRecordList.size());
			}
		});
		pg.setRecords(list);
		return ResponseBean.success(pg);
	}
	
	/**
	 * 获取客户参加的活动信息
	 *
	 * @param pageNumber
	 * @param pageSize
	 * @param orgId
	 * @param person
	 * @return
	 */
	@Override
	public ResponseBean getCustomerActivityList(Integer pageNumber, Integer pageSize, Integer orgId, TaPerson person) {
		//分页获取全部数据
		Page pg = new Page(pageNumber,pageSize);
		List<Map<String, Object>> customerActivityList = activityMapper.getCustomerActivityList(pg, orgId, person.getPersonId());
		pg.setRecords(customerActivityList);
		return ResponseBean.success(pg);
	}
}
