package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.bean.result.SysMessageCountResult;
import com.qmth.sop.business.bean.result.SysMessageResult;
import com.qmth.sop.business.bean.result.SysMessageWorkResult;
import com.qmth.sop.business.entity.*;
import com.qmth.sop.business.mapper.SysMessageMapper;
import com.qmth.sop.business.service.SysMessageService;
import com.qmth.sop.business.service.SysUserService;
import com.qmth.sop.business.service.TBServiceService;
import com.qmth.sop.business.service.TBUserArchivesSupplierService;
import com.qmth.sop.common.enums.JobQuartzTypeEnum;
import com.qmth.sop.common.enums.JobTypeEnum;
import com.qmth.sop.common.enums.MessageTypeEnum;
import com.qmth.sop.common.enums.NoticeTypeEnum;
import com.qmth.sop.common.util.ServletUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 系统消息 服务实现.
 *
 * @author: shudonghui
 * @date: 2023-08-07 13:53:29
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Service
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements SysMessageService {

    @Resource
    SysUserService sysUserService;

    @Resource
    TBUserArchivesSupplierService tbUserArchivesSupplierService;

    @Resource
    TBServiceService tbServiceService;

    @Override
    public IPage<SysMessageResult> query(IPage<Map> iPage, Long noticeId, String city, Long supplierId, Boolean status) {
        return this.baseMapper.query(iPage, noticeId, city, supplierId, status);
    }

    @Override
    public List<SysMessageResult> query(Long noticeId, String city, Long supplierId, Boolean status) {
        return this.baseMapper.query(noticeId, city, supplierId, status);
    }

    @Override
    public SysMessageCountResult count(long noticeId) {
        return this.baseMapper.count(noticeId);
    }

    @Override
    @Transactional
    public void deleteByNoticeId(long noticeId) {
        this.remove(new QueryWrapper<SysMessage>().lambda().eq(SysMessage::getNoticeId, noticeId));
    }

    @Override
    @Transactional
    public void publishByNotice(SysNotice sysNotice) {
        if (sysNotice.getType() == NoticeTypeEnum.SYSTEM) {
            List<SysUser> sysUsers = sysUserService.listEnable();
            sysUsers.forEach(s -> insertMessage(sysNotice, s.getId()));
        } else if (sysNotice.getType() == NoticeTypeEnum.SUPPLIER) {
            List<SysUser> sysUsers = tbUserArchivesSupplierService.findUsers(sysNotice.getSupplierId());
            sysUsers.forEach(s -> insertMessage(sysNotice, s.getId()));
        } else if (sysNotice.getType() == NoticeTypeEnum.SERVICE) {
            List<TBService> tbServices = tbServiceService.listEnable(sysNotice.getServiceId());
            tbServices.forEach(s -> insertMessage(sysNotice, s.getServiceLeadId()));
        }
    }

    /**
     * 保存消息
     *
     * @param sysMessageList
     */
    @Override
    @Transactional
    public void saveSysMessage(List<SysMessage> sysMessageList) {
        this.saveBatch(sysMessageList);
    }

    private void insertMessage(SysNotice sysNotice, Long s) {
        SysMessage sysMessage = new SysMessage();
        sysMessage.setContent(sysNotice.getContent());
        sysMessage.setCrmNo(null);
        sysMessage.setFormUserId(sysNotice.getCreateId());
        sysMessage.setNoticeId(sysNotice.getId());
        sysMessage.setReceiveUserId(s);
        sysMessage.setSendTime(System.currentTimeMillis());
        sysMessage.setServiceId(sysNotice.getServiceId());
        sysMessage.setStatus(false);
        sysMessage.setSupplierId(sysNotice.getSupplierId());
        sysMessage.setType(MessageTypeEnum.SYSTEM);
        this.save(sysMessage);
    }

    @Override
    @Transactional
    public boolean read(long id) {
        SysMessage sysMessage = this.getById(id);

        sysMessage.setStatus(true);
        sysMessage.setReceiveTime(System.currentTimeMillis());
        return this.updateById(sysMessage);

    }

    @Override
    public long countByTypes(MessageTypeEnum[] types) {
        //枚举数组转String数组
        String[] typeStr = new String[types.length];
        for (int i = 0; i < types.length; i++) {
            typeStr[i] = types[i].toString();
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return this.baseMapper.countByTypes(typeStr.length > 0 ? typeStr : null, sysUser.getId());
    }

    @Override
    public IPage<SysMessageWorkResult> pageByTypes(IPage<Map> iPage, MessageTypeEnum[] types, Boolean status, Long serviceId, String custom, String query, Long startTime, Long endTime) {
        String[] typeStr = new String[types.length];
        for (int i = 0; i < types.length; i++) {
            typeStr[i] = types[i].toString();
        }
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return this.baseMapper.pageByTypes(iPage, typeStr.length > 0 ? typeStr : null, status, serviceId, custom, query, startTime, endTime, sysUser.getId());
    }


}