package com.inkasso.factoring.settlement.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.factoring.common.dict.impl.SettlementDict;
import com.inkasso.factoring.common.enums.ProjectInitiationStatusEnum;
import com.inkasso.factoring.dict.service.DictionaryService;
import com.inkasso.factoring.file.entity.FileStorageDO;
import com.inkasso.factoring.file.enums.FileTypeEnum;
import com.inkasso.factoring.file.service.FileService;
import com.inkasso.factoring.salesManagement.entity.CSalesContractBasicInfo;
import com.inkasso.factoring.salesManagement.entity.CSalesContractPersonInfo;
import com.inkasso.factoring.settlement.dao.SettlementBasicInfoMapper;
import com.inkasso.factoring.settlement.entity.SearchBusiness;
import com.inkasso.factoring.settlement.entity.SettlementBasicInfo;
import com.inkasso.factoring.settlement.service.SettlementBasicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SettlementBasicInfoServiceImpl extends ServiceImpl<SettlementBasicInfoMapper, SettlementBasicInfo>  implements SettlementBasicInfoService {
    @Autowired
    private DictionaryService dictService;
    @Autowired
    private FileService fileService;

    @Override
    public SettlementBasicInfo get(Long id) {
        return baseMapper.getListPage(id);
    }

    @Override
    public List<SettlementBasicInfo> findByBusinessNumber(String businessNumber) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("business_number", businessNumber);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<SettlementBasicInfo> findAllList(SearchBusiness search) {
        return null;
    }

//    @Override
//    public List<BusinessProcessingTaskVo> findMyCompletedList(SearchBusinessProcessingTask search) {
//        return baseMapper.findMyCompletedList(search);
//    }
//
//    @Override
//    public List<BusinessProcessingTaskVo> findAllList(SearchBusinessProcessingTask search) {
//        List<BusinessProcessingTaskVo> list = baseMapper.findAllList(search);
//        //判断变更按钮是否显示
//        if (list != null && list.size() > 0) {
//            list.stream().forEach(processingTaskVo -> autoButtonFlag(processingTaskVo)
//            );
//        }
//        return list;
//    }

//    @Override
//    public List<BillGoodsInfoVo> findRelationLedgerInfo(Long ledgerAccountBasicInfoId) {
//        return baseMapper.findRelationLedgerInfo(ledgerAccountBasicInfoId);
//    }
//
//    @Override
//    public List<BusinessProcessingTaskVo> findAllListSupplier(SearchBusinessProcessingTask search) {
//        return baseMapper.findAllListSupplier(search);
//    }

    @Override
    public void downloadAllSettlementFile(Long id, HttpServletResponse response) {
        List<FileStorageDO> settlementAllFiles = new ArrayList<>();

        //下游结算单文件
        List<FileStorageDO> settlementFiles = fileService.findByBusinessId(id, FileTypeEnum.SETTLEMENT_FILE.getCode());

        settlementAllFiles.addAll(settlementFiles);

        //网价文件
        List<FileStorageDO> settlementNetPriceFile = fileService.findByBusinessId(id, FileTypeEnum.SETTLEMENT_NET_PRICE_FILE.getCode());
        settlementAllFiles.addAll(settlementNetPriceFile);

        //补充说明文件
        List<FileStorageDO> supplementFile = fileService.findByBusinessId(id, FileTypeEnum.SETTLEMENT_SUPPLEMENT_FILE.getCode());
        settlementAllFiles.addAll(supplementFile);
        SettlementBasicInfo settlementBasicInfo = get(id);
        if (settlementBasicInfo.getSettlementStatus().equals(ProjectInitiationStatusEnum.PROCESSING.getCode())){
            //状态已签署 下载签署文件
            //上游签署文件
            List<FileStorageDO> upstreamSignedFile = fileService.findByBusinessId(id, FileTypeEnum.SETTLEMENT_UPSTREAM_SIGNED_FILE.getCode());
            settlementAllFiles.add(upstreamSignedFile.get(0));
        }else {
            //状态为待签署 下载未签署文件
            //上游签署文件
            List<FileStorageDO> upstreamSignedFile = fileService.findByBusinessId(id, FileTypeEnum.SETTLEMENT_UPSTREAM_UNSIGNED_FILE.getCode());
            settlementAllFiles.add(upstreamSignedFile.get(0));
        }
        //打包下载
        fileService.renameFileStorageList(settlementAllFiles);
        fileService.downloadZipFile(settlementAllFiles, FileTypeEnum.SETTLEMENT_ZIP_FILE.getDesc(),
                FileTypeEnum.SETTLEMENT_ZIP_FILE, response);
    }


    //填充是否展示下载文件按钮
//    private void autoButtonFlag(BusinessProcessingTaskVo processingTaskVo) {
//        Dictionary signDict = dictService.getByCode(SettlementDict.ChildEnum.SIGN.getCode());
//        Dictionary singedlDict = dictService.getByCode(SettlementDict.ChildEnum.NEED_SIGN.getCode());
//        processingTaskVo.setDownloadFileFlag(false);
//        if (signDict.getId().equals(processingTaskVo.getStatus().getId()) || singedlDict.getId().equals(processingTaskVo.getStatus().getId())) {
//            processingTaskVo.setDownloadFileFlag(true);
//        }
//    }
}
