package com.huiju.estateagents.property.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.event.EventBus;
import com.huiju.estateagents.mapper.TaPersonMapper;
import com.huiju.estateagents.property.common.Constant;
import com.huiju.estateagents.property.dao.*;
import com.huiju.estateagents.property.model.*;
import com.huiju.estateagents.property.service.SocialServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author weichaochao
 * @Title: SocialServiceImpl
 * @date 2018/10/23
 */
@Service("SocialService")
@Transactional
public class SocialServiceImpl implements SocialServiceI {

    @Autowired
    private TpAnnouncementMapper tpAnnouncementMapper;

    @Autowired
    private TpSocialViewMapper tpSocialViewMapper;

    @Autowired
    private TdImagesMapper tdImagesMapper;

    @Autowired
    private TpTicketMapper tpTicketMapper;

    @Autowired
    private TpTicketRecordMapper tpTicketRecordMapper;

    @Autowired
    private TaPersonMapper taPersonMapper;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    @Transactional
    public TpAnnouncement findAnnouncementDetail(Integer id, Integer orgId, String personId) {
        //更新此用户的查看次数操作
        Integer allViewNum = updateSocialView(personId, Constant.ANNOUNCEMENT, orgId, id);
        if (null != allViewNum) {
            tpAnnouncementMapper.updateByPrimaryKeySelective(id, allViewNum);
        }
        TpAnnouncement tpAnnouncement = tpAnnouncementMapper.selectByPrimaryKey(id, orgId);
//        tpAnnouncement.setUserName(tpUserMapper.selectByPrimaryKey(tpAnnouncement.getCreateUser()).getUserName());
//        tpAnnouncement.setUserName(tpUserMapper.selectByPrimaryKey(tpAnnouncement.getCreateUser()).getUserName());

        Map<String, Object> map = Maps.newHashMap();
        map.put("uuid", tpAnnouncement.getId());
        map.put("type", Constant.ANNOUNCEMENT);
        // 查询图片
        List<TdImages> tdImagesList = tdImagesMapper.getByUUIDAndByType(map);
        List<String> imgList = tdImagesList.stream().map(e -> new String(e.getImageUrl())).collect(Collectors.toList());
        tpAnnouncement.setImgList(imgList);
        return tpAnnouncement;
    }

    /**
     * 分页获取公告列表
     *
     * @param orgId
     * @param title
     * @param annType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseBean getAnnouncements(Integer orgId, String title, String annType, Integer pageNum, Integer pageSize,Integer communityId) {
        ResponseBean responseBean = new ResponseBean();
        Map<String, Object> parameter = Maps.newHashMap();
        //使用分页插件
        Page<TpAnnouncement> page = new Page<>(pageNum, pageSize);
        // 获取数据
        IPage<TpAnnouncement> wxAnnouncementsPage = tpAnnouncementMapper.getWxAnnouncements(page, orgId, title, annType, communityId);
        responseBean.addSuccess(wxAnnouncementsPage);
        return responseBean;
    }

    /**
     * 评价工单以及评分
     *
     * @param orgId
     * @param tpTicket
     * @param ticketId
     * @param personId
     */
    @Override
    public void accessTicket(Integer orgId, TpTicket tpTicket, String ticketId, String personId) {

        //修改工单内容和评分
        tpTicket.setId(Integer.valueOf(ticketId));
        tpTicket.setStatus("5");
        tpTicketMapper.updateById(tpTicket);
        //根据1.4版本需要被终止时，清空当前工单受理人
        tpTicketMapper.updateTpUserId(Integer.valueOf(ticketId));
        //工单处理表新增评分记录
        TpTicketRecord record = new TpTicketRecord();
        record.setOrgId(orgId);
        record.setTicketId(Integer.valueOf(ticketId));
        record.setStatus("5");
        record.setContent(tpTicket.getComment());
        record.setCreateUser(personId);
        record.setCreateDate(LocalDateTime.now());
        tpTicketRecordMapper.insert(record);

        TpTicket tic = tpTicketMapper.selectById(Integer.valueOf(ticketId));
        // 评价成功加积分
        applicationContext.publishEvent(new EventBus(tic, EventBus.EventType.TicketEvaluation,1));
    }


    /**
     * 更新此用户的查看次数操作
     *
     * @param userId
     * @param socialType
     * @param orgId
     * @param uuid
     */
    private Integer updateSocialView(String personId, String socialType, Integer orgId, Integer uuid) {
        //检查是否查看过这个帖子
        TpSocialView tpSocialView = new TpSocialView();
        tpSocialView.setTaUserId(personId);
        tpSocialView.setSocialType(socialType);
        tpSocialView.setOrgId(orgId);
        tpSocialView.setUuid(uuid);
        Integer viewNum = tpSocialViewMapper.selectViewNum(tpSocialView);

        //查看过的话查看次数加一
        if (viewNum == 0) {
            tpSocialView.setCreateDate(new Date());
            tpSocialViewMapper.insertSelective(tpSocialView);
            tpSocialView.setTaUserId(null);
            Integer allViewNum = tpSocialViewMapper.selectViewNum(tpSocialView);
            return allViewNum;
        }
        return null;
    }
}