package com.sunxung.factoring.service.settlement.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.dict.impl.SettlementDict;
import com.sunxung.factoring.entity.cashflowmanage.vo.BillGoodsInfoVo;
import com.sunxung.factoring.entity.project.vo.BusinessProcessingTaskVo;
import com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask;
import com.sunxung.factoring.entity.settlement.SettlementBasicInfo;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.settlement.SettlementBasicInfoMapper;
import com.sunxung.factoring.service.settlement.SettlementBasicInfoService;
import com.sunxung.factoring.service.settlement.SettlementBillUnitPriceAdjustServiceService;
import com.sunxung.factoring.service.sys.DictionaryService;
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.context.annotation.Primary;
import org.springframework.stereotype.Service;

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

/**
 * @author liutao
 * @date 2021.09.15
 */
@Service("billUnitPriceAdjustService")
public class SettlementBillUnitPriceAdjustServiceServiceImpl extends ServiceImpl<SettlementBasicInfoMapper, SettlementBasicInfo> implements SettlementBillUnitPriceAdjustServiceService {

    @Autowired
    private DictionaryService dictService;
    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;
    @Autowired
    private FileStorageService fileStorageService;

    @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<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<FileStorage> settlementAllFiles = new ArrayList<>();

        //下游结算单文件
        List<FileStorage> settlementFiles = sysAttachmentRefService.getFiles(AttachmentDto.builder()
                .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_FILE).setEntityId(id).build());
        settlementAllFiles.addAll(settlementFiles);

        //网价文件
        List<FileStorage> settlementNetPriceFile = sysAttachmentRefService.getFiles(AttachmentDto.builder()
                .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_NET_PRICE_FILE).setEntityId(id).build());
        settlementAllFiles.addAll(settlementNetPriceFile);

        //补充说明文件
        List<FileStorage> supplementFile = sysAttachmentRefService.getFiles(AttachmentDto.builder()
                .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_SUPPLEMENT_FILE).setEntityId(id).build());
        settlementAllFiles.addAll(supplementFile);
        SettlementBasicInfo settlementBasicInfo = get(id);
        if (settlementBasicInfo.getStatusDictionary().getCode().equals(SettlementDict.ChildEnum.SIGN.getCode())){
            //状态已签署 下载签署文件
            //上游签署文件
            List<FileStorage> upstreamSignedFile = sysAttachmentRefService.getFiles(AttachmentDto.builder()
                    .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_SIGNED_FILE).setEntityId(id).build());
            settlementAllFiles.add(upstreamSignedFile.get(0));
        }else {
            //状态为待签署 下载未签署文件
            //上游签署文件
            List<FileStorage> upstreamSignedFile = sysAttachmentRefService.getFiles(AttachmentDto.builder()
                    .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_UPSTREAM_UNSIGNED_FILE).setEntityId(id).build());
            settlementAllFiles.add(upstreamSignedFile.get(0));
        }
        //打包下载
        fileStorageService.renameFileStorageList(settlementAllFiles);
        fileStorageService.downloadZipFile(settlementAllFiles, FileModuleDict.ChildEnum.SETTLEMENT_ZIP_FILE.getCname(),
                FileModuleDict.ChildEnum.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);
        }
    }
}
