package org.jeecg.modules.adweb.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.adweb.product.dto.ProductDTO;
import org.jeecg.modules.adweb.product.entity.AdwebProduct;
import org.jeecg.modules.adweb.product.mapper.AdwebProductMapper;
import org.jeecg.modules.adweb.product.service.IAdwebProductService;
import org.jeecg.modules.adweb.site.entity.AdwebSite;
import org.springframework.stereotype.Service;

/**
 * @Description: wp站点产品
 * @Author: jeecg-boot
 * @Date: 2024-12-06
 * @Version: V1.0
 */
@Service
@Slf4j
public class AdwebProductServiceImpl extends ServiceImpl<AdwebProductMapper, AdwebProduct> implements IAdwebProductService {

    /**
     * @param productDTO
     * @param adwebSites
     */
    @Override
    public void addOrUpdateProduct(ProductDTO productDTO, AdwebSite adwebSites) {
        // 产品同步时，update 有可能是新增或者更新，根据数据库中记录进行再次判断
        if ("update".equals(productDTO.getActionType())) {
            handleUpdate(productDTO, adwebSites);
        } else if ("delete".equals(productDTO.getActionType())) {
            handleDelete(productDTO, adwebSites);
        } else {
            handleAdd(productDTO, adwebSites);
        }

    }

    private void handleUpdate(ProductDTO productDTO, AdwebSite adwebSites) {
        QueryWrapper<AdwebProduct> productQuery = new QueryWrapper<>();
        productQuery.eq("product_id", productDTO.getProductId());
        productQuery.eq("site_code", adwebSites.getCode());

        AdwebProduct product = this.getOne(productQuery);
        // 执行更新操作
        if (product != null) {
            updateProduct(productDTO, product);
        } else {
            addProduct(productDTO, adwebSites);
        }
    }

    private void handleDelete(ProductDTO productDTO, AdwebSite adwebSites) {
        UpdateWrapper<AdwebProduct> productQuery = new UpdateWrapper<>();
        productQuery.eq("product_id", productDTO.getProductId());
        productQuery.eq("site_code", adwebSites.getCode());
        productQuery.set("action_type", "delete");
        productQuery.set("request_time", productDTO.getRequestTime());
        productQuery.set("author_name", productDTO.getAuthorName());

        try {
            boolean isUpdated = this.update(productQuery);
            log.info("MQ消息删除产品成功, 网站：{}, 产品ID：{}, 是否更新成功：{}", adwebSites.getCode(), productDTO.getProductId(), isUpdated);
        } catch (Exception e) {
            log.error("MQ消息删除产品失败：{}", e.getMessage(), e);
        }
    }

    private void handleAdd(ProductDTO productDTO, AdwebSite adwebSites) {
        AdwebProduct product = new AdwebProduct();
        BeanUtil.copyProperties(productDTO, product);
        product.setSiteCode(adwebSites.getCode());
        this.saveProduct(product);
    }

    private void updateProduct(ProductDTO productDTO, AdwebProduct product) {
        BeanUtil.copyProperties(productDTO, product);

        try {
            this.updateById(product);
            log.info("MQ消息更新产品成功：{}", product);
        } catch (Exception e) {
            log.error("MQ消息更新产品失败：{}", e.getMessage(), e);
        }
    }

    private void addProduct(ProductDTO productDTO, AdwebSite adwebSites) {
        AdwebProduct product = new AdwebProduct();
        BeanUtil.copyProperties(productDTO, product);
        product.setSiteCode(adwebSites.getCode()).setActionType("add");
        this.saveProduct(product);
    }

    private void saveProduct(AdwebProduct product) {
        try {
            this.save(product);

            log.info("MQ消息新增产品成功：{}", product);
        } catch (Exception e) {
            log.error("MQ消息新增产品失败：{}", e.getMessage());
        }
    }
}
