package cn.iocoder.yudao.module.product.service.storecategory;

import cn.iocoder.yudao.module.product.dal.dataobject.storecategory.StoreProductCategoryDO;
import cn.iocoder.yudao.module.product.dal.mysql.storecategory.StoreProductCategoryMapper;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.product.controller.admin.storecategory.vo.StoreProductCategoryCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.storecategory.vo.StoreProductCategoryListReqVO;
import cn.iocoder.yudao.module.product.controller.admin.storecategory.vo.StoreProductCategoryUpdateReqVO;
import cn.iocoder.yudao.module.product.convert.storecategory.StoreProductCategoryConvert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Objects;

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


/**
 * 商品分类 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class StoreProductCategoryServiceImpl extends ServiceImpl<StoreProductCategoryMapper, StoreProductCategoryDO> implements StoreProductCategoryService {

    @Resource
    private StoreProductCategoryMapper storeProductCategoryMapper;


    @Override
    public Long createCategory(StoreProductCategoryCreateReqVO createReqVO) {
        // 校验父分类存在
        //validateParentProductCategory(createReqVO.getParentId());

        // 插入
        StoreProductCategoryDO category = StoreProductCategoryConvert.INSTANCE.convert(createReqVO);
        storeProductCategoryMapper.insert(category);
        // 返回
        return category.getId();
    }

    @Override
    public void updateCategory(StoreProductCategoryUpdateReqVO updateReqVO) {
        // 校验分类是否存在
        validateProductCategoryExists(updateReqVO.getId());
        // 校验父分类存在
       //validateParentProductCategory(updateReqVO.getParentId());

        // 更新
        StoreProductCategoryDO updateObj = StoreProductCategoryConvert.INSTANCE.convert(updateReqVO);
        storeProductCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteCategory(Long id) {
        // 校验分类是否存在
        validateProductCategoryExists(id);
        // 校验是否还有子分类
        if (storeProductCategoryMapper.selectCountByParentId(id) > 0) {
            throw exception(CATEGORY_EXISTS_CHILDREN);
        }
        // TODO yshop 补充只有不存在商品才可以删除
        // 删除
        storeProductCategoryMapper.deleteById(id);
    }

    private void validateParentProductCategory(Long id) {
        // 如果是根分类，无需验证
        if (Objects.equals(id, StoreProductCategoryDO.PARENT_ID_NULL)) {
            return;
        }
        // 父分类不存在
        StoreProductCategoryDO category = storeProductCategoryMapper.selectById(id);
        if (category == null) {
            throw exception(CATEGORY_PARENT_NOT_EXISTS);
        }

        StoreProductCategoryDO storeCategory = storeProductCategoryMapper.selectById(id);

        // 最多二级
        if (!Objects.equals(storeCategory.getParentId(), StoreProductCategoryDO.PARENT_ID_NULL)) {
            throw exception(CATEGORY_PARENT_NOT_FIRST_LEVEL);
        }
    }

    private void validateProductCategoryExists(Long id) {
        StoreProductCategoryDO category = storeProductCategoryMapper.selectById(id);
        if (category == null) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public StoreProductCategoryDO getCategory(Long id) {
        return storeProductCategoryMapper.selectById(id);
    }

    @Override
    public void validateCategory(Long id) {
        StoreProductCategoryDO category = storeProductCategoryMapper.selectById(id);
        if (category == null) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
        if (Objects.equals(category.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
            throw exception(CATEGORY_DISABLED, category.getName());
        }
    }

    @Override
    public Integer getCategoryLevel(Long id) {
        if (Objects.equals(id, StoreProductCategoryDO.PARENT_ID_NULL)) {
            return 0;
        }
        int level = 1;
        for (int i = 0; i < 100; i++) {
            StoreProductCategoryDO category = storeProductCategoryMapper.selectById(id);
            // 如果没有父节点，break 结束
            if (category == null
                    || Objects.equals(category.getParentId(), StoreProductCategoryDO.PARENT_ID_NULL)) {
                break;
            }
            // 继续递归父节点
            level++;
            id = category.getParentId();
        }
        return level;
    }

    @Override
    public List<StoreProductCategoryDO> getEnableCategoryList(StoreProductCategoryListReqVO listReqVO) {
        return storeProductCategoryMapper.selectList(listReqVO);
    }

    @Override
    public List<StoreProductCategoryDO> getEnableCategoryList(Integer shopId) {
        return storeProductCategoryMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus(),shopId);
    }

}
