package com.sunxung.factoring.service.underlying.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.acceptance.AcceptanceGoodsInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.entity.underlying.UnderlyingHisAccountsReceivableInfo;
import com.sunxung.factoring.entity.underlying.UnderlyingHisGoodsInfo;
import com.sunxung.factoring.entity.underlying.UnderlyingHisInfo;
import com.sunxung.factoring.entity.underlying.vo.UnderlyingHisInfoVo;
import com.sunxung.factoring.mapper.underlying.UnderlyingHisInfoMapper;
import com.sunxung.factoring.service.acceptancesheet.AcceptanceGoodsInfoService;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import com.sunxung.factoring.service.underlying.UnderlyingHisAccountsReceivableInfoService;
import com.sunxung.factoring.service.underlying.UnderlyingHisGoodsInfoService;
import com.sunxung.factoring.service.underlying.UnderlyingHisInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liutao
 * @date 2021.10.11
 */
@Service("underlying_createService")
public class UnderlyingHisInfoServiceImpl extends ServiceImpl<UnderlyingHisInfoMapper, UnderlyingHisInfo>
        implements UnderlyingHisInfoService {

    @Autowired
    private AcceptanceGoodsInfoService acceptanceGoodsInfoService;

    @Autowired
    private UnderlyingHisGoodsInfoService underlyingHisGoodsInfoService;

    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;

    @Autowired
    private UnderlyingHisAccountsReceivableInfoService underlyingHisAccountsReceivableInfoService;

    @Override
    public UnderlyingHisInfoVo getView(Long id) {
        UnderlyingHisInfoVo underlyingHisInfoVo = new UnderlyingHisInfoVo();
        UnderlyingHisInfo underlyingHisInfo = baseMapper.getDetail(id);
        underlyingHisInfoVo.setUnderlyingHisInfo(underlyingHisInfo);
        if (underlyingHisInfo != null) {
            //获取货物信息
            List<UnderlyingHisGoodsInfo> hisGoodsInfoList = underlyingHisGoodsInfoService
                    .list(new QueryWrapper<UnderlyingHisGoodsInfo>().eq("underlying_his_info_id", underlyingHisInfo.getId()));

            if (hisGoodsInfoList != null && hisGoodsInfoList.size() > 0) {
                hisGoodsInfoList.stream().forEach(goods -> {
                    Long acceptanceGoodsInfoId = goods.getAcceptanceGoodsInfoId();
                    if (acceptanceGoodsInfoId != null) {
                        AcceptanceGoodsInfo acceptanceGoodsInfo = acceptanceGoodsInfoService.getById(acceptanceGoodsInfoId);
                        goods.setAcceptanceGoodsInfo(acceptanceGoodsInfo);
                    }
                });
            }
            underlyingHisInfoVo.setUnderlyingHisGoodsInfoList(hisGoodsInfoList);

            //获取网价附件
            AttachmentDto hisAttachmentDto = AttachmentDto.builder()
                    .setEntityId(underlyingHisInfo.getId())
                    .setChildEnum(FileModuleDict.ChildEnum.UNDERLYING_HIS_FILE)
                    .build();
            List<FileStorage> fileStorageList = sysAttachmentRefService.getFiles(hisAttachmentDto);
            underlyingHisInfoVo.setUnderlyingFileList(fileStorageList);

            //获取应收账款信息
            List<UnderlyingHisAccountsReceivableInfo> hisAccountsReceivableList = underlyingHisAccountsReceivableInfoService
                    .list(new QueryWrapper<UnderlyingHisAccountsReceivableInfo>().eq("underlying_his_info_id", underlyingHisInfo.getId()));
            underlyingHisInfoVo.setHisAccountsReceivableInfoList(hisAccountsReceivableList);
        }
        return underlyingHisInfoVo;
    }
}
