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

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.vehiclemodel.vo.VehicleModelMaintainItemVO;
import cn.iocoder.yudao.module.asset.controller.admin.vehiclemodel.vo.VehicleModelPageReqVO;
import cn.iocoder.yudao.module.asset.controller.admin.vehiclemodel.vo.VehicleModelSaveReqVO;
import cn.iocoder.yudao.module.asset.dal.dataobject.vehiclemodel.VehicleModelDO;
import cn.iocoder.yudao.module.asset.dal.dataobject.vehiclemodel.VehicleModelMaintainItemDO;
import cn.iocoder.yudao.module.asset.dal.mysql.vehiclemodel.VehicleModelMapper;
import cn.iocoder.yudao.module.asset.dal.mysql.vehiclemodel.VehicleModelMaintainItemMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.List;

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

/**
 * 车型参数 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VehicleModelServiceImpl implements VehicleModelService {

    @Resource
    private VehicleModelMapper vehicleModelMapper;

    @Resource
    private VehicleModelMaintainItemMapper vehicleModelMaintainItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createVehicleModel(VehicleModelSaveReqVO createReqVO) {
        // 插入车型参数
        VehicleModelDO vehicleModel = BeanUtils.toBean(createReqVO, VehicleModelDO.class);
        vehicleModelMapper.insert(vehicleModel);
        
        // 插入维保项目
        saveMaintainItems(vehicleModel.getId(), createReqVO.getMaintainItems());
        
        // 返回
        return vehicleModel.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVehicleModel(VehicleModelSaveReqVO updateReqVO) {
        // 校验存在
        validateVehicleModelExists(updateReqVO.getId());
        
        // 更新车型参数
        VehicleModelDO updateObj = BeanUtils.toBean(updateReqVO, VehicleModelDO.class);
        vehicleModelMapper.updateById(updateObj);
        
        // 删除旧的维保项目
        vehicleModelMaintainItemMapper.deleteByVehicleModelId(updateReqVO.getId());
        
        // 插入新的维保项目
        saveMaintainItems(updateReqVO.getId(), updateReqVO.getMaintainItems());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVehicleModel(Long id) {
        // 校验存在
        validateVehicleModelExists(id);
        
        // 删除车型参数
        vehicleModelMapper.deleteById(id);
        
        // 删除维保项目
        vehicleModelMaintainItemMapper.deleteByVehicleModelId(id);
    }

    private void validateVehicleModelExists(Long id) {
        if (vehicleModelMapper.selectById(id) == null) {
            throw exception(VEHICLE_MODEL_NOT_EXISTS);
        }
    }

    @Override
    public VehicleModelDO getVehicleModel(Long id) {
        return vehicleModelMapper.selectById(id);
    }

    @Override
    public PageResult<VehicleModelDO> getVehicleModelPage(VehicleModelPageReqVO pageReqVO) {
        return vehicleModelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VehicleModelDO> getVehicleModelSimpleList() {
        return vehicleModelMapper.selectList();
    }

    @Override
    public List<VehicleModelDO> getVehicleModelListByBrand(String brand) {
        return vehicleModelMapper.selectListByBrand(brand);
    }

    @Override
    public List<VehicleModelDO> getVehicleModelListByModel(String model) {
        return vehicleModelMapper.selectListByModel(model);
    }

    @Override
    public List<VehicleModelMaintainItemDO> getVehicleModelMaintainItems(Long vehicleModelId) {
        return vehicleModelMaintainItemMapper.selectListByVehicleModelId(vehicleModelId);
    }

    @Override
    public List<String> getUsedMaintainItems() {
        return vehicleModelMaintainItemMapper.selectDistinctMaintainItems();
    }

    /**
     * 保存维保项目
     */
    private void saveMaintainItems(Long vehicleModelId, List<VehicleModelMaintainItemVO> maintainItems) {
        if (maintainItems == null || maintainItems.isEmpty()) {
            return;
        }

        for (VehicleModelMaintainItemVO item : maintainItems) {
            VehicleModelMaintainItemDO maintainItem = BeanUtils.toBean(item, VehicleModelMaintainItemDO.class);
            maintainItem.setVehicleModelId(vehicleModelId);

            // 自动计算 totalFee，处理 null 值
            BigDecimal hourFee = item.getHourFee() != null ? item.getHourFee() : BigDecimal.ZERO;
            BigDecimal materialFee = item.getMaterialFee() != null ? item.getMaterialFee() : BigDecimal.ZERO;
            maintainItem.setTotalFee(hourFee.add(materialFee));

            vehicleModelMaintainItemMapper.insert(maintainItem);
        }
    }

}
