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.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.entity.TaBuilding;
import com.huiju.estateagents.entity.TaPerson;
import com.huiju.estateagents.entity.TaSave;
import com.huiju.estateagents.mapper.*;
import com.huiju.estateagents.service.ITaSaveService;
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-07-25
 */
@Service
public class TaSaveServiceImpl extends ServiceImpl<TaSaveMapper, TaSave> implements ITaSaveService {


    @Autowired
    TaPersonMapper taPersonMapper;

    @Autowired
    private TaSaveMapper taSaveMapper;

    @Autowired
    private TaNewsMapper taNewsMapper;

    @Autowired
    private TaBuildingMapper taBuildingMapper;

    @Autowired
    private TaBuildingDynamicMapper taBuildingDynamicMapper;

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

        TaPerson taPerson = persons.get(0);

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

        // 写入收藏
        TaSave taSave = new TaSave();
        taSave.setSavedTarget(like);
        taSave.setCreateDate(LocalDateTime.now());
        taSave.setNameOrnick(taPerson.getNickname());
        taSave.setPersonId(taPerson.getPersonId());
        taSave.setPhotoOravatar(taPerson.getAvatarurl());
        taSave.setOrgId(orgId);
        taSave.setTagertType(typeOf);

        int row = taSaveMapper.insert(taSave);
        if (row < 1) {
            return ResponseBean.error("收藏失败", ResponseBean.ERROR_UNAVAILABLE);
        }

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

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

        // 如果是項目收藏, 则 +1
        if(typeOf.equals(CommConstant.FAVOR_PROJECT)) {
            taBuildingMapper.setFieldNum(like, "save_num", 1);
        }
        if(typeOf.equals("card")){
            taPersonMapper.setFieldNum(like,"save_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<TaSave> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id", taPerson.getPersonId());
        queryWrapper.eq("saved_target", like);
        queryWrapper.eq("tagert_type", typeOf);
        List<TaSave> saves = taSaveMapper.selectList(queryWrapper);
        if (null == saves || saves.size() == 0) {
            return ResponseBean.success("");
        }

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

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

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

        if(typeOf.equals(CommConstant.FAVOR_PROJECT)){
            taBuildingMapper.setFieldNum(like, "save_num", -1);
        }
        if(typeOf.equals("card")){
            taPersonMapper.setFieldNum(like,"save_num", -1);
        }

        return ResponseBean.success("");
    }

    @Override
    public List<String> getTargesOfPerson(String personId, String tagertType) {
        return taSaveMapper.getTargesOfPerson(personId, tagertType);
    }

    @Override
    public TaSave getInfoBy(String personId, String tagertType, String savedTarget) {
        QueryWrapper<TaSave> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id", personId);
        queryWrapper.eq("tagert_type", tagertType);
        queryWrapper.eq("saved_target", savedTarget);
        queryWrapper.last("limit 1");

        return taSaveMapper.selectOne(queryWrapper);
    }

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