/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.service.sysmgr.service.impl;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.utils.Base64Util;
import com.crunii.micro.domain.entity.main.TsmPub;
import com.crunii.micro.domain.entity.main.TsmPubContent;
import com.crunii.micro.domain.repo.main.TsmPubContentRepo;
import com.crunii.micro.domain.repo.main.TsmPubRepo;
import com.crunii.micro.service.sysmgr.dto.pub.PubC;
import com.crunii.micro.service.sysmgr.dto.pub.PubInfo;
import com.crunii.micro.service.sysmgr.dto.pub.PubReq;
import com.crunii.micro.service.sysmgr.dto.pub.PubU;
import com.crunii.micro.service.sysmgr.jdbc.PubJdbcDao;
import com.crunii.micro.service.sysmgr.service.PublishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 田平 create 2019年6月14日上午10:58:10
 */
@Service
public class PublishServiceImpl implements PublishService {

	@Autowired
	private TsmPubRepo tsmPubRepo;

	@Autowired
	private TsmPubContentRepo tsmPubContentRepo;

	@Autowired
	private PubJdbcDao pubJdbcDao;

	public PageRsp<PubInfo> pagePub(PageReq<PubReq> req) {
		return pubJdbcDao.pagePub(req);
	}

	public String getPubContent(int pubId) {
		TsmPubContent tsmPubContent = tsmPubContentRepo.getOne(pubId);
		return Base64Util.encodeMime(tsmPubContent.getPubContent());
	}

	public void createPub(PubC pubC) {
		TsmPub tsmPub = new TsmPub();
		BeanUtils.copyProperties(pubC, tsmPub);
		tsmPub.setState(1);
		tsmPub.setCreateTime(new Date());
		tsmPubRepo.save(tsmPub);
		tsmPubRepo.flush();
		TsmPubContent tsmPubContent = new TsmPubContent();
		tsmPubContent.setPubId(tsmPub.getPubId());
		String content = Base64Util.decodeMime(pubC.getPubContent());
		tsmPubContent.setPubContent(content);
		tsmPubContentRepo.save(tsmPubContent);
	}

	public void updatePub(PubU pubU) {
		TsmPub tsmPub = tsmPubRepo.getOne(pubU.getPubId());
		BeanUtils.copyProperties(pubU, tsmPub);
		tsmPubRepo.save(tsmPub);
		TsmPubContent tsmPubContent = tsmPubContentRepo.getOne(pubU.getPubId());
		String content = Base64Util.decodeMime(pubU.getPubContent());
		tsmPubContent.setPubContent(content);
		tsmPubContentRepo.save(tsmPubContent);
	}

}
