package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.finance.convert.AccountStatementAttachmentConvert;
import com.jwipc.finance.entity.AccountStatementAttachment;
import com.jwipc.finance.logic.AccountStatementAttachmentLogic;
import com.jwipc.finance.model.po.AccountStatementAttachmentPO;
import com.jwipc.finance.model.query.AccountStatementAttachmentQuery;
import com.jwipc.finance.service.IAccountStatementAttachmentService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.sys.dto.BusinessAttachmentDTO;
import com.jwipc.sys.logic.BusinessAttachmentLogic;
import com.jwipc.sys.model.po.BusinessAttachmentPO;
import com.jwipc.sys.model.query.BusinessAttachmentQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shenyang.ou
 * @date 2025/10/16 11:23
 * @desc
 */
@Service
@RequiredArgsConstructor
public class AccountStatementAttachmentLogicImpl implements AccountStatementAttachmentLogic {
    private final IAccountStatementAttachmentService accountStatementAttachmentService;
    private final BusinessAttachmentLogic businessAttachmentLogic;
    private final AccountStatementAttachmentConvert accountStatementAttachmentConvert;

    @Override
    public PageView list(AccountStatementAttachmentQuery query) {
        List<AccountStatementAttachmentPO> list = accountStatementAttachmentService.queryByPage(null, query);
        if (query.getReloadFlag()!=null && query.getReloadFlag()==1){
            BusinessAttachmentQuery businessAttachmentQuery = new BusinessAttachmentQuery();
            businessAttachmentQuery.setBusinessCodes(query.getBusinessCodes());
            businessAttachmentQuery.setBusinessTypeCodes(query.getBusinessTypeCodes());
            PageView businessAttachmentPageView = businessAttachmentLogic.listNoPageByCodes(businessAttachmentQuery);
            List<BusinessAttachmentDTO> businessAttachmentRows = (List<BusinessAttachmentDTO>) businessAttachmentPageView.getRows();
            //list和businessAttachmentRows进行对比，将businessAttachmentRows中存在的但list中缺少的添加到list中
            for (BusinessAttachmentDTO businessAttachmentDTO : businessAttachmentRows) {
                boolean exist = list.stream().anyMatch(item -> item.getBusinessAttachmentId().equals(businessAttachmentDTO.getId()));
                if (!exist) {
                    AccountStatementAttachmentPO po = new AccountStatementAttachmentPO();
                    po.setBusinessId(businessAttachmentDTO.getBusinessId());
                    po.setBusinessAttachmentId(businessAttachmentDTO.getId());
                    po.setBusinessCode(businessAttachmentDTO.getBusinessCode());
                    po.setBusinessTypeCode(businessAttachmentDTO.getBusinessTypeCode());
                    po.setSignStatus(businessAttachmentDTO.getSignStatus());
                    po.setSignOrder(businessAttachmentDTO.getSignOrder());
                    po.setSignatureType(businessAttachmentDTO.getSignatureType());
//                    po.setImageFileId(businessAttachmentDTO.getImageFileId());
//                    po.setImagePath(businessAttachmentDTO.getImagePath());
//                    po.setImageCode(businessAttachmentDTO.getImageCode());
                    po.setRemark(businessAttachmentDTO.getRemark());
                    po.setType(businessAttachmentDTO.getType());
                    po.setFileId(businessAttachmentDTO.getFileId());
                    po.setFileName(businessAttachmentDTO.getFileName());
                    list.add(po);
                }
            }
        }
        return PageUtil.getPV(accountStatementAttachmentConvert.poList2DTOList(list), list.size());
    }

    /**
     * 根据对账单id集合删除附件
     *
     * @param asIdList 对账单id集合
     */
    @Override
    public void deleteByAsIdList(List<Long> asIdList) {
        asIdList.forEach(asId -> {
            accountStatementAttachmentService.remove(new LambdaQueryWrapper<AccountStatementAttachment>().eq(AccountStatementAttachment::getAsId, asId));
        });

    }
}
