package com.bonus.canteen.core.android.service.impl;

import com.bonus.canteen.core.android.domain.BaseSetting;
import com.bonus.canteen.core.android.domain.CarouselImage;
import com.bonus.canteen.core.android.enums.BasicSettingsEnum;
import com.bonus.canteen.core.android.mapper.BaseSettingMapper;
import com.bonus.canteen.core.android.service.BaseSettingService;
import com.bonus.common.core.web.domain.AjaxResult;
import org.springframework.stereotype.Service;

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

/**
 * @author xliu
 * @date 2025/5/7 15:46
 */
@Service
public class BaseSettingServiceImpl implements BaseSettingService {

    @Resource
    private BaseSettingMapper baseSettingMapper;

    @Override
    public AjaxResult addBaseSetting(BaseSetting baseSetting) {
//        List<BaseSetting> list = baseSettingMapper.getBaseSettingByKey(baseSetting.getKey());
//        int num = 0;
//        if(list !=null && list.size()>0){
//            num =  baseSettingMapper.updateBaseSettingByKey(baseSetting);
//        }else{
//            num =  baseSettingMapper.addBaseSetting(baseSetting);
//        }

        String key = BasicSettingsEnum.getKey(baseSetting.getKey());
        if(key !=null){
            baseSetting.setKey(key);
            int num = baseSettingMapper.addBaseSetting(baseSetting);
            if(num >0){
                return AjaxResult.success("添加成功");
            }else{
                return AjaxResult.error("添加失败");
            }
        }else{
           throw new RuntimeException("基础类型不存在");
        }
    }

    @Override
    public int updateBaseSettingByKey(BaseSetting baseSetting) {
        return baseSettingMapper.updateBaseSettingByKey(baseSetting);
    }

    @Override
    public int deleteBaseSettingByKey(BaseSetting baseSetting) {
        return baseSettingMapper.deleteBaseSettingByKey(baseSetting);
    }


    @Override
    public BaseSetting getBaseSettingByKey(BaseSetting baseSetting) {
        String key = BasicSettingsEnum.getKey(baseSetting.getKey());
        if(key !=null){
            baseSetting.setKey(key);
        }else{
            throw new RuntimeException("基础类型不存在");
        }
        BaseSetting base =  baseSettingMapper.getBaseSettingByKey(baseSetting);
        return base;
    }

    @Override
    public int addCarouseImage(CarouselImage carouselImage) {
        return baseSettingMapper.addCarouseImage(carouselImage);
    }

    @Override
    public int delCarouseImageById(CarouselImage carouselImage) {
        return baseSettingMapper.delCarouseImageById(carouselImage);
    }

    @Override
    public int updateCarouseImageById(CarouselImage carouselImage) {
        return baseSettingMapper.updateCarouseImageById(carouselImage);
    }

    @Override
    public List<CarouselImage> getCarouselImage(CarouselImage carouselImage) {
        return baseSettingMapper.getCarouselImage(carouselImage);
    }
}
