package com.zcloud.service.notice.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.notice.NoticeCorpMapper;
import com.zcloud.service.notice.NoticeCorpService;
import com.zcloud.service.notice.NoticeCorpUserService;
import com.zcloud.service.notice.NoticeTemplateService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;
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.List;

/**
 * 说明：企业通知公告
 * 作者：luoxiaobao
 * 时间：2021-01-22
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class NoticeCorpServiceImpl implements NoticeCorpService {

	@Autowired
	private NoticeCorpMapper noticecorpMapper;
	@Autowired
	private NoticeTemplateService noticeTemplateService;
	@Autowired
	private NoticeCorpUserService noticeCorpUserService;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		noticecorpMapper.save(pd);
	}

	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		noticecorpMapper.delete(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		noticecorpMapper.edit(pd);
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return noticecorpMapper.datalistPage(page);
	}

	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAll(PageData pd)throws Exception{
		return noticecorpMapper.listAll(pd);
	}

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return noticecorpMapper.findById(pd);
	}

	public PageData findByUserId(PageData pd)throws Exception{
		return noticecorpMapper.findByUserId(pd);
	}


	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		noticecorpMapper.deleteAll(ArrayDATA_IDS);
	}

	@Override
	public List<PageData> listByUserId(Page page) throws Exception {
		// TODO Auto-generated method stub
		return noticecorpMapper.listByUserId(page);
	}

	@Override
	public List<PageData> listByIndex(Page page) throws Exception {
		// TODO Auto-generated method stub
		return noticecorpMapper.listByIndex(page);
	}

	@Override
	public int getRedPoint(PageData pd) throws Exception {
		// TODO Auto-generated method stub
		return noticecorpMapper.getRedPoint(pd);
	}


	/**获取平台公告（第一条）
	 * @param pd
	 * @throws Exception
	 */
	public PageData getPlaNewNotice(PageData pd)throws Exception{
		return noticecorpMapper.getPlaNewNotice(pd);
	}

	@Override
	public void sendNotice(PageData pd) throws Exception {
		PageData notice =  noticeTemplateService.findByBianMa(pd);
		String  content = "";
		if(notice!=null){
			content =  notice.getString("TEMPLATE_STR");
			PageData contentPd = (PageData) pd.get("CONTENT");
			for(Object key : contentPd.keySet()){	//替换占位符
				if ("number".equals(key.toString()) && Tools.notEmpty(pd.getString("WORKURL"))) {
					content = content.replace("${" + key.toString() + "}", "<span style=\"color: blue; font-weight: bold;\">"+contentPd.get(key).toString()+"</span>");
				} else {
					content = content.replace("${" + key.toString() + "}", contentPd.get(key).toString());
				}
			}
		}else {
			content = "未维护模板,需要管理端-公告模板进行维护";
		}

		PageData pd1 = new PageData();
		pd1.put("NOTICECORP_ID", UuidUtil.get32UUID());
		pd1.put("USERNAME",pd.getString("SENDER_NAME"));
		pd1.put("CREATOR", pd.getString("SENDER_ID"));	//添加人
		pd1.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
		pd1.put("SYNOPSIS",pd.getString("SYNOPSIS"));
		pd1.put("WORKURL",pd.getString("WORKURL"));
		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",pd.getString("CORPINFO_ID"));
		noticecorpMapper.save(pd1);
		pd1.put("NOTICECORPUSERID_ID",UuidUtil.get32UUID());
		pd1.put("USER_ID",pd.getString("RECEIVER_ID"));
		pd1.put("TYPE",0);
		noticeCorpUserService.save(pd1);
	}
}

