package com.gic.haoban.manage.web.log;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityDTO;
import com.gic.log.record.bean.GicLogRecordBean;
import com.gic.log.record.service.LogRecordAfterService;
import com.gic.log.record.util.GicLogRecordEvaluationContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * Created by wangzubo on 2022/11/14.
 */
@Service
public class LogRecordAfterGroupChatActivityServiceImpl extends LogRecordAfterService {

    private static final Logger logger = LogManager.getLogger(LogRecordAfterGroupChatActivityServiceImpl.class);

    /**
     {活动名称-活动id}的
     1、活动名称从【XX】修改为【XX】
     2、活动时间从2022.10,23修改为2023.10.23
     3、活动图片更换
     4、活动说明调整
     5、分享配置调整
     * @param record
     */
    @Override
    public void deal(GicLogRecordBean record) {
        try {
            GroupChatActivityDTO oldActivityDTO = GicLogRecordEvaluationContext.getAttribute("oldActivityDTO", GroupChatActivityDTO.class);
            GroupChatActivityDTO newActivityDTO = GicLogRecordEvaluationContext.getAttribute("newActivityDTO", GroupChatActivityDTO.class);
            logger.info("活动操作前信息 {}", JSON.toJSONString(oldActivityDTO));
            logger.info("活动操作后信息 {}", JSON.toJSONString(newActivityDTO));
            if (oldActivityDTO == null || newActivityDTO == null) {
                record.setStatusFlag(0);
                return;
            }

            String content = StrUtil.format("【{}+{}】",oldActivityDTO.getActivityName(),oldActivityDTO.getChatActivityId());
            StringBuilder cdSb = new StringBuilder();
            String oldActivityName = oldActivityDTO.getActivityName();
            String newActivityName = newActivityDTO.getActivityName();
            Date oldStartDate = oldActivityDTO.getStartDate();
            Date oldEndDate = oldActivityDTO.getEndDate();
            Date newStartDate = newActivityDTO.getStartDate();
            Date newEndDate = newActivityDTO.getEndDate();
            if (!Objects.equals(oldActivityName,newActivityName)) {
                cdSb.append("    ")
                        .append(StrUtil.format("活动名称从【{}】修改为【{}】",
                                oldActivityName,
                                newActivityName));
            }
            if (!Objects.equals(oldStartDate,newStartDate) || !Objects.equals(oldEndDate,newEndDate)) {
                cdSb.append("    ")
                        .append(StrUtil.format("活动时间从【{}】修改为【{}】",
                                DateUtil.formatDate(oldStartDate) + "-" + DateUtil.formatDate(oldEndDate),
                                DateUtil.formatDate(newStartDate) + "-" + DateUtil.formatDate(newEndDate)));
            }
            String oldActivityDesc = oldActivityDTO.getActivityDesc();
            String newActivityDesc = newActivityDTO.getActivityDesc();
            if (!Objects.equals(oldActivityDesc,newActivityDesc)) {
                cdSb.append("    ")
                        .append("活动说明调整");
            }
            String oldActivityImg = oldActivityDTO.getActivityImg();
            String oldActivityInviteImg = oldActivityDTO.getActivityInviteImg();

            String newActivityImg = newActivityDTO.getActivityImg();
            String newActivityInviteImg = newActivityDTO.getActivityInviteImg();

            if (!Objects.equals(oldActivityImg,newActivityImg)||
                    !Objects.equals(oldActivityInviteImg,newActivityInviteImg)
            ) {
                cdSb.append("    ")
                        .append("活动图片更换");
            }
            String oldActivityShareTitle = oldActivityDTO.getActivityShareTitle();
            String oldActivityPosterImg = oldActivityDTO.getActivityPosterImg();
            String oldActivityShareImg = oldActivityDTO.getActivityShareImg();
            String newActivityShareTitle = newActivityDTO.getActivityShareTitle();
            String newActivityPosterImg = newActivityDTO.getActivityPosterImg();
            String newActivityShareImg = newActivityDTO.getActivityShareImg();
            if (!Objects.equals(oldActivityShareTitle, newActivityShareTitle) ||
                    !Objects.equals(oldActivityShareImg, newActivityShareImg) ||
                    !Objects.equals(oldActivityPosterImg, newActivityPosterImg)) {
                cdSb.append("    ")
                        .append("分享配置调整");
            }
            String s = cdSb.toString();
            if (StringUtils.isBlank(s)) {
                GicLogRecordEvaluationContext.noWriteLog();
                record.setStatusFlag(0);
            }else {
                content += s;
            }
            record.setContent(content);
        } catch (Exception e) {
            record.setStatusFlag(0);
            record.setContent("日志记录异常！");
            logger.info("日志记录异常执行失败", e);
        }
    }


}
