package com.czg.service.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.entity.ShopInfo;
import com.czg.account.service.ShopInfoService;
import com.czg.config.RabbitPublisher;
import com.czg.market.dto.SmsShopTemplateDTO;
import com.czg.market.dto.SmsTemplateQueryDTO;
import com.czg.market.entity.SmsShopTemplate;
import com.czg.market.service.SmsShopTemplateService;
import com.czg.service.market.mapper.SmsShopTemplateMapper;
import com.czg.utils.CzgStrUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 店铺模板表 服务层实现。
 *
 * @author ww
 * @since 2025-10-14
 */
@Service
public class SmsShopTemplateServiceImpl extends ServiceImpl<SmsShopTemplateMapper, SmsShopTemplate> implements SmsShopTemplateService {

    @Resource
    private RabbitPublisher rabbitPublisher;
    @DubboReference
    private ShopInfoService shopInfoService;

    @Override
    public List<SmsShopTemplateDTO> getTemplateList(String name, Long shopId, Integer status) {
        Set<Long> list = new HashSet<>();
        list.add(1L);
        list.add(shopId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(SmsShopTemplate::getTitle, name)
                .in(SmsShopTemplate::getShopId, list)
                .eq(SmsShopTemplate::getIsDel, 0)
                .orderBy(SmsShopTemplate::getShopId).asc()
                .orderBy(SmsShopTemplate::getSort).asc();
        if (list.size() == 1) {
            queryWrapper.eq(SmsShopTemplate::getShopUse, status);
        } else {
            queryWrapper.eq(SmsShopTemplate::getShopUse, 1);
        }
        return listAs(queryWrapper, SmsShopTemplateDTO.class);
    }

    @Override
    public Page<SmsShopTemplateDTO> getTemplateList2(SmsTemplateQueryDTO param) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                .le(SmsShopTemplate::getCreateTime, CzgStrUtils.getStrOrNull(param.getEndTime()))
                .ge(SmsShopTemplate::getCreateTime, CzgStrUtils.getStrOrNull(param.getStartTime()))
                .eq(SmsShopTemplate::getStatus, param.getStatus())
                .eq(SmsShopTemplate::getIsDel, 0)
                .eq(SmsShopTemplate::getShopUse, param.getShopUse())
                .eq(SmsShopTemplate::getShopId, param.getShopId())
                .orderBy(SmsShopTemplate::getShopId).asc()
                .orderBy(SmsShopTemplate::getSort).asc();
        Page<SmsShopTemplateDTO> page = pageAs(new Page<>(param.getPage(), param.getSize()), queryWrapper, SmsShopTemplateDTO.class);
        if (param.getShopId() == null) {
            Map<Long, String> shopNameMap = new HashMap<>();
            for (SmsShopTemplateDTO record : page.getRecords()) {
                if (!shopNameMap.containsKey(record.getShopId())) {
                    ShopInfo shopInfo = shopInfoService.getById(record.getShopId());
                    shopNameMap.put(record.getShopId(), shopInfo.getShopName());
                }
                record.setShopName(shopNameMap.get(record.getShopId()));
            }
        }
        return page;
    }

    @Override
    public void addTemplate(SmsShopTemplateDTO param) {
        SmsShopTemplate template = BeanUtil.toBean(param, SmsShopTemplate.class);
        template.setStatus(0);
        template.setSort(0);
        template.setShopUse(1);
        if (template.getShopId().equals(1L)) {
            template.setStatus(2);
        }
        save(template);
        if (!template.getShopId().equals(1L)) {
            sendApplyMsg(template.getShopId(), template.getId());
        }
    }

    @Override
    public void shopUse(SmsShopTemplateDTO param) {
        SmsShopTemplate template = new SmsShopTemplate();
        template.setId(param.getId());
        template.setShopUse(param.getShopUse());
        template.setIsDel(param.getIsDel());
        updateById(template);
    }

    @Override
    public void resubmit(SmsShopTemplateDTO param) {

        // 校验状态是否为 -1 失败
        SmsShopTemplate template1 = getById(param.getId());
        if (!template1.getStatus().equals(-1)) {
            throw new IllegalArgumentException("只有失败的模板才能重新提交");
        }

        SmsShopTemplate template = BeanUtil.toBean(param, SmsShopTemplate.class);
        template.setStatus(-2);
        template.setFailMsg("");
        updateById(template);
        sendApplyMsg(template.getShopId(), template.getId());
    }

    private void sendApplyMsg(Long shopId, Long templateId) {
        // 推送消息 进行 阿里模板申请
        rabbitPublisher.sendApplySmsMsg(shopId + "," + templateId, "applySmsTemp");
    }
}
