package org.jeecg.modules.shop.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.jeecg.modules.sequence.service.IMiddleSequenceService;
import org.jeecg.modules.shop.customer.entity.PartnerCustomer;
import org.jeecg.modules.shop.customer.entity.PartnerCustomerLinkman;
import org.jeecg.modules.shop.customer.entity.dto.PartnerCustomerDTO;
import org.jeecg.modules.shop.customer.mapper.PartnerCustomerMapper;
import org.jeecg.modules.shop.customer.queue.RabbitCustomer;
import org.jeecg.modules.shop.customer.service.IPartnerCustomerLinkmanService;
import org.jeecg.modules.shop.customer.service.IPartnerCustomerService;
import org.jeecg.modules.shop.customer.service.IPartnerCustomerTimelineService;
import org.jeecg.modules.shop.user.util.partnerUserHolder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.Part;

/**
 * @Description: partner_customer
 * @Author: jeecg-boot
 * @Date:   2025-07-11
 * @Version: V1.0
 */
@Service
public class PartnerCustomerServiceImpl extends ServiceImpl<PartnerCustomerMapper, PartnerCustomer> implements IPartnerCustomerService {

    @Autowired
    private  IPartnerCustomerLinkmanService linkmanService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private  IMiddleSequenceService middleSequenceService;

    @Override
    @Transactional
    public void addCustomer(PartnerCustomerDTO dto) {
        PartnerCustomer cu = BeanUtil.copyProperties(dto, PartnerCustomer.class);
        cu.setId(String.valueOf(IdWorker.getId()));
        cu.setUserId(partnerUserHolder.currentLoginUserId());
        cu.setBillNo(middleSequenceService.generate("PCU",6));
        this.save( cu);
        if (CollectionUtil.isNotEmpty(dto.getLinkmanList())){
            dto.getLinkmanList().forEach(linkman -> {
                linkman.setCustomerId(cu.getId());
                linkman.setFullName(linkman.getFirstName()+ linkman.getLastName());
            });
            linkmanService.saveBatch(dto.getLinkmanList());
        }
        //mq 发送消息把dto推到中台去
        rabbitTemplate.convertAndSend(RabbitCustomer.ADDCUSTOMER_EXCHANGE, RabbitCustomer.ADDCUSTOMER_ROUTE, JSONUtil.toJsonStr(dto));
    }

    @Override
    public void updatecustomer(PartnerCustomerDTO dto) {
        linkmanService.remove(new QueryWrapper<PartnerCustomerLinkman>().eq("customer_id", dto.getId()));
        PartnerCustomer cu = this.getById(dto.getId());
        BeanUtil.copyProperties(dto, cu);
        this.updateById(cu);
        linkmanService.saveOrUpdateBatch(dto.getLinkmanList());
        PartnerCustomerDTO d2 = BeanUtil.copyProperties(cu, PartnerCustomerDTO.class);
        d2.setLinkmanList(dto.getLinkmanList());
        rabbitTemplate.convertAndSend(RabbitCustomer.UPDATECUSTOMER_EXCHANGE, RabbitCustomer.UPDATECUSTOMER_ROUTE, JSONUtil.toJsonStr(d2));
    }

}
