package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.TaBuildingDynamic;
import com.huiju.estateagents.entity.TaFavor;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.mapper.*;
import com.huiju.estateagents.service.ITaFavorService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 点赞表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@Service
public class TaFavorServiceImpl extends ServiceImpl<TaFavorMapper, TaFavor> implements ITaFavorService {

    @Autowired
    TaPersonMapper taPersonMapper;

    @Autowired
    TaFavorMapper taFavorMapper;

    @Autowired
    private TaNewsMapper taNewsMapper;

    @Autowired
    private TaBuildingDynamicMapper taBuildingDynamicMapper;

    @Autowired
    private TaBuildingMapper taBuildingMapper;

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

        TaPerson taPerson = persons.get(0);

        // 是否被赞过
        QueryWrapper<TaFavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id", taPerson.getPersonId());
        queryWrapper.eq("be_favor", like);
        queryWrapper.eq("tagert_type", typeOf);
        List<TaFavor> favors = taFavorMapper.selectList(queryWrapper);
        if (null != favors && favors.size() > 0) {
//            return ResponseBean.error("请不需要重复点赞", ResponseBean.ERROR_UNAVAILABLE);
            return ResponseBean.success("");
        }

        // 写入赞
        TaFavor taFavor = new TaFavor();
        taFavor.setBeFavor(like);
        taFavor.setCreateDate(LocalDateTime.now());
        taFavor.setNameOrnick(taPerson.getNickname());
        taFavor.setPersonId(taPerson.getPersonId());
        taFavor.setPhotoOravatar(taPerson.getAvatarurl());
        taFavor.setTagertType(typeOf);

        int row = taFavorMapper.insert(taFavor);
        if (row < 1) {
            return ResponseBean.error("点赞失败", ResponseBean.ERROR_UNAVAILABLE);
        }

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

        // 如果是资讯点赞, 则 +1
        if(typeOf.equals(CommConstant.FAVOR_NEWS)) {
            taNewsMapper.setFieldNum(like, "favor_num", 1);
        }

        // 如果是活动点赞, 则 +1
        if(typeOf.equals(CommConstant.FAVOR_ACTIVITY)) {
            taBuildingDynamicMapper.setFieldNum(like, "favor_num", 1);
        }

        if(typeOf.equals(CommConstant.FAVOR_PROJECT)) {
            taBuildingMapper.setFieldNum(like, "favor_num", 1);
        }

        return ResponseBean.success("");
    }

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

        TaPerson taPerson = persons.get(0);

        // 是否被赞过
        QueryWrapper<TaFavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id", taPerson.getPersonId());
        queryWrapper.eq("be_favor", like);
        queryWrapper.eq("tagert_type", typeOf);
        List<TaFavor> favors = taFavorMapper.selectList(queryWrapper);
        if (null == favors || favors.size() == 0) {
            return ResponseBean.success("");
        }

        Map<String, Object> condition = new HashMap<>();
        condition.put("person_id", taPerson.getPersonId());
        condition.put("be_favor", like);
        condition.put("tagert_type", typeOf);
        taFavorMapper.deleteByMap(condition);

        // 如果是点赞置业, 则人员点赞 - 1
        if (typeOf.equals("card")) {
            taPersonMapper.setFieldIncrement(taPerson.getPersonId(),"like_num", -1);
        }

        // 如果是收藏资讯, 则 - 1
        if (typeOf.equals(CommConstant.FAVOR_NEWS)) {
            taNewsMapper.setFieldNum(taPerson.getPersonId(),"favor_num", -1);
        }

        // 如果项目资讯, 则 - 1
        if(typeOf.equals(CommConstant.FAVOR_PROJECT)) {
            taBuildingMapper.setFieldNum(like, "favor_num", -1);
        }


        // 如果是活动点赞, 则 -1
        if(typeOf.equals(CommConstant.FAVOR_ACTIVITY)) {
            taBuildingDynamicMapper.setFieldNum(like, "favor_num", -1);
        }

        return ResponseBean.success("");
    }

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