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.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.entity.TaUv;
import com.huiju.estateagents.mapper.TaBuildingMapper;
import com.huiju.estateagents.mapper.TaPersonMapper;
import com.huiju.estateagents.mapper.TaUvMapper;
import com.huiju.estateagents.service.ITaUvService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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-05-10
 */
@Service
public class TaUvServiceImpl extends ServiceImpl<TaUvMapper, TaUv> implements ITaUvService {
    @Autowired
    TaPersonMapper taPersonMapper;

    @Autowired
    TaUvMapper taUvMapper;

    @Autowired
    TaBuildingMapper buildingMapper;

    @Override
    public ResponseBean saveData(String openid, String typeOf, String id) {
        List<TaPerson> persons = getPersonsByOpenId(openid);
        if (null == persons || persons.size() != 1) {
            return ResponseBean.error("验证当前人员信息失败", ResponseBean.ERROR_UNAVAILABLE);
        }

        if (typeOf.equals(CommConstant.FAVOR_PROJECT)) {
            buildingMapper.setFieldNum(id,"pv_num",1);
        }

        TaPerson taPerson = persons.get(0);

        QueryWrapper<TaUv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id", taPerson.getPersonId());
        queryWrapper.eq("be_uv", id);
        queryWrapper.eq("tagert_type", typeOf);
        List<TaUv> uvs = taUvMapper.selectList(queryWrapper);
        if (null != uvs && uvs.size() > 0) {
            for(int i=0;i<uvs.size();i++){
                uvs.get(i).setCreateDate(LocalDateTime.now());
                uvs.get(i).setNameOrnick(taPerson.getNickname());
                uvs.get(i).setPersonId(taPerson.getPersonId());
                uvs.get(i).setPhotoOravatar(taPerson.getAvatarurl());
            }
            if(this.updateBatchById(uvs)){
                return ResponseBean.success("");
            }else {
                return ResponseBean.error("",ResponseBean.ERROR_UNAVAILABLE);
            }

        }

        TaUv taUv = new TaUv();
        taUv.setBeUv(id);
        taUv.setCreateDate(LocalDateTime.now());
        taUv.setNameOrnick(taPerson.getNickname());
        taUv.setPersonId(taPerson.getPersonId());
        taUv.setPhotoOravatar(taPerson.getAvatarurl());
        taUv.setTagertType(typeOf);

        int row = taUvMapper.insert(taUv);
        if (row < 1) {
            return ResponseBean.error("人气增加失败", ResponseBean.ERROR_UNAVAILABLE);
        }

        // 如果是点赞置业, 则人员点赞 + 1
        if (typeOf.equals(CommConstant.FAVOR_CONSULTANT)) {
            taPersonMapper.setFieldIncrement(id,"hot_num", 1);
        }

        return ResponseBean.success("");
    }

    @Override
    public ResponseBean getListOf(String id, String typeOf, Integer pageNumber, Integer pageSize) {
        IPage<TaUv> page = new Page(pageNumber, pageSize);
        QueryWrapper<TaUv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("be_uv", id);
        queryWrapper.eq("tagert_type", typeOf);
        queryWrapper.orderByDesc("create_date");

        IPage<TaUv> result = taUvMapper.selectPage(page, queryWrapper);
        return ResponseBean.success(result);
    }


    List<TaPerson> getPersonsByOpenId(String openid) {
        return taPersonMapper.getPersonByOpenId(openid);
    }
}
