package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaPersonFromRecord;
import com.huiju.estateagents.entity.TaPersonVisitRecord;
import com.huiju.estateagents.entity.TdWxDict;
import com.huiju.estateagents.mapper.TaPersonFromRecordMapper;
import com.huiju.estateagents.mapper.TdWxDictMapper;
import com.huiju.estateagents.service.ITaPersonFromRecordService;
import com.huiju.estateagents.statistic.entity.TsPersonFromStatistic;
import com.huiju.estateagents.statistic.mapper.TsPersonFromStatisticMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 新增用户来源表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-01-03
 */
@Service
public class TaPersonFromRecordServiceImpl extends ServiceImpl<TaPersonFromRecordMapper, TaPersonFromRecord> implements ITaPersonFromRecordService {

    @Autowired
    private TaPersonFromRecordMapper taPersonFromRecordMapper;

    @Autowired
    private TsPersonFromStatisticMapper tsPersonFromStatisticMapper;

    @Autowired
    private TdWxDictMapper tdWxDictMapper;

    /**
     * 通過買點表清洗數據
     * @param taPersonVisitRecord
     */
    @Override
    public void addNewPersonRecordRecord(TaPersonVisitRecord taPersonVisitRecord) {
        TaPersonFromRecord taPersonFromRecord = new TaPersonFromRecord();
        taPersonFromRecord.setCreateDate(LocalDateTime.now());
        taPersonFromRecord.setActivity(taPersonVisitRecord.getActivity());
        taPersonFromRecord.setOrgId(Integer.valueOf(taPersonVisitRecord.getOrgId()));
        taPersonFromRecord.setPersonId(taPersonVisitRecord.getPersonId());
        taPersonFromRecord.setBuildingId(taPersonVisitRecord.getBuildingId());
        taPersonFromRecord.setEvent(taPersonVisitRecord.getEvent());
        taPersonFromRecord.setEventType(taPersonVisitRecord.getEventType());
        taPersonFromRecord.setSceneId(taPersonVisitRecord.getSceneId());
        TdWxDict tdWxDict = tdWxDictMapper.selectById(taPersonVisitRecord.getSceneId());
        taPersonFromRecord.setSceneType(null == tdWxDict ? "" : tdWxDict.getSceneType());
        taPersonFromRecord.setSharePersonId(taPersonVisitRecord.getSharePersonId());
        taPersonFromRecord.setTargetId(taPersonVisitRecord.getTargetId());
        QueryWrapper<TaPersonFromRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id",taPersonVisitRecord.getOrgId());
        queryWrapper.eq("person_id",taPersonVisitRecord.getPersonId());
        int count = taPersonFromRecordMapper.selectCount(queryWrapper);
        if(count > 0){
            return;
        }else {
            taPersonFromRecord.setIsFirstTime(true);
        }
        taPersonFromRecordMapper.insert(taPersonFromRecord);
    }

    /**
     * 定时任务批量保存数据
     */
    @Override
    public void personFromStatistic() {
        List<TsPersonFromStatistic> list =  taPersonFromRecordMapper.getNowDataList();
        tsPersonFromStatisticMapper.deleteByData();
        tsPersonFromStatisticMapper.insertStatisticList(list);
    }
}
