package com.bonus.material.task;

import com.alibaba.fastjson2.JSONArray;
import com.bonus.common.biz.constant.MaterialConstants;
import com.bonus.common.biz.enums.MessageTypeEnum;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.SpringUtils;
import com.bonus.common.redis.service.RedisService;
import com.bonus.material.basic.domain.BmMessage;
import com.bonus.material.basic.mapper.BmMessageMapper;
import com.bonus.material.device.domain.MaDevQc;
import com.bonus.material.device.mapper.MaDevQcMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author：liang.chao
 * @Date：2024/12/20 - 19:53
 */
@Component
public class NoticeTask {
    @Resource
    private MaDevQcMapper maDevQcMapper;

    @Resource
    private BmMessageMapper bmMessageMapper;
    @Scheduled(cron = "0 10 0 * * ?")
    public void setNotice() {
        List<MaDevQc> qcList = maDevQcMapper.checkQcTime();
        if (qcList.size() > 0) {
            for (MaDevQc maDevQc : qcList) {
             /*   Notice notice = new Notice();
                notice.setNoticeType(1);
                notice.setNoticeTitle("关于"+maDevQc.getDeviceName()+"质检到期通知");
                notice.setNoticeContent("您好，系统检测出" + maDevQc.getDeviceName() + "已过质检日期，请及时上传质检报告");
                notice.setCreateBy("admin");
                notice.setCreateTime(new Date());
                notice.setOwnCo(Integer.parseInt(maDevQc.getQcCom()));
                noticeMapper.addNotice(notice);*/
                BmMessage bmMessage = new BmMessage();
                bmMessage.setCreateTime(DateUtils.getNowDate());
                try {
                    bmMessage.setMessageContent("您好，系统检测出" + maDevQc.getDeviceName() + "已过质检日期，请及时上传质检报告");
                    bmMessage.setMessageType(String.valueOf(MessageTypeEnum.QUALITY_CONTROL_MESSAGE.getType()));
                    bmMessage.setUuid(String.valueOf(UUID.randomUUID()));
                    bmMessage.setFromCompany(0L);
                    bmMessage.setFromUser(0L);
                    bmMessage.setFromCompanyName("系统");

                    bmMessage.setToCompanyName(maDevQc.getComName());
                    bmMessage.setToUser(Long.valueOf(maDevQc.getOwnId()));
                    bmMessage.setToCompany(Long.valueOf(maDevQc.getQcCom()));
                    bmMessage.setIsRead(0);

                    String msgKey = MaterialConstants.CACHE_MATERIAL_MALL_MESSAGE + bmMessage.getFromCompany() + ":" + bmMessage.getToCompany();
                    JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(msgKey);
                    if (arrayCache == null) {
                        arrayCache = new JSONArray();
                    }
                    List<BmMessage> list = arrayCache.toList(BmMessage.class);
                    list.add(bmMessage);
                    (SpringUtils.getBean(RedisService.class)).setCacheObject(msgKey, list, MaterialConstants.CACHE_MATERIAL_MALL_MESSAGE_HOURS, TimeUnit.HOURS);
                    bmMessageMapper.insertBmMessage(bmMessage);
                } catch (Exception e) {
                    throw new ServiceException("错误信息描述");
                }
            }
        }
    }
}
