package cn.com.countrygarden.bi.main.service.experience.impl;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.core.ResultCode;
import cn.com.countrygarden.bi.main.dao.experience.TygGzhH5Mapper;
import cn.com.countrygarden.bi.main.dao.experience.WcxExperienceMapper;
import cn.com.countrygarden.bi.main.dto.experience.WcxExperContentDto;
import cn.com.countrygarden.bi.main.dto.experience.WcxMyPublishContentDto;
import cn.com.countrygarden.bi.main.dto.experience.WcxPublishContentDto;
import cn.com.countrygarden.bi.main.dto.experience.WcxPublishFileDto;
import cn.com.countrygarden.bi.main.model.experience.WcxPublishFile;
import cn.com.countrygarden.bi.main.model.experience.WcxPublishMessage;
import cn.com.countrygarden.bi.main.service.experience.TygGzhH5Service;
import cn.com.countrygarden.bi.main.util.EmptyUtils;
import cn.com.countrygarden.bi.main.util.PageUtils;
import cn.com.countrygarden.bi.main.util.SequenceUUID;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class TygGzhH5ServiceImpl implements TygGzhH5Service {
    @Autowired
    private TygGzhH5Mapper tygGzhH5Mapper;
    @Autowired
    private WcxExperienceMapper wcxExperienceMapper;
    @Override
    public Result index(String phone, int size, int page) {
        Result result=new Result();
        if(phone ==null || phone ==""){
            result.setResultCode(400);
            result.setResultMsg("phone不能为空");
        }
        List<WcxExperContentDto> list= tygGzhH5Mapper.selectListByPhone(phone,size,(page-1)*size);
        if (CollectionUtils.isNotEmpty(list)){
            for (WcxExperContentDto contentDto : list){
                String publishNo = contentDto.getPublishNo();
                String innSimpleContent = contentDto.getPublishContent();
                if (StringUtils.hasText(innSimpleContent)) {
                    String resultString = EmojiParser.parseToUnicode(innSimpleContent);
                    contentDto.setPublishContent(resultString);
                } else {
                    // todo 日志 打印没有简易信息
                }
                List<WcxPublishFileDto> fileDtoList = wcxExperienceMapper.queryPublishFile(publishNo);
                contentDto.setFileDtos(fileDtoList);
            }
        }
        int total = tygGzhH5Mapper.selectCountByPhone(phone);
        PageUtils pageUtils = new PageUtils(list,total);
        return result.setResultCode(ResultCode.SUCCESS).setData(pageUtils);
    }

    @Override
    public Result publish(WcxPublishContentDto dto, String openid) {
        Result result = new Result();

        try {
            WcxPublishMessage publishMessage = new WcxPublishMessage();
            List<WcxPublishFile> fileList = new ArrayList<WcxPublishFile>();
            String publishNo = SequenceUUID.getSequence();
            publishMessage.setPublishNo(publishNo);
            publishMessage.setOpenId(openid);
            publishMessage.setCreated(new Date());
            publishMessage.setCreater(null);
            publishMessage.setUpdated(new Date());
            publishMessage.setUpdater(null);
            publishMessage.setName(dto.getName());
            publishMessage.setPhone(dto.getPhone());
            publishMessage.setIdentity(dto.getIdentity());
            publishMessage.setPublishType(dto.getPublishType());
            publishMessage.setPublishContent(dto.getPublishContent());
            publishMessage.setPublishTime(new Date());
            publishMessage.setDeleted(0);
            publishMessage.setVersion(0L);
            publishMessage.setAvatarUrl(dto.getAvatarUrl());

            tygGzhH5Mapper.save(publishMessage);

            List<WcxPublishFileDto> dtoList = dto.getFileDtos();

            if (CollectionUtils.isNotEmpty(dtoList)){

                for (WcxPublishFileDto fileDto : dtoList){

                    WcxPublishFile publishFile = new WcxPublishFile();
                    publishFile.setCreated(new Date());
                    publishFile.setCreater(null);
                    publishFile.setUpdated(new Date());
                    publishFile.setUpdater(null);
                    publishFile.setPushlishNo(publishNo);
                    publishFile.setPublishType(fileDto.getPublishType());
                    publishFile.setPublishUrl(fileDto.getPublishUrl());
                    publishFile.setDeleted(0);
                    publishFile.setVersion(0L);
                    fileList.add(publishFile);
                }
            }

            if (CollectionUtils.isNotEmpty(fileList)){
                tygGzhH5Mapper.saveFile(fileList);
            }
            result.setResultCode(200);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setResultCode(500);
            result.setResultMsg("系统服务开小差，请稍后再试");
            return result;
        }
    }

    @Override
    public Map<String, Object> myInfo(WcxPublishContentDto dto) {
        Map<String,Object> dataMap = new HashMap<String,Object>();

        //List<WcxMyPublishContentDto> myInfos = wcxExperPublishMapper.myInfo(dto);

        dto.setIdentity("1");
        WcxMyPublishContentDto info = tygGzhH5Mapper.info(dto);
        if (EmptyUtils.isEmpty(info)){
            dto.setIdentity("0");
            info = tygGzhH5Mapper.info(dto);
            info.setIdentity("0");
        }else{
            info.setIdentity("1");
        }
        //查询总分
        int account=tygGzhH5Mapper.selectCount(dto);
        dto.setAccount(account);
        tygGzhH5Mapper.updateAccount(dto);
        //点赞
        dto.setPublishType("0");
        dto.setDeleted(0);
        List<WcxMyPublishContentDto> niceList = tygGzhH5Mapper.myInfo(dto);

        //吐槽
        dto.setPublishType("1");
        List<WcxMyPublishContentDto> ridiculeList = tygGzhH5Mapper.myInfo(dto);

        //评价分数
        dto.setPublishType(null);
        List<WcxMyPublishContentDto> pointList = tygGzhH5Mapper.myInfo(dto);
        int ponits = 0;
        for (WcxMyPublishContentDto message : pointList){
            ponits = ponits + (EmptyUtils.isNotEmpty(message.getPoint())?message.getPoint():0);
        }

        dataMap.put("myInfo",info);
        dataMap.put("nice",niceList.size());
        dataMap.put("ridiCule",ridiculeList.size());
        dataMap.put("ponits",ponits);

        return dataMap;
    }

    @Override
    public Result delete(WcxPublishContentDto dto) {
        Result result = new Result();

        try {

            List<WcxMyPublishContentDto> myInfo = tygGzhH5Mapper.myInfo(dto);
            if (EmptyUtils.isEmpty(myInfo)){
                result.setResultCode(10010);
                result.setResultMsg("删除的记录不存在!");
                return result;
            }
            //更新当前评论人总分
            int i1 = wcxExperienceMapper.selectCountSourceById(dto.getId());
            //单前评论分
            int i2 = wcxExperienceMapper.selectSourceById(dto.getId());
            WcxMyPublishContentDto message = myInfo.get(0);
            tygGzhH5Mapper.deletePublishMessage(1,dto.getId());
            tygGzhH5Mapper.deletePublishFile(1,message.getPublishNo());
            //删除总评分
            //分数差
            int i = i1 - i2;
            tygGzhH5Mapper.updateAccountByphone(i<0?0:i,dto.getId());
            result.setResultCode(200);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setResultCode(500);
            result.setResultMsg("服务异常，请稍后再试");
            return result;
        }
    }
}
