package com.fjhx.sale.service.ext.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.sale.entity.ext.dto.ExtQuotationFollowDto;
import com.fjhx.sale.entity.ext.po.ExtQuotation;
import com.fjhx.sale.entity.ext.po.ExtQuotationFollow;
import com.fjhx.sale.entity.ext.vo.ExtQuotationFollowVo;
import com.fjhx.sale.mapper.ext.ExtQuotationFollowMapper;
import com.fjhx.sale.service.ext.ExtQuotationFollowService;
import com.fjhx.sale.service.ext.ExtQuotationService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * <p>
 * 对外报价单跟进记录 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-14
 */
@Service
public class ExtQuotationFollowServiceImpl extends ServiceImpl<ExtQuotationFollowMapper, ExtQuotationFollow> implements ExtQuotationFollowService {

    @Autowired
    private ExtQuotationService extQuotationService;

    @Override
    public ExtQuotationFollowVo detail(Long id) {
        ExtQuotationFollow ExtQuotationFollow = this.getById(id);
        ExtQuotationFollowVo result = BeanUtil.toBean(ExtQuotationFollow, ExtQuotationFollowVo.class);
        return result;
    }

    @Override
    @DSTransactional
    public void add(ExtQuotationFollowDto extQuotationFollowDto) {
        if (ObjectUtil.isEmpty(extQuotationFollowDto.getQuotationId())) {
            throw new ServiceException("报价单id不能为空！");
        }
        this.save(extQuotationFollowDto);

        updateLastFollowTime(extQuotationFollowDto.getQuotationId());
    }

    @Override
    @DSTransactional
    public void edit(ExtQuotationFollowDto extQuotationFollowDto) {
        Long id = extQuotationFollowDto.getId();
        ExtQuotationFollow extQuotationFollow = getById(id);
        if (ObjectUtil.isEmpty(extQuotationFollow)) {
            throw new ServiceException("查询不到跟进信息！");
        }
        this.updateById(extQuotationFollowDto);

        updateLastFollowTime(extQuotationFollow.getQuotationId());
    }

    @Override
    @DSTransactional
    public void delete(Long id) {
        ExtQuotationFollow extQuotationFollow = getById(id);
        if (ObjectUtil.isEmpty(extQuotationFollow)) {
            throw new ServiceException("查询不到跟进信息！");
        }
        this.removeById(id);
        updateLastFollowTime(extQuotationFollow.getQuotationId());
    }

    /**
     * 更新客户最后一次跟进时间
     */
    private synchronized void updateLastFollowTime(Long extQuotationId) {
        //获取跟进时间最新的一条
        ExtQuotationFollow extQuotationFollow = this.getOne(q -> q
                .eq(ExtQuotationFollow::getQuotationId, extQuotationId)
                .orderByDesc(ExtQuotationFollow::getFollowTime)
                .orderByDesc(ExtQuotationFollow::getId)
        );
        Date lastFollowTime = ObjectUtil.isNotEmpty(extQuotationFollow) ? extQuotationFollow.getFollowTime() : null;
        extQuotationService.update(q -> q
                .eq(ExtQuotation::getId, extQuotationId)
                .set(ExtQuotation::getLastFollowTime, lastFollowTime)
        );

    }

}
