package com.yida.data.system.service.impl;

import cc.mrbird.febs.common.redis.service.RedisService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.common.ResultMsgType;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.instructions.EduInstructionsContent;
import com.yida.data.system.mapper.EduInstructionsContentMapper;
import com.yida.data.system.service.EduInstructionsContentService;
import lombok.RequiredArgsConstructor;
import org.HdrHistogram.AtomicHistogram;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Service实现
 *
 * @author wjm
 * @date 2021-08-30 15:54:11
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduInstructionsContentServiceImpl extends ServiceImpl
        <EduInstructionsContentMapper, EduInstructionsContent> implements EduInstructionsContentService {

    @Resource
    private RedisService redisService;

    @Override
    public IPage<EduInstructionsContent> listPageEduInstructionsContent(Page page, String typeCode, String title) {
        IPage<EduInstructionsContent> eduInstructionsContentIPage = baseMapper.listPageEduInstructionsContent(page, typeCode, title);
        return eduInstructionsContentIPage;
    }

    @Override
    public boolean saveInstructionsContent(EduInstructionsContent eduInstructionsContent, CurrentUser currentUser) {

        EduInstructionsContent oneEduInstructionsContent = getOneEduInstructionsContent(eduInstructionsContent.getTypeCode(), null);

        if (ObjectUtils.isNotEmpty(oneEduInstructionsContent)) {
            return false;
        }
        if (ObjectUtils.isEmpty(eduInstructionsContent.getId())) {
            //新增
            eduInstructionsContent.setCreateDate(LocalDateTime.now());
            eduInstructionsContent.setCreateId(currentUser.getUserId());
        } else {
            //编辑
            eduInstructionsContent.setUpdateDate(LocalDateTime.now());
            eduInstructionsContent.setUpdateId(currentUser.getUserId());
        }
        this.saveOrUpdate(eduInstructionsContent);

        //编辑redis缓存
        if (redisService.hasKey(CachePrefixConstant.INSTRUCTIONS_CONTENT + eduInstructionsContent.getTypeCode())) {
            redisService.del(CachePrefixConstant.INSTRUCTIONS_CONTENT + eduInstructionsContent.getTypeCode());
            redisService.set(CachePrefixConstant.INSTRUCTIONS_CONTENT + eduInstructionsContent.getTypeCode(), eduInstructionsContent);
        }
        return true;
    }

    @Override
    public EduInstructionsContent getOneEduInstructionsContent(String typeCode, Integer id) {
        if (redisService.hasKey(CachePrefixConstant.INSTRUCTIONS_CONTENT + typeCode)) {
            EduInstructionsContent eduInstructionsContent = (EduInstructionsContent) redisService.get(CachePrefixConstant.INSTRUCTIONS_CONTENT + typeCode);
            return eduInstructionsContent;
        }
        if (ObjectUtils.isNotEmpty(typeCode)) {
            EduInstructionsContent one = this.getOne(Wrappers.<EduInstructionsContent>query().lambda().eq(EduInstructionsContent::getTypeCode, typeCode));
            redisService.set(CachePrefixConstant.INSTRUCTIONS_CONTENT + typeCode, one);
            return one;
        } else if (ObjectUtils.isNotEmpty(id)) {
            return this.getById(id);
        }
        return null;
    }

    @Override
    public Boolean deleteByIds(List<Integer> ids) {
        for (Integer id : ids) {
            EduInstructionsContent eduInstructionsContent = this.getById(id);
            if (redisService.hasKey(CachePrefixConstant.INSTRUCTIONS_CONTENT + eduInstructionsContent.getTypeCode())) {
                redisService.del(CachePrefixConstant.INSTRUCTIONS_CONTENT + eduInstructionsContent.getTypeCode());
            }
        }
        return this.removeByIds(ids);
    }
}
