package com.sckw.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sckw.goods.entity.KwBusinessCategory;
import com.sckw.goods.entity.req.CategoryInsertParam;
import com.sckw.goods.entity.req.CategoryUpdateParam;
import com.sckw.goods.entity.res.CategoryFindListRes;
import com.sckw.goods.mapper.CategoryMapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-04-26
 */
@Service
public class CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    /**
     * @Description: 分类查询
     * @Author: Lt
     * @Date: 2024/4/26 15:37
     */
    public List<CategoryFindListRes> findList(String name)
    {
        return categoryMapper.selectList(name);
    }

    /**
    * @Description: 分类添加
    * @Author: Lt
    * @Date: 2024/4/26 16:35
    */
    public int insert(CategoryInsertParam categoryInsertParam)
    {
        LambdaQueryWrapper<KwBusinessCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwBusinessCategory::getCategoryName, categoryInsertParam.getCategoryName())
                .eq(KwBusinessCategory::getDelFlag, Global.NUMERICAL_ZERO);
        Long l = categoryMapper.selectCount(wrapper);

        if (l > 0){
            throw new RuntimeException("分类名称已存在");
        }
        KwBusinessCategory insertParam = BeanUtils.copyProperties(categoryInsertParam, KwBusinessCategory.class);
        return categoryMapper.insert(insertParam);
    }

    /**
    * @Description: 分类编辑
    * @Author: Lt
    * @Date: 2024/4/26 16:51
    */
    public int update(CategoryUpdateParam categoryUpdateParam)
    {
        LambdaQueryWrapper<KwBusinessCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(KwBusinessCategory::getId, categoryUpdateParam.getId())
                .eq(KwBusinessCategory::getCategoryName, categoryUpdateParam.getCategoryName())
                .eq(KwBusinessCategory::getDelFlag, Global.NUMERICAL_ZERO);
        Long l = categoryMapper.selectCount(wrapper);

        if (l > 0){
            throw new RuntimeException("分类名称已存在");
        }
        KwBusinessCategory updateParam = BeanUtils.copyProperties(categoryUpdateParam, KwBusinessCategory.class);
        return categoryMapper.updateById(updateParam);
    }

    /**
    * @Description: 分类删除
    * @Author: Lt
    * @Date: 2024/4/26 16:59
    */
    public int delete(List<Long> ids)
    {
        LambdaUpdateWrapper<KwBusinessCategory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(KwBusinessCategory::getDelFlag, Global.YES).in(KwBusinessCategory::getId, ids);
        return categoryMapper.update(null, updateWrapper);
    }




}
