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

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.utils.Assert;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.sale.entity.after.dto.AfterSalesRecordSelectDto;
import com.fjhx.sale.entity.after.dto.AfterSalesRecordsDto;
import com.fjhx.sale.entity.after.po.AfterSalesRecords;
import com.fjhx.sale.entity.after.vo.AfterSalesRecordsVo;
import com.fjhx.sale.mapper.after.AfterSalesRecordsMapper;
import com.fjhx.sale.service.after.AfterSalesRecordsService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 售后跟进记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-07-20
 */
@Service
public class AfterSalesRecordsServicesImpl extends ServiceImpl<AfterSalesRecordsMapper, AfterSalesRecords> implements AfterSalesRecordsService {

    @Override
    public Page<AfterSalesRecordsVo> getPage(AfterSalesRecordSelectDto dto) {
        IWrapper<AfterSalesRecords> wrapper = getWrapper();
        Long afterSalesDetailId = dto.getAfterSalesDetailId();
        Assert.notEmpty(afterSalesDetailId, "售后明细id不能为空");
        wrapper.eq(AfterSalesRecordsVo::getAfterSalesDetailId, afterSalesDetailId);
        wrapper.orderByDesc("asr", AfterSalesRecords::getFollowUpTime);
        Page<AfterSalesRecordsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<AfterSalesRecordsVo> records = page.getRecords();
        //赋值跟进人名称
        UserUtil.assignmentNickName(records, AfterSalesRecordsVo::getFollowUpUserId, AfterSalesRecordsVo::setFollowUpUserName);
        return page;
    }

    @DSTransactional
    @Override
    public void add(AfterSalesRecordsDto afterSalesRecordDto) {
        this.save(afterSalesRecordDto);
        ObsFileUtil.saveFile(afterSalesRecordDto.getFileList(), afterSalesRecordDto.getId());
    }

}
