package com.fjhx.customer.service.customer.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.customer.entity.customer.dto.CustomerFollowRecordsDto;
import com.fjhx.customer.entity.customer.dto.CustomerFollowRecordsSelectDto;
import com.fjhx.customer.entity.customer.po.CustomerFollowRecords;
import com.fjhx.customer.entity.customer.vo.CustomerFollowRecordsVo;
import com.fjhx.customer.mapper.customer.CustomerFollowRecordsMapper;
import com.fjhx.customer.service.customer.CustomerFollowRecordsService;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.file.entity.FileInfo;
import com.fjhx.file.entity.ObsFile;
import com.fjhx.file.service.FileInfoService;
import com.fjhx.file.utils.ObsFileUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 客户跟进记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-05
 */
@Service
public class CustomerFollowRecordsServiceImpl extends ServiceImpl<CustomerFollowRecordsMapper, CustomerFollowRecords> implements CustomerFollowRecordsService {

    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private CustomerService customerService;

    @Override
    public Page<CustomerFollowRecordsVo> getPage(CustomerFollowRecordsSelectDto dto) {
        IWrapper<CustomerFollowRecords> wrapper = getWrapper();
//        wrapper.eq("cfr", CustomerFollowRecords::getCustomerId, dto.getCustomerId());//根据客户id过滤
        wrapper.orderByDesc("t1", CustomerFollowRecords::getDate);
        Page<CustomerFollowRecordsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper, dto.getCustomerId(), SecurityUtils.getTenantId());
        List<CustomerFollowRecordsVo> records = page.getRecords();
        //赋值文件列表信息
        List<Long> ids = records.stream().map(CustomerFollowRecordsVo::getId).filter(ObjectUtil::isNotNull).distinct().collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(ids)) {
            DynamicDataSourceContextHolder.push(SourceConstant.BASE);
            Map<Long, List<FileInfo>> longListMap = fileInfoService.mapKGroup(FileInfo::getBusinessId, q -> q.in(FileInfo::getBusinessId, ids));
            for (CustomerFollowRecordsVo record : records) {
                List<ObsFile> obsFiles = BeanUtil.copyToList(longListMap.get(record.getId()), ObsFile.class);
                record.setFileList(obsFiles);
            }
        }

        //赋值跟进人名称
        UserUtil.assignmentNickName(records, CustomerFollowRecordsVo::getCreateUser, CustomerFollowRecordsVo::setFollowUpUserName);

        //赋值客户名称
        customerService.attributeAssign(records, CustomerFollowRecordsVo::getCustomerId, (item, customer) -> {
            item.setCustomerName(customer.getName());
        });

        return page;
    }

    @Override
    public List<CustomerFollowRecordsVo> followList(Long customerId){
        if(ObjectUtil.isEmpty(customerId)){
            throw new ServiceException("客户id不能为空");
        }
        List<CustomerFollowRecords> customerFollowRecords = list(q->q.eq(CustomerFollowRecords::getCustomerId,customerId)
                .orderByDesc(CustomerFollowRecords::getDate));
        List<CustomerFollowRecordsVo> customerFollowRecordsVos = BeanUtil.copyToList(customerFollowRecords, CustomerFollowRecordsVo.class);
        //赋值文件列表信息
        List<Long> ids = customerFollowRecordsVos.stream().map(CustomerFollowRecordsVo::getId).filter(ObjectUtil::isNotNull).distinct().collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(ids)) {
            DynamicDataSourceContextHolder.push(SourceConstant.BASE);
            Map<Long, List<FileInfo>> longListMap = fileInfoService.mapKGroup(FileInfo::getBusinessId, q -> q.in(FileInfo::getBusinessId, ids));
            for (CustomerFollowRecordsVo record : customerFollowRecordsVos) {
                List<ObsFile> obsFiles = BeanUtil.copyToList(longListMap.get(record.getId()), ObsFile.class);
                record.setFileList(obsFiles);
            }
        }
        return customerFollowRecordsVos;
    }

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

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

    @DSTransactional
    @Override
    public void edit(CustomerFollowRecordsDto customerFollowRecordsDto) {
        this.updateById(customerFollowRecordsDto);
        ObsFileUtil.editFile(customerFollowRecordsDto.getFileList(), customerFollowRecordsDto.getId());
    }

    @DSTransactional
    @Override
    public void delete(Long id) {
        this.removeById(id);
        ObsFileUtil.removeFile(id);
    }

}
