package cn.axzo.msg.center.message.service.impl;

import cn.axzo.msg.center.dal.MessageTemplateButtonV3Dao;
import cn.axzo.msg.center.domain.entity.MessageTemplateButtonV3;
import cn.axzo.msg.center.message.service.MessageTemplateButtonV3Service;
import cn.axzo.msg.center.service.dto.MessageTemplateButtonV3DTO;
import cn.axzo.msg.center.service.enums.RouterCategoryEnum;
import cn.axzo.msg.center.utils.UUIDUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author cold_blade
 * @date 2023/10/5
 * @version 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MessageTemplateButtonV3ServiceImpl implements MessageTemplateButtonV3Service {

    private final MessageTemplateButtonV3Dao messageTemplateButtonV3Dao;

    @Override
    public void saveTemplateButton(String messageTemplateCode, List<MessageTemplateButtonV3DTO> buttonV3List) {
        log.info("ButtonV3#saveTemplateButton,code:{},buttonV3List:{}",messageTemplateCode, JSON.toJSONString(buttonV3List));
        if (StringUtils.isBlank(messageTemplateCode) || CollectionUtils.isEmpty(buttonV3List)) {
            return;
        }
        AtomicInteger priority = new AtomicInteger(1);
        List<MessageTemplateButtonV3> buttonV3s = buttonV3List.stream().map(item -> {
            MessageTemplateButtonV3 buttonV3 = BeanUtil.copyProperties(item, MessageTemplateButtonV3.class);
            buttonV3.setTemplateCode(messageTemplateCode);
            buttonV3.setPriority(priority.getAndIncrement());
            buttonV3.setCode(StringUtils.isNotBlank(item.getBtnCode()) ? item.getBtnCode() : UUIDUtil.uuidString());
            if (item.getCategory() == RouterCategoryEnum.ACTION) {
                item.setUrlConfig(null);
            }
            if (item.getUrlConfig() != null) {
                item.getUrlConfig().setFieldsNullIfUrlAbsent();
            }
            return buttonV3;
        }).collect(Collectors.toList());
        log.info("ButtonV3#saveTemplateButton,code:{},buttonV3s:{}",messageTemplateCode, JSON.toJSONString(buttonV3s));
        messageTemplateButtonV3Dao.saveBatch(buttonV3s);
    }

    @Override
    public void updateTemplateButton(String messageTemplateCode, List<MessageTemplateButtonV3DTO> buttonV3List) {
        log.info("ButtonV3#updateTemplateButton,code:{},buttonV3List:{}",messageTemplateCode, JSON.toJSONString(buttonV3List));
        //逻辑删除
        this.deleteTemplateButton(messageTemplateCode);
        // 保存
        this.saveTemplateButton(messageTemplateCode, buttonV3List);
    }

    @Override
    public void deleteTemplateButton(String templateNode) {
        log.info("ButtonV3#deleteTemplateButton,code:{}",templateNode);
        if (StringUtils.isBlank(templateNode)) {
            log.info("the templateNode is blank.");
            return;
        }
        messageTemplateButtonV3Dao.lambdaUpdate()
                .eq(MessageTemplateButtonV3::getTemplateCode, templateNode)
                .eq(MessageTemplateButtonV3::getIsDelete, 0)
                .setSql("is_delete = id")
                .update();
    }

    @Override
    public List<MessageTemplateButtonV3> listMessageTemplateButtonByCode(String messageTemplateCode) {
        if (StringUtils.isBlank(messageTemplateCode)) {
            log.info("messageTemplateCode is blank.");
            return Lists.newArrayList();
        }
        return messageTemplateButtonV3Dao.lambdaQuery()
                .eq(MessageTemplateButtonV3::getTemplateCode, messageTemplateCode)
                .eq(MessageTemplateButtonV3::getIsDelete, 0)
                .list();
    }
}
