package com.casic.missiles.service.Impl.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.core.model.auth.AuthUser;
import com.casic.missiles.core.shiro.ShiroKit;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.system.NoticeAddRequest;
import com.casic.missiles.dto.system.NoticeListRequest;
import com.casic.missiles.dto.system.NoticeReadRequest;
import com.casic.missiles.enums.PrefixCodeEnum;
import com.casic.missiles.mapper.system.SystemNoticeMapper;
import com.casic.missiles.mapper.system.SystemNoticeUserRelationMapper;
import com.casic.missiles.model.system.SystemNotice;
import com.casic.missiles.model.system.SystemNoticeUserRelation;
import com.casic.missiles.service.system.ISystemNoticeService;
import com.casic.missiles.service.system.ISystemNoticeUserRelationService;
import com.casic.missiles.utils.ConvertUtils;
import com.casic.missiles.utils.NumberGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系统管理-通知公告表 服务实现类
 * </p>
 *
 * @author wangpeng
 * @since 2022-11-29
 */
@Service
public class SystemNoticeServiceImpl extends ServiceImpl<SystemNoticeMapper, SystemNotice> implements ISystemNoticeService {
    @Autowired
    private SystemNoticeMapper noticeMapper;
    @Autowired
    private ISystemNoticeUserRelationService noticeUserRelationService;
    @Autowired
    private SystemNoticeUserRelationMapper noticeUserRelationMapper;

    @Override
    public Page<SystemNotice> listPage(Page<SystemNotice> page, NoticeListRequest request) {
        AuthUser user = ShiroKit.getUser();
        if(!Objects.isNull(user)){
            request.setUserId(user.getId());
        }
        Page<SystemNotice> noticePage = noticeMapper.selectListPage(page, request);
        return noticePage;
    }

    @Override
    public ReturnDTO addNotice(NoticeAddRequest noticeAddRequest) {
        //生成编号
        Long maxNo = noticeMapper.selectMaxNoticeNo();
        String noticeNo = NumberGeneratorUtil.getContactNo(PrefixCodeEnum.NOTICE_PREFIX, maxNo);
        noticeAddRequest.setNoticeNo(noticeNo);
        SystemNotice systemNotice = ConvertUtils.sourceToTarget(noticeAddRequest, SystemNotice.class);
        int insertFlag = noticeMapper.insert(systemNotice);
        if(insertFlag <= 0){
            return ReturnUtil.failed("新建失败");
        }
        return ReturnUtil.success();
    }

    @Override
    public ReturnDTO updateNotice(SystemNotice systemNotice) {
        int updateFlag = noticeMapper.updateById(systemNotice);
        if(updateFlag > 0){
            return ReturnUtil.success();
        }
        return ReturnUtil.failed("更新失败");
    }

    @Override
    public ReturnDTO deleteNotice(Long id) {
        if(noticeMapper.deleteById(id) > 0){
            QueryWrapper<SystemNoticeUserRelation> wrapper = new QueryWrapper<>();
            wrapper.eq("notice_id", id);
            noticeUserRelationMapper.delete(wrapper);
            return ReturnUtil.success();
        }
        return ReturnUtil.failed("刪除失败");
    }

    @Override
    public ReturnDTO noticeRead(NoticeReadRequest noticeReadRequest) {
        Long userId = noticeReadRequest.getUserId();
        List<Long> ids = noticeReadRequest.getIds();
        List<SystemNoticeUserRelation> list = new ArrayList<>();
        ids.forEach(id -> {
            SystemNoticeUserRelation noticeUserRelation = new SystemNoticeUserRelation();
            noticeUserRelation.setUserId(userId);
            noticeUserRelation.setNoticeId(id);
            list.add(noticeUserRelation);
        });
        if(noticeUserRelationService.saveBatch(list)){
            return ReturnUtil.success();
        }
        return ReturnUtil.failed();
    }
}
