package com.gci.boot.modules.vehicles.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.gci.boot.exception.GciBootException;
import com.gci.boot.modules.basedata.entity.TbVehicle;
import com.gci.boot.modules.basedata.service.ITbVehicleService;
import com.gci.boot.modules.vehicles.entity.TbAccidentInfo;
import com.gci.boot.modules.vehicles.entity.TbAccidentInsuranceInfo;
import com.gci.boot.modules.vehicles.entity.TbAccidentProgress;
import com.gci.boot.modules.vehicles.entity.TbInsuranceRecord;
import com.gci.boot.modules.vehicles.mapper.TbInsuranceRecordMapper;
import com.gci.boot.modules.vehicles.service.ITbAccidentInfoService;
import com.gci.boot.modules.vehicles.service.ITbAccidentInsuranceInfoService;
import com.gci.boot.modules.vehicles.service.ITbAccidentProgressService;
import com.gci.boot.modules.vehicles.service.ITbInsuranceRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 出险管理 服务实现类
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-22
 */
@Service
@Slf4j
public class TbInsuranceRecordServiceImpl extends ServiceImpl<TbInsuranceRecordMapper, TbInsuranceRecord> implements ITbInsuranceRecordService {

    @Autowired
    @Lazy
    private ITbAccidentInfoService accidentInfoService;
    @Autowired
    private ITbAccidentInsuranceInfoService accidentInsuranceInfoService;
    @Autowired
    private ITbAccidentProgressService accidentProgressService;
    @Autowired
    private ITbVehicleService vehicleService;

    @Override
    public void saveItem(TbInsuranceRecord item) {
        save(item);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateItem(TbInsuranceRecord item) {
        TbVehicle v = vehicleService.getById(item.getVehicleId());
        item.setVehicleType(v.getVehicleType());
        updateById(item);
        if (StrUtil.isNotBlank(item.getInfoId())) {
            TbAccidentInfo accidentInfo = accidentInfoService.getItemById(item.getInfoId());
            if (BeanUtil.isEmpty(accidentInfo)) {
                log.warn("出险已有事故id但是无法查询对应数据！infoId:{}",item.getInfoId());
            } else {
                TbAccidentInsuranceInfo accidentInsuranceInfo = accidentInfo.getAccidentInsuranceInfo();
                if (BeanUtil.isEmpty(accidentInsuranceInfo)) {
                    log.warn("出险已有事故id但是无法查询对应事故保险数据!infoId:{}",item.getInfoId());
                } else {
                    if (BeanUtil.isEmpty(v)) {
                        throw new GciBootException("车辆信息错误！");
                    }
                    accidentInfo.setPlateNumber(v.getPlateNumber());
                    accidentInfo.setProjectId(v.getProjectId());
                    accidentInfo.setProcessedPerson(item.getHandleUser());
                    accidentInfo.setReportNumber(item.getInsuranceNo());

                    String accidentInsuranceInfoId = accidentInsuranceInfo.getId();
                    BeanUtil.copyProperties(item,accidentInsuranceInfo);
                    accidentInsuranceInfo.setId(accidentInsuranceInfoId);
                    accidentInsuranceInfo.setDocumentImage(item.getCompensateUrl());
                    accidentInsuranceInfo.setTotalAmountActuallyIncurred(item.getInsurancePrice());

                    TbAccidentProgress accidentProgress = accidentInfo.getAccidentProgress();
                    if (BeanUtil.isEmpty(accidentProgress)) {
                        accidentProgress = new TbAccidentProgress();
                        accidentProgress.setInfoId(accidentInfo.getId());
                        accidentProgress.setAccidentCertificatePhoto(item.getAccidentUrl());
                        accidentProgress.setLossAssessmentPhoto(item.getLossAssessmentUrl());
                        accidentProgress.setCompensationAgreementPhoto(item.getCompensateAgreementUrl());
                        accidentProgress.setOtherPhoto(item.getOtherUrl());
                    } else {
                        accidentProgress.setInfoId(accidentInfo.getId());
                        accidentProgress.setAccidentCertificatePhoto(item.getAccidentUrl());
                        accidentProgress.setLossAssessmentPhoto(item.getLossAssessmentUrl());
                        accidentProgress.setCompensationAgreementPhoto(item.getCompensateAgreementUrl());
                        accidentProgress.setOtherPhoto(item.getOtherUrl());
                    }
                    accidentInfoService.updateById(accidentInfo);
                    accidentInsuranceInfoService.updateById(accidentInsuranceInfo);
                    if (StrUtil.isBlank(accidentProgress.getId())) {
                        accidentProgressService.save(accidentProgress);
                    } else {
                        accidentProgressService.updateById(accidentProgress);
                    }
                }
            }
        }
    }
}
