package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.ImGroupDao;
import com.yonge.cooleshow.biz.dal.dao.ImGroupNoticeDao;
import com.yonge.cooleshow.biz.dal.dto.ImGroupNoticeDto;
import com.yonge.cooleshow.biz.dal.entity.ImGroup;
import com.yonge.cooleshow.biz.dal.entity.ImGroupNotice;
import com.yonge.cooleshow.biz.dal.service.ImGroupNoticeService;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 群公告(ImGroupNotice)表服务实现类
 *
 * @author zx
 * @since 2022-03-22 10:45:59
 */
@Service("imGroupNoticeService")
public class ImGroupNoticeServiceImpl extends ServiceImpl<ImGroupNoticeDao, ImGroupNotice> implements ImGroupNoticeService {

    private final static Logger log = LoggerFactory.getLogger(ImGroupNoticeServiceImpl.class);

    @Resource
    private SysUserFeignService sysUserFeignService;
    @Resource
    private ImGroupDao imGroupDao;

    @Override
    public ImGroupNoticeDao getDao() {
        return this.baseMapper;
    }

    @Override
    public void create(ImGroupNotice imGroupNotice) {
        Long userId = this.checkAdmin(imGroupNotice.getGroupId());
        Date date = new Date();
        imGroupNotice.setOperatorId(userId);
        imGroupNotice.setCreateTime(date);
        imGroupNotice.setUpdateTime(date);
        baseMapper.insert(imGroupNotice);
    }

    @Override
    public void del(Long noticeId) {
        ImGroupNotice imGroupNotice = baseMapper.selectById(noticeId);
        if(Objects.isNull(imGroupNotice)){
            throw new BizException("群公告不存在");
        }
        this.checkAdmin(imGroupNotice.getGroupId());
        baseMapper.deleteById(noticeId);
    }

    @Override
    public PageInfo<ImGroupNoticeDto> queryPage(Map<String,Object> params) {
        Page<ImGroupNoticeDto> pageInfo = PageUtil.getPageInfo(params);
        pageInfo.setDesc("top_flag_","id_");
        return PageUtil.pageInfo(baseMapper.queryPage(pageInfo,params));
    }

    @Override
    public void modify(ImGroupNotice imGroupNotice) {
        if(Objects.isNull(baseMapper.selectById(imGroupNotice.getId()))){
            throw new BizException("群公告不存在");
        }
        Long userId = this.checkAdmin(imGroupNotice.getGroupId());
        Date date = new Date();
        imGroupNotice.setOperatorId(userId);
        imGroupNotice.setCreateTime(date);
        imGroupNotice.setUpdateTime(date);
        baseMapper.updateById(imGroupNotice);
    }

    public Long checkAdmin(String groupId){
        ImGroup imGroup = imGroupDao.selectById(groupId);
        if(Objects.isNull(imGroup)){
            throw new BizException("群信息不存在");
        }
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(!Objects.equals(sysUser.getId(),imGroup.getCreateBy())){
            throw new BizException("您没有操作权限");
        }
        return sysUser.getId();
    }

}

