package com.zcloud.service.announcement.impl;

import com.zcloud.entity.PageData;
import com.zcloud.service.announcement.TemplateAnnounService;
import com.zcloud.service.notice.NoticeCorpService;
import com.zcloud.service.notice.NoticeCorpUserService;
import com.zcloud.service.notice.NoticeTemplateService;
import com.zcloud.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional //开启事物
public class TemplateAnnounServiceImpl  implements TemplateAnnounService {
    @Autowired
    private NoticeTemplateService noticeTemplateService;
    @Autowired
    private NoticeCorpService noticeCorpService;
    @Autowired
    private NoticeCorpUserService noticeCorpUserService;


    /**
     *HIDDEN_MAIL_001
     * 隐患提示，有一条隐患描述为${hiddendescr}的隐患，在${params2}流程待您${params3}。
     */
    /**
     *  发送通知公告根据模板信息
     * @param templateBianma 模板编码
     * @param noticeUserId 发送人id
     * @param SYNOPSIS SYNOPSIS
     * @param notRepr 模板 信息
     */
    @Override
    public void sendOutNoticeAnnouncement(String templateBianma,String noticeUserId,String SYNOPSIS, Map<String,Object> notRepr ) {
        /**    根据编码获取通知公告模板  */
        try{
            PageData notice =  noticeTemplateService.findByBianMa(templateBianma);
            String content ="暂无";
            if(Tools.isEmpty(notice)){
                ResultCodeConstant.resMap(000301); //没有维护公告模板
            }
            content  = notice.getString("CONTENT");
            String[] temp = content.split("}"); //分割公告模板，知道公告模板有几个参数
            if((temp.length-1)!=notRepr.size()){
                ResultCodeConstant.resMap(000302); //公告模板 维护错误，没有正确的参数
            }else{
                for(Object key : notRepr.keySet()){	//替换占位符
                    content = content.replace("${" + key.toString() + "}", notRepr.get(key).toString());
                }
            }


            PageData pd1 = new PageData();
            pd1.put("NOTICECORP_ID", UuidUtil.get32UUID());
            pd1.put("USERNAME",Jurisdiction.getUsername()); //添加人
            pd1.put("CREATOR", Jurisdiction.getUSER_ID());	//添加人
            pd1.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
            pd1.put("SYNOPSIS",SYNOPSIS); //公告简介
            pd1.put("CONTENT","<p>"+content+"</p>");//内容
            pd1.put("ISALL","yes");
            pd1.put("ISREPLY",0);
            pd1.put("ISDELETE",0);
            pd1.put("ISTOP",0);
            pd1.put("STATUS",1);
            pd1.put("CORP_INFO_ID", Jurisdiction.getCORPINFO_ID());
            noticeCorpService.save(pd1);
            pd1.put("NOTICECORPUSERID_ID",UuidUtil.get32UUID());
            pd1.put("USER_ID",noticeUserId); //通知人
            pd1.put("TYPE",0);
            noticeCorpUserService.save(pd1);

        }catch (Exception e){

            System.out.println('2');
        }

    }
}
