package com.mosty.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.query.base.GetMyMessageQuery;
import com.mosty.base.model.dto.base.SysMessageInfoInsertDto;
import com.mosty.base.model.dto.base.SysMessageItemInsertDto;
import com.mosty.base.model.dto.base.SysMessageInfo;
import com.mosty.base.model.dto.base.SysMessageItem;
import com.mosty.base.mapper.SysMessageInfoMapper;
import com.mosty.base.mapper.SysMessageItemMapper;
import com.mosty.base.service.SysMessageService;
import com.mosty.common.base.util.IpUtil;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author dw
 * @since 2022/8/26
 **/
@Service
@AllArgsConstructor
public class SysMessageServiceImpl extends ServiceImpl<SysMessageInfoMapper, SysMessageInfo>
        implements SysMessageService {

    private final SysMessageItemMapper sysMessageItemMapper;

    @Override
    @Transactional
    public Integer sendMsg(SysMessageInfoInsertDto dto) {
        SysMessageInfo info = new SysMessageInfo();
        BeanUtils.copyProperties(dto, info);
        info.setXtCjsj(new Date());
        info.setXtLrsj(new Date());
        info.setXtLrrid("admin");
        info.setXtLrrxm("系统消息");
        info.setXtLrip(IpUtil.getIpAddress());
        info.setXtZhxgsj(new Date());
        info.setXtZhxgrid("admin");
        info.setXtZhxgip(IpUtil.getIpAddress());
        info.setXtZhxgrxm("系统消息");
        info.setXtZxbz(0);
        info.setXtZxyy(null);
        int count = this.baseMapper.insert(info);
        List<SysMessageItemInsertDto> itemList = dto.getItemList();
        itemList.forEach(itemDto -> {
            SysMessageItem item = new SysMessageItem();
            BeanUtils.copyProperties(itemDto, item);
            item.setMessageId(info.getId());
            item.setGroupId(info.getGroupId());
            item.setXtCjsj(new Date());
            item.setXtLrsj(new Date());
            item.setXtLrrid("admin");
            item.setXtLrrxm("系统消息");
            item.setXtLrip(IpUtil.getIpAddress());
            item.setXtZhxgsj(new Date());
            item.setXtZhxgrid("admin");
            item.setXtZhxgip(IpUtil.getIpAddress());
            item.setXtZhxgrxm("系统消息");
            item.setXtZxbz(0);
            item.setXtZxyy(null);
            this.sysMessageItemMapper.insert(item);
        });
        return count;
    }

    @Override
    public IPage<SysMessageInfo> getPageList(GetMyMessageQuery dto) {
        UserInfo user = UserInfoManager.get();
        IPage<SysMessageInfo> page = new Page<SysMessageInfo>(dto.getPageNum(), dto.getPageSize());
        return this.baseMapper.selectPage(page,
                new LambdaQueryWrapper<SysMessageInfo>()
                        .eq(SysMessageInfo::getXtZxbz, 0)
                        .last(" and id in (select a.message_id from sys_message_item a " +
                                "where a.xt_zxbz = 0 and a.xxjsr_id = '" + user.getUserId() + "') " +
                                "order by xt_cjsj desc")
        );
    }
}
