package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaPersonBuilding;
import com.huiju.estateagents.entity.TaShareActivity;
import com.huiju.estateagents.entity.TaShareRecord;
import com.huiju.estateagents.mapper.TaShareActivityMapper;
import com.huiju.estateagents.mapper.TaShareRecordMapper;
import com.huiju.estateagents.service.ITaShareActivityService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 拼团活动表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-10-17
 */
@Service
public class TaShareActivityServiceImpl extends ServiceImpl<TaShareActivityMapper, TaShareActivity> implements ITaShareActivityService {

    @Autowired
    private TaShareActivityMapper taShareActivityMapper;
    @Autowired
    private TaShareRecordMapper taShareRecordMapper;

    @Override
    public ResponseBean getList(Integer pageNum, Integer pageSize, String buildingId, String activityName, String activityStatus, LocalDateTime startTime, LocalDateTime endTime, Integer cityId, Integer orgId,List<TaPersonBuilding> personBuildingList) {
        ResponseBean responseBean = new ResponseBean();
        Page<TaShareActivity> page = new Page<>();
        page.setSize(pageSize == null ? 10 : pageSize);
        page.setCurrent(pageNum == null ? 1 : pageNum);

        QueryWrapper<TaShareActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(buildingId != null, "building_id", buildingId);
        queryWrapper.like(activityName != null, "activity_name", activityName);
        queryWrapper.eq(activityStatus != null, "activity_status", activityStatus);
        queryWrapper.eq(startTime != null, "start_time", startTime);
        queryWrapper.eq(endTime != null, "end_time", endTime);
        queryWrapper.eq(orgId != null, "org_id", orgId);
        queryWrapper.eq(cityId != null, "city_id", cityId);
        queryWrapper.in(personBuildingList.size() > 0,"building_id",personBuildingList.stream().map(TaPersonBuilding::getBuildingId).collect(Collectors.toList()));
        queryWrapper.orderByDesc("heavy");
        IPage<TaShareActivity> taNewsIPage = this.page(page, queryWrapper);

        List<TaShareActivity> taShareActivities = taNewsIPage.getRecords();
        for (TaShareActivity taShareActivity : taShareActivities){
            Integer participateNum = taShareActivityMapper.calParticipateNum(taShareActivity.getGroupActivityId());
            taShareActivity.setParticipateNum(participateNum);
            taShareActivity.setGroupBuyPeople(countGroupBuyPeople(taShareActivity));
        }
        taNewsIPage.setRecords(taShareActivities);
        responseBean.addSuccess(taNewsIPage);
        return responseBean;
    }

    @Override
    public Integer countGroupBuyPeople(TaShareActivity taShareActivity){
        return taShareRecordMapper.countGroupBuyPeople(taShareActivity.getGroupActivityId());
    }
    @Override
    public void updateActivityToUnStart() {
        taShareActivityMapper.updateActivityToUnStart();
    }

    @Override
    public void updateUnStartGroupActivity() {
        taShareActivityMapper.updateUnStartGroupActivity();
    }

    @Override
    public void updateProcessingGroupActivity() {
        taShareActivityMapper.updateProcessGroupActivity();
    }

    @Override
    public ResponseBean getEffectiveList(Integer pageNum, Integer pageSize, Integer cityId, String buildingId, Integer orgId,Integer status,String name,List<TaPersonBuilding> personBuildingList) {
        ResponseBean responseBean = new ResponseBean();
        Page<TaShareActivity> page = new Page<>();
        page.setSize(pageSize == null ? 10 : pageSize);
        page.setCurrent(pageNum == null ? 1 : pageNum);

        QueryWrapper<TaShareActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("activity_status", 0, 1);
        queryWrapper.eq(orgId != null, "org_id", orgId);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.eq(cityId != null, "city_id", cityId);
        queryWrapper.eq(buildingId != null, "building_id", buildingId);
        queryWrapper.like(!StringUtils.isEmpty(name), "activity_name", name);
        queryWrapper.in(personBuildingList.size() > 0,"building_id",personBuildingList.stream().map(TaPersonBuilding::getBuildingId).collect(Collectors.toList()));
        queryWrapper.orderByAsc("activity_status");
        queryWrapper.orderByAsc("start_time");
        IPage<TaShareActivity> taNewsIPage = this.page(page, queryWrapper);

        List<TaShareActivity> taShareActivities = taNewsIPage.getRecords();
        for (TaShareActivity taShareActivity : taShareActivities){
            Integer participateNum = taShareActivityMapper.calParticipateNum(taShareActivity.getGroupActivityId());
            taShareActivity.setParticipateNum(participateNum);
        }
        taNewsIPage.setRecords(taShareActivities);
        responseBean.addSuccess(taNewsIPage);
        return responseBean;
    }
}
