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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.CollectionUtil;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.settlement.SettlementBasicInfo;
import com.sunxung.factoring.entity.settlement.SettlementElectronicInfo;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.entity.sys.FileStorageDO;
import com.sunxung.factoring.mapper.settlement.SettlementElectronicInfoMapper;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.settlement.SettlementElectronicFlowService;
import com.sunxung.factoring.service.settlement.SettlementElectronicInfoService;
import com.sunxung.factoring.service.settlement.SettlementManagementService;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import com.sunxung.factoring.service.sys.FileService;
import com.sunxung.factoring.service.sys.FileStorageService;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liutao
 * @date 2022.3.17
 */
@Service("sign_settlementService")
public class SettlementElectronicFlowServiceImpl extends ServiceImpl<SettlementElectronicInfoMapper, SettlementElectronicInfo> implements SettlementElectronicFlowService {

    @Autowired
    private SettlementElectronicInfoService settlementElectronicInfoService;
    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;

    @Autowired
    private SettlementManagementService settlementManagementService;
    @Resource(name = "SupplierInfoService")
    private ISupplierInfoService iSupplierInfoService;
    @Autowired
    private IEnterpriseService iEnterpriseService;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private FileService fileService;


    public Object getView(Long SettlementElectronicInfoId) {
        SettlementElectronicInfo settlementElectronicInfo = settlementElectronicInfoService.getById(SettlementElectronicInfoId);
        //结算单已签署附件
//        AttachmentDto attachmentDtoSignedFile = AttachmentDto.builder()
//                .setEntityId(settlementElectronicInfo.getSettlementBasicInfoId())
//                .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_SIGNED_FILE)
//                .build();
//        List<FileStorage> signedFile = sysAttachmentRefService.getFiles(attachmentDtoSignedFile);
        List<FileStorageDO> signedFile = fileService.findByBusinessId(
            settlementElectronicInfo.getSettlementBasicInfoId(),
            FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_SIGNED_FILE.getCode());
        if (CollectionUtil.isNotEmpty(signedFile)) {
            settlementElectronicInfo.setSettlementSignedFile(signedFile.get(0));
        }
        SettlementBasicInfo settlementBasicInfo = settlementManagementService.getDetail(settlementElectronicInfo.getSettlementBasicInfoId());
        SupplierInfo supplierInfo = iSupplierInfoService.get(settlementBasicInfo.getSupplierId());
        Enterprise enterprise = iEnterpriseService.getById(supplierInfo.getcEnterpriseId());
        settlementElectronicInfo.setSupplierName( enterprise.getCorporateName());


        return settlementElectronicInfo;
    }
}
