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

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhy.module.core.controller.admin.resource.vo.ResourceCategorySaveReqVO;
import com.yhy.module.core.dal.dataobject.resource.ResourceCategoryDO;
import com.yhy.module.core.dal.mysql.resource.ResourceCategoryMapper;
import com.yhy.module.core.service.resource.ResourceCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
import static com.yhy.module.core.enums.ErrorCodeConstants.*;

@Service
public class ResourceCategoryServiceImpl implements ResourceCategoryService {

    @Resource
    private ResourceCategoryMapper categoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(ResourceCategorySaveReqVO reqVO) {
        Long tenantId = TenantContextHolder.getTenantId();
        
        // 检查代码唯一性
        validateCategoryCodeDuplicate(reqVO.getCode(), null, tenantId);

        ResourceCategoryDO category = BeanUtils.toBean(reqVO, ResourceCategoryDO.class);
        category.setTenantId(tenantId);
        categoryMapper.insert(category);
        return category.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ResourceCategorySaveReqVO reqVO) {
        // 校验类别存在
        validateCategoryExists(reqVO.getId());
        
        Long tenantId = TenantContextHolder.getTenantId();
        
        // 检查代码唯一性（排除自己）
        validateCategoryCodeDuplicate(reqVO.getCode(), reqVO.getId(), tenantId);

        ResourceCategoryDO category = BeanUtils.toBean(reqVO, ResourceCategoryDO.class);
        category.setTenantId(tenantId);
        categoryMapper.updateById(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        // 校验类别存在
        validateCategoryExists(id);
        
        // TODO: 检查是否有工料机项引用此类别
        // 如果有引用，应该禁止删除或提示用户
        
        categoryMapper.deleteById(id);
    }
    
    /**
     * 校验类别是否存在
     */
    private void validateCategoryExists(Long id) {
        if (categoryMapper.selectById(id) == null) {
            throw exception(RESOURCE_CATEGORY_NOT_EXISTS);
        }
    }
    
    /**
     * 校验类别代码是否重复
     */
    private void validateCategoryCodeDuplicate(String code, Long excludeId, Long tenantId) {
        ResourceCategoryDO category = categoryMapper.selectOne(new LambdaQueryWrapper<ResourceCategoryDO>()
                .eq(ResourceCategoryDO::getTenantId, tenantId)
                .eq(ResourceCategoryDO::getCode, code));
        if (category == null) {
            return;
        }
        // 如果 excludeId 为空，说明是新增，直接抛异常
        if (excludeId == null) {
            throw exception(RESOURCE_CATEGORY_CODE_DUPLICATE, code);
        }
        // 如果 ID 不同，说明代码重复
        if (!category.getId().equals(excludeId)) {
            throw exception(RESOURCE_CATEGORY_CODE_DUPLICATE, code);
        }
    }

    @Override
    public ResourceCategoryDO get(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public List<ResourceCategoryDO> list() {
        return categoryMapper.selectList(new LambdaQueryWrapper<ResourceCategoryDO>()
                .eq(ResourceCategoryDO::getTenantId, TenantContextHolder.getTenantId())
                .orderByAsc(ResourceCategoryDO::getSortOrder)
                .orderByAsc(ResourceCategoryDO::getId));
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void swapSortOrder(Long nodeId1, Long nodeId2) {
        Long tenantId = TenantContextHolder.getTenantId();
        
        // 1. 查询两个节点（使用行锁）
        ResourceCategoryDO node1 = categoryMapper.selectOne(
                new LambdaQueryWrapper<ResourceCategoryDO>()
                        .eq(ResourceCategoryDO::getId, nodeId1)
                        .eq(ResourceCategoryDO::getTenantId, tenantId)
                        .last("FOR UPDATE")
        );
        ResourceCategoryDO node2 = categoryMapper.selectOne(
                new LambdaQueryWrapper<ResourceCategoryDO>()
                        .eq(ResourceCategoryDO::getId, nodeId2)
                        .eq(ResourceCategoryDO::getTenantId, tenantId)
                        .last("FOR UPDATE")
        );
        
        if (node1 == null || node2 == null) {
            throw exception0(BAD_REQUEST.getCode(), "类别不存在");
        }
        
        // 2. 交换 sort_order
        Integer tempSortOrder = node1.getSortOrder();
        node1.setSortOrder(node2.getSortOrder());
        node2.setSortOrder(tempSortOrder);
        
        // 3. 更新数据库
        categoryMapper.updateById(node1);
        categoryMapper.updateById(node2);
    }
}
