package cn.iocoder.yudao.module.asset.service.prepare;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.asset.controller.admin.prepare.vo.VehiclePreparePageReqVO;
import cn.iocoder.yudao.module.asset.controller.admin.prepare.vo.VehiclePrepareSaveReqVO;
import cn.iocoder.yudao.module.asset.dal.dataobject.inspection.InspectionTemplateDO;
import cn.iocoder.yudao.module.asset.dal.dataobject.prepare.VehiclePrepareDO;
import cn.iocoder.yudao.module.asset.dal.mysql.contract.ContractMapper;
import cn.iocoder.yudao.module.asset.dal.mysql.prepare.VehiclePrepareMapper;
import cn.iocoder.yudao.module.asset.dal.mysql.vehicle.VehicleBaseMapper;
import cn.iocoder.yudao.module.asset.enums.inspection.InspectionSourceTypeEnum;
import cn.iocoder.yudao.module.asset.service.inspection.InspectionRecordService;
import cn.iocoder.yudao.module.asset.service.inspection.InspectionTemplateService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.asset.enums.ErrorCodeConstants.*;

@Service
@Validated
public class VehiclePrepareServiceImpl implements VehiclePrepareService {

    @Resource
    private VehiclePrepareMapper vehiclePrepareMapper;

    @Resource
    private ContractMapper contractMapper;

    @Resource
    private VehicleBaseMapper vehicleBaseMapper;

    @Resource
    private InspectionTemplateService inspectionTemplateService;

    @Resource
    private InspectionRecordService inspectionRecordService;

    @Override
    public Long createVehiclePrepare(VehiclePrepareSaveReqVO createReqVO) {
        // 校验合同是否存在
        if (createReqVO.getContractId() != null) {
            if (contractMapper.selectById(createReqVO.getContractId()) == null) {
                throw exception(CONTRACT_NOT_EXISTS);
            }
        }
        // 校验车辆是否存在
        if (createReqVO.getVehicleId() != null) {
            if (vehicleBaseMapper.selectById(createReqVO.getVehicleId()) == null) {
                throw exception(VEHICLE_NOT_EXISTS);
            }
        }

        VehiclePrepareDO prepare = BeanUtils.toBean(createReqVO, VehiclePrepareDO.class);
        vehiclePrepareMapper.insert(prepare);

        // 自动匹配验车模板并创建验车记录
        InspectionTemplateDO template = inspectionTemplateService.matchTemplate(
                InspectionSourceTypeEnum.PREPARE.getType(), prepare.getVehicleType());
        if (template != null) {
            Long inspectionRecordId = inspectionRecordService.createRecord(
                    template.getId(), InspectionSourceTypeEnum.PREPARE.getType(),
                    prepare.getId(), prepare.getVehicleId());
            // 回写验车记录ID
            vehiclePrepareMapper.updateById(VehiclePrepareDO.builder()
                    .id(prepare.getId()).inspectionRecordId(inspectionRecordId).build());
        }

        return prepare.getId();
    }

    @Override
    public void updateVehiclePrepare(VehiclePrepareSaveReqVO updateReqVO) {
        validateVehiclePrepareExists(updateReqVO.getId());
        VehiclePrepareDO updateObj = BeanUtils.toBean(updateReqVO, VehiclePrepareDO.class);
        vehiclePrepareMapper.updateById(updateObj);
    }

    @Override
    public void deleteVehiclePrepare(Long id) {
        validateVehiclePrepareExists(id);
        vehiclePrepareMapper.deleteById(id);
    }

    @Override
    public VehiclePrepareDO getVehiclePrepare(Long id) {
        return vehiclePrepareMapper.selectById(id);
    }

    @Override
    public PageResult<VehiclePrepareDO> getVehiclePreparePage(VehiclePreparePageReqVO pageReqVO) {
        return vehiclePrepareMapper.selectPage(pageReqVO);
    }

    @Override
    public void completeVehiclePrepare(Long id) {
        VehiclePrepareDO prepare = validateVehiclePrepareExists(id);
        if (prepare.getStatus().equals(1)) {
            throw exception(VEHICLE_PREPARE_ALREADY_COMPLETED);
        }
        vehiclePrepareMapper.updateById(VehiclePrepareDO.builder()
                .id(id).status(1).completeTime(LocalDateTime.now()).build());

        // 同时完成关联的验车记录
        if (prepare.getInspectionRecordId() != null) {
            inspectionRecordService.completeRecord(prepare.getInspectionRecordId(), null);
        }
    }

    private VehiclePrepareDO validateVehiclePrepareExists(Long id) {
        VehiclePrepareDO prepare = vehiclePrepareMapper.selectById(id);
        if (prepare == null) {
            throw exception(VEHICLE_PREPARE_NOT_EXISTS);
        }
        return prepare;
    }

}
