package com.yunzhi.marketing.xlk.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.yunzhi.marketing.common.StringUtils;
import com.yunzhi.marketing.drainage.entity.TaDrainage;
import com.yunzhi.marketing.entity.*;
import com.yunzhi.marketing.mapper.*;
import com.yunzhi.marketing.service.IMiniAppService;
import com.yunzhi.marketing.xlk.entity.CustomerInfo;
import com.yunzhi.marketing.xlk.mapper.CustomerInfoMapper;
import com.yunzhi.marketing.xlk.service.ICustomerInfoService;
import com.yunzhi.marketing.xlk.vo.CustomerInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 客户基本信息表  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-07-29
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {

    @Autowired
    TaPersonMapper taPersonMapper;

    @Autowired
    TaRecommendCustomerMapper taRecommendCustomerMapper;

    @Autowired
    private TdWxDictMapper tdWxDictMapper;

    @Autowired
    TaPersonVisitRecordMapper taPersonVisitRecordMapper;

    @Autowired
    private TaPersonFromRecordMapper taPersonFromRecordMapper;

    @Autowired
    private TaBuildingDynamicMapper taBuildingDynamicMapper;

    /**
     * 获取其他信息
     *
     * @param customerInfoVO
     */
    @Override
    public void getOtherInfo(CustomerInfoVO customerInfoVO) {
        if (StringUtils.isEmpty(customerInfoVO.getTaRecommendCustomer().getPersonId())) {
            return;
        }

        // 获取person用户信息
        String personId = customerInfoVO.getTaRecommendCustomer().getPersonId();
//        TaPerson taPerson = taPersonMapper.getById(personId);
//        customerInfoVO.setAvatarurl(taPerson.getAvatarurl());
        if (!StringUtils.isEmpty(personId)) {
            List<TaPersonVisitRecord> records = taPersonVisitRecordMapper.visitRecordByPersonIdNew(personId);
            records.forEach(e -> {
                //获取活动标题
                if ("activity".equals(e.getEventType()) && null != e.getTargetId()) {
                    TaBuildingDynamic taBuildingDynamic = taBuildingDynamicMapper.selectById(e.getTargetId());
                    if (null != taBuildingDynamic) {
                        e.setActivityName(taBuildingDynamic.getTitle());
                    }

                }
            });
            customerInfoVO.setPersonVisitRecords(records);
            customerInfoVO.setVisitTimes(records.size());
            customerInfoVO.setPersonIntentions(taRecommendCustomerMapper.getCustomerIntentions(personId, null));
//            customerInfoVO.setDuration(taPersonVisitRecordMapper.getDurationByPersonId(personId));
//            customerInfoVO.setVisitTime(taPersonVisitRecordMapper.getFirstVisitTimeByPersonId(personId));
        }
    }
}
