package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.CommConstant;
import com.huiju.estateagents.entity.*;
import com.huiju.estateagents.mapper.*;
import com.huiju.estateagents.service.ITaSharePersonFromService;
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 2019-10-08
 */
@Service
public class TaSharePersonFromServiceImpl extends ServiceImpl<TaSharePersonFromMapper, TaSharePersonFrom> implements ITaSharePersonFromService {

    @Autowired
    private TaPersonMapper taPersonMapper;

    @Autowired
    private TaBuildingDynamicMapper taBuildingDynamicMapper;

    @Autowired
    private TaPersonBuildingMapper taPersonBuildingMapper;

    @Autowired
    private TaNewsMapper taNewsMapper;

    @Autowired
    private TaSharePersonFromMapper taSharePersonFromMapper;

    @Autowired
    private  TaSalesBatchMapper taSalesBatchMapper;

    @Autowired
    private TaLiveActivityMapper taLiveActivityMapper;

    /**
     * 添加客户分享记录
     *
     * @param personId
     * @param orgId
     * @param from
     * @param recommender
     * @param targetId
     * @return
     */
    @Override
    public TaSharePersonFrom addSharePersonRecord(String personId, Integer orgId, String from, String recommender, String targetId) {
        TaPerson taPerson = taPersonMapper.getById(personId);
        TaSharePersonFrom taSharePersonFrom = new TaSharePersonFrom();
        TaPerson recommenderPerson = taPersonMapper.getById(recommender);
        taSharePersonFrom.setCreateDate(LocalDateTime.now());
        taSharePersonFrom.setOrgId(orgId);
        taSharePersonFrom.setPersonId(taPerson.getPersonId());
        taSharePersonFrom.setSharePersonType(recommenderPerson.getPersonType());
        taSharePersonFrom.setTargetId(targetId);
        taSharePersonFrom.setTargetType(from);
        if(CommConstant.PERSON_REALTY_CONSULTANT.equals(recommenderPerson.getPersonType())){
            taSharePersonFrom.setSharePerson(recommenderPerson.getUserId().toString());
        }else {
            taSharePersonFrom.setSharePerson(recommenderPerson.getPersonId());
        }
        QueryWrapper<TaSharePersonFrom> sharePersonFromQueryWrapper = new QueryWrapper<>();
        sharePersonFromQueryWrapper.eq("person_id",taPerson.getPersonId());
        if(from.equals("dynamic_share")){ // activity_share
            taSharePersonFrom.setStatus(CommConstant.STATUS_UNACCALIMED);
            TaBuildingDynamic buildingDynamic = taBuildingDynamicMapper.selectById(targetId);
            taSharePersonFrom.setBuildingId(buildingDynamic.getBuildingId());
            sharePersonFromQueryWrapper.eq("building_id",buildingDynamic.getBuildingId());
        }else {
            taSharePersonFrom.setStatus(CommConstant.STATUS_NORMAL);
        }
        if (from.contains("card")){
            // 因为targetId 是 personId ，需要查询出 userId
            TaPerson person = taPersonMapper.getById(targetId);

            QueryWrapper<TaPersonBuilding> personBuildingQueryWrapper = new QueryWrapper<>();
            personBuildingQueryWrapper.eq("user_id",person.getUserId());
            TaPersonBuilding personBuilding = taPersonBuildingMapper.selectOne(personBuildingQueryWrapper);
            taSharePersonFrom.setBuildingId(personBuilding.getBuildingId());
            sharePersonFromQueryWrapper.eq("building_id",personBuilding.getBuildingId());
        }else if (from.contains("news")){
            TaNews news = taNewsMapper.selectById(targetId);
            taSharePersonFrom.setBuildingId(news.getBuildingId());
            sharePersonFromQueryWrapper.eq("building_id",news.getBuildingId());
        }else if (from.contains("building")){
            taSharePersonFrom.setBuildingId(targetId);
            sharePersonFromQueryWrapper.eq("building_id",targetId);
        }
        Integer isFirstTime = taSharePersonFromMapper.selectCount(sharePersonFromQueryWrapper);
        if(isFirstTime>0){
            taSharePersonFrom.setIsFirstTime(false);
        }else {
            taSharePersonFrom.setIsFirstTime(true);
        }
        taSharePersonFromMapper.insert(taSharePersonFrom);
        return taSharePersonFrom;
    }

    @Override
    public ResponseBean selectHouseVisitList(Integer pageNum, Integer pageSize, Integer orgId, String personType, String sceneType, String phone, String saleBatchId) {
        Page page = new Page(pageNum,pageSize);
        // 分页查询
        List<TaSharePersonFrom> list = taSharePersonFromMapper.selectHouseVisitList(page, orgId, personType, sceneType, phone, saleBatchId);
        TaSalesBatch taSalesBatch = taSalesBatchMapper.selectById(saleBatchId);
        list.forEach(e -> {
            e.setSalesBatchName(taSalesBatch.getSalesBatchName());
        });
        page.setRecords(list);
        return ResponseBean.success(page);
    }

    @Override
    public ResponseBean selectLiveActivityVisitList(Integer pageNum, Integer pageSize, Integer orgId, String personType, String sceneType, String phone, String liveActivityId) {
        Page page = new Page(pageNum,pageSize);
        // 分页查询
        List<TaSharePersonFrom> list = taSharePersonFromMapper.selectLiveActivityVisitList(page, orgId, personType, sceneType, phone, liveActivityId);
        TaLiveActivity taLiveActivity = taLiveActivityMapper.selectById(liveActivityId);
        list.forEach(e -> {
            e.setLiveActivityTitle(taLiveActivity.getLiveActivityTitle());
        });
        page.setRecords(list);
        return ResponseBean.success(page);
    }
}
