package com.yhy.module.core.service.quota.impl;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yhy.module.core.enums.ErrorCodeConstants.QUOTA_ADJUSTMENT_SETTING_HAS_DETAILS;
import static com.yhy.module.core.enums.ErrorCodeConstants.QUOTA_ADJUSTMENT_SETTING_NOT_EXISTS;
import static com.yhy.module.core.enums.ErrorCodeConstants.QUOTA_ADJUSTMENT_SETTING_NOT_SAME_QUOTA_ITEM;
import static com.yhy.module.core.enums.ErrorCodeConstants.QUOTA_ADJUSTMENT_SETTING_QUOTA_ITEM_NOT_EXISTS;
import static com.yhy.module.core.enums.ErrorCodeConstants.QUOTA_ADJUSTMENT_SETTING_REFERENCED;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.yhy.module.core.controller.admin.quota.vo.QuotaAdjustmentSettingSaveReqVO;
import com.yhy.module.core.dal.dataobject.quota.QuotaAdjustmentSettingDO;
import com.yhy.module.core.dal.mysql.quota.QuotaAdjustmentDetailMapper;
import com.yhy.module.core.dal.mysql.quota.QuotaAdjustmentSettingMapper;
import com.yhy.module.core.dal.mysql.quota.QuotaItemMapper;
import com.yhy.module.core.service.quota.QuotaAdjustmentSettingService;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

/**
 * 定额调整设置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class QuotaAdjustmentSettingServiceImpl implements QuotaAdjustmentSettingService {

    @Resource
    private QuotaAdjustmentSettingMapper quotaAdjustmentSettingMapper;

    @Resource
    private QuotaAdjustmentDetailMapper quotaAdjustmentDetailMapper;

    @Resource
    private QuotaItemMapper quotaItemMapper;

    @Override
    public Long createQuotaAdjustmentSetting(QuotaAdjustmentSettingSaveReqVO createReqVO) {
        // 验证定额子目是否存在
        validateQuotaItemExists(createReqVO.getQuotaItemId());

        // 插入
        QuotaAdjustmentSettingDO setting = BeanUtils.toBean(createReqVO, QuotaAdjustmentSettingDO.class);
        if (setting.getSortOrder() == null) {
            setting.setSortOrder(0);
        }
        quotaAdjustmentSettingMapper.insert(setting);
        return setting.getId();
    }

    @Override
    public void updateQuotaAdjustmentSetting(QuotaAdjustmentSettingSaveReqVO updateReqVO) {
        // 校验存在
        validateQuotaAdjustmentSettingExists(updateReqVO.getId());
        // 验证定额子目是否存在
        validateQuotaItemExists(updateReqVO.getQuotaItemId());

        // 更新
        QuotaAdjustmentSettingDO updateObj = BeanUtils.toBean(updateReqVO, QuotaAdjustmentSettingDO.class);
        quotaAdjustmentSettingMapper.updateById(updateObj);
    }

    @Override
    public void deleteQuotaAdjustmentSetting(Long id) {
        // 校验存在
        validateQuotaAdjustmentSettingExists(id);
        
        // 检查是否有明细
        Long detailCount = quotaAdjustmentDetailMapper.countBySettingId(id);
        if (detailCount > 0) {
            throw exception(QUOTA_ADJUSTMENT_SETTING_HAS_DETAILS);
        }
        
        // 检查是否被其他明细引用
        Long referenceCount = quotaAdjustmentDetailMapper.countByAdjustmentCode(id);
        if (referenceCount > 0) {
            throw exception(QUOTA_ADJUSTMENT_SETTING_REFERENCED);
        }

        // 删除
        quotaAdjustmentSettingMapper.deleteById(id);
    }

    @Override
    public QuotaAdjustmentSettingDO getQuotaAdjustmentSetting(Long id) {
        return quotaAdjustmentSettingMapper.selectById(id);
    }

    @Override
    public List<QuotaAdjustmentSettingDO> getQuotaAdjustmentSettingList(Long quotaItemId) {
        return quotaAdjustmentSettingMapper.selectListByQuotaItemId(quotaItemId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void swapSort(Long id1, Long id2) {
        // 验证两个调整设置是否存在
        QuotaAdjustmentSettingDO setting1 = quotaAdjustmentSettingMapper.selectByIdForUpdate(id1);
        QuotaAdjustmentSettingDO setting2 = quotaAdjustmentSettingMapper.selectByIdForUpdate(id2);

        if (setting1 == null) {
            throw exception(QUOTA_ADJUSTMENT_SETTING_NOT_EXISTS);
        }
        if (setting2 == null) {
            throw exception(QUOTA_ADJUSTMENT_SETTING_NOT_EXISTS);
        }

        // 验证是否在同一定额子目下
        if (!setting1.getQuotaItemId().equals(setting2.getQuotaItemId())) {
            throw exception(QUOTA_ADJUSTMENT_SETTING_NOT_SAME_QUOTA_ITEM);
        }

        // 交换排序值
        Integer tempSort = setting1.getSortOrder();
        setting1.setSortOrder(setting2.getSortOrder());
        setting2.setSortOrder(tempSort);

        quotaAdjustmentSettingMapper.updateById(setting1);
        quotaAdjustmentSettingMapper.updateById(setting2);
    }

    private void validateQuotaAdjustmentSettingExists(Long id) {
        if (quotaAdjustmentSettingMapper.selectById(id) == null) {
            throw exception(QUOTA_ADJUSTMENT_SETTING_NOT_EXISTS);
        }
    }

    private void validateQuotaItemExists(Long quotaItemId) {
        if (quotaItemMapper.selectById(quotaItemId) == null) {
            throw exception(QUOTA_ADJUSTMENT_SETTING_QUOTA_ITEM_NOT_EXISTS);
        }
    }

}
