package com.video.services.impl;

import com.video.mapper.SystemMenuMapper;
import com.video.model.pojo.SystemMenu;
import com.video.model.utils.CommonResult;
import com.video.model.vo.SystemMenuVo;
import com.video.services.SystemMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-03-30
 */
@Service
public class SystemMenuServiceImpl extends ServiceImpl<SystemMenuMapper, SystemMenu> implements SystemMenuService {

    @Autowired
    private SystemMenuMapper systemMenuMapper;

    /*
    查询获取所有菜单
    * */
    @Override
    @Cacheable(value = {"getSystemMenus"})
    public CommonResult<List<SystemMenu>> getSystemMenus() {
        List<SystemMenu> result = systemMenuMapper.selectList(null);
        return CommonResult.ok().setResult(result);
    }

    @Override
    //根据参数清除名为roomThirdSetting的缓存
    @CacheEvict(value = "getSystemMenus")
    public CommonResult insertSystemMenus(SystemMenu systemMenu) {
        int result = systemMenuMapper.insert(systemMenu);
        return result > 0 ? CommonResult.ok() : CommonResult.fail();
    }

    @Override
    public CommonResult deleteSystemMenu(Integer userID) {
        int result = systemMenuMapper.deleteById(Long.valueOf(userID));
        return result > 0 ? CommonResult.ok() : CommonResult.fail();
    }

    @Override
    public CommonResult<List<SystemMenu>> getSystemMenusByParentId(Long parentId) {
        HashMap<String, Object> map = new HashMap<>();
        // 自定义要查询
        map.put("menu_parent_id",parentId);

        List<SystemMenu> result = systemMenuMapper.selectByMap(map);

        return CommonResult.ok().setResult(result);
    }

    @Override
    @CacheEvict(value = "insertPlaybackRecord", allEntries = true)//allEntries清除整个缓存区
    public CommonResult<Integer> insertPlaybackRecord(SystemMenu systemMenu) {
        Integer result = systemMenuMapper.insert(systemMenu); // 帮我们自动生成id
        return CommonResult.ok().setResult(result);// 发现，id会自动回填
    }

    @Override
    @Cacheable("insertPlaybackRecord")
    public CommonResult<List<SystemMenu>> getPlaybackRecords() {
        List<SystemMenu> result = systemMenuMapper.selectList(null);
        return CommonResult.ok().setResult(result);
    }

    @Override
    public CommonResult<List<SystemMenuVo>> getPlaybackRecordBySql() {
        List<SystemMenuVo> result = systemMenuMapper.querySystemMenuAll();
        return CommonResult.ok().setResult(result);
    }

}
