package com.aircraft.modules.aircraft.service.impl;

import com.aircraft.domain.dto.AttachmentMaterialDTO;
import com.aircraft.modules.aircraft.domain.AircraftInsurance;
import com.aircraft.modules.aircraft.mapper.AircraftInsuranceMapper;
import com.aircraft.modules.aircraft.service.AircraftInsuranceService;
import com.aircraft.modules.system.domain.AttachmentMaterial;
import com.aircraft.modules.system.service.IAttachmentMaterialService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 飞行器保险表 服务实现类
 * </p>
 *
 * @author cli
 * @since 2025-07-11
 */
@Service
@RequiredArgsConstructor
public class AircraftInsuranceServiceImpl extends ServiceImpl<AircraftInsuranceMapper, AircraftInsurance> implements AircraftInsuranceService {

    private final IAttachmentMaterialService attachmentMaterialService;

    //todo 枚举？
    private final String BUSINESS_TYPE = "aircraft_insurance";


    @Override
    public IPage<AircraftInsurance> page(Long aircraftId, Page<AircraftInsurance> page) {
        return lambdaQuery()
                .eq(AircraftInsurance::getAircraftId, aircraftId)
                .orderByDesc(AircraftInsurance::getId)
                .page(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(AircraftInsurance aircraftInsurance) {
        aircraftInsurance.setId(null);
        save(aircraftInsurance);
        //保存保险附件
        List<AttachmentMaterial> insuranceAttachment = aircraftInsurance.getInsuranceAttachment()
                .stream()
                .map(InsuranceImage -> {
                    AttachmentMaterial attachmentMaterial = new AttachmentMaterial();
                    BeanUtils.copyProperties(InsuranceImage, attachmentMaterial);
                    attachmentMaterial.setId(null);
                    attachmentMaterial.setBusinessId(aircraftInsurance.getId());
                    attachmentMaterial.setBusinessType(BUSINESS_TYPE);
                    return attachmentMaterial;
                }).toList();
        attachmentMaterialService.saveBatch(insuranceAttachment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AircraftInsurance aircraftInsurance) {
        updateById(aircraftInsurance);
        //保存保险附件
        List<AttachmentMaterial> insuranceAttachment = aircraftInsurance.getInsuranceAttachment()
                .stream()
                .map(InsuranceImage -> {
                    AttachmentMaterial attachmentMaterial = new AttachmentMaterial();
                    BeanUtils.copyProperties(InsuranceImage, attachmentMaterial);
                    attachmentMaterial.setBusinessId(aircraftInsurance.getId());
                    attachmentMaterial.setBusinessType(BUSINESS_TYPE);
                    return attachmentMaterial;
                }).toList();
        List<Long> ids = insuranceAttachment.stream().map(AttachmentMaterial::getId).toList();
        if (CollectionUtils.isNotEmpty(ids)) {
            //删除不在本次提交范围的附件
            attachmentMaterialService.remove(Wrappers.<AttachmentMaterial>lambdaQuery()
                    .eq(AttachmentMaterial::getBusinessType, BUSINESS_TYPE)
                    .eq(AttachmentMaterial::getBusinessId, aircraftInsurance.getId())
                    .notIn(AttachmentMaterial::getId, ids));
            //保存本次提交的附件
            attachmentMaterialService.saveOrUpdateBatch(insuranceAttachment);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        removeById(id);
        attachmentMaterialService.remove(Wrappers.<AttachmentMaterial>lambdaQuery()
                .eq(AttachmentMaterial::getBusinessType, BUSINESS_TYPE)
                .eq(AttachmentMaterial::getBusinessId, id));
    }

    @Override
    public AircraftInsurance findById(Long id) {
        AircraftInsurance aircraftInsurance = getById(id);
        if (null != aircraftInsurance) {
            List<AttachmentMaterialDTO> insuranceAttachment = attachmentMaterialService
                    .lambdaQuery()
                    .eq(AttachmentMaterial::getBusinessType, BUSINESS_TYPE)
                    .eq(AttachmentMaterial::getBusinessId, id)
                    .list()
                    .stream()
                    .map(attachmentMaterial -> {
                        AttachmentMaterialDTO localAttachmentMaterialDTO = new AttachmentMaterialDTO();
                        BeanUtils.copyProperties(attachmentMaterial, localAttachmentMaterialDTO);
                        return localAttachmentMaterialDTO;
                    }).toList();
            aircraftInsurance.setInsuranceAttachment(insuranceAttachment);
        }
        return aircraftInsurance;
    }
}
