package org.springblade.flow.form.service.impl;

import java.util.List;

import org.springblade.flow.base.service.BillRuleService;
import org.springblade.flow.base.util.FileManageUtil;
import org.springblade.flow.engine.util.ModelUtil;
import org.springblade.flow.exception.WorkFlowException;
import org.springblade.flow.form.entity.PostBatchTabEntity;
import org.springblade.flow.form.mapper.PostBatchTabMapper;
import org.springblade.flow.form.model.postbatchtab.PostBatchTabForm;
import org.springblade.flow.form.service.PostBatchTabService;
import org.springblade.flow.model.FileModel;
import org.springblade.flow.util.JsonUtil;
import org.springblade.flow.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 发文呈批表
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月29日 上午9:18
 */
@Service
public class PostBatchTabServiceImpl extends ServiceImpl<PostBatchTabMapper, PostBatchTabEntity>
		implements PostBatchTabService {

	@Autowired
	private BillRuleService billRuleService;
	@Autowired
	private FileManageUtil fileManageUtil;

	@Override
	public PostBatchTabEntity getInfo(String id) {
		QueryWrapper<PostBatchTabEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(PostBatchTabEntity::getId, id);
		return this.getOne(queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = WorkFlowException.class)
	public void save(String id, PostBatchTabEntity entity) throws WorkFlowException {
		// 表单信息
		if (id == null) {
			entity.setId(RandomUtil.uuId());
			this.save(entity);
			billRuleService.useBillNumber("WF_PostBatchTabNo");
			// 添加附件
			List<FileModel> data = JsonUtil.getJsonToList(entity.getFileJson(), FileModel.class);
			fileManageUtil.createFile(data);
		} else {
			entity.setId(id);
			this.updateById(entity);
			// 更新附件
			List<FileModel> data = JsonUtil.getJsonToList(entity.getFileJson(), FileModel.class);
			fileManageUtil.updateFile(data);
		}
		// 流程信息
		ModelUtil.save(id, entity.getFlowId(), entity.getId(), entity.getFlowTitle(), entity.getFlowUrgent(),
				entity.getBillNo(), entity);
	}

	@Override
	@Transactional(rollbackFor = WorkFlowException.class)
	public void submit(String id, PostBatchTabEntity entity) throws WorkFlowException {
		// 表单信息
		if (id == null) {
			entity.setId(RandomUtil.uuId());
			this.save(entity);
			billRuleService.useBillNumber("WF_PostBatchTabNo");
			// 添加附件
			List<FileModel> data = JsonUtil.getJsonToList(entity.getFileJson(), FileModel.class);
			fileManageUtil.createFile(data);
		} else {
			entity.setId(id);
			this.updateById(entity);
			// 更新附件
			List<FileModel> data = JsonUtil.getJsonToList(entity.getFileJson(), FileModel.class);
			fileManageUtil.updateFile(data);
		}
		// 流程信息
		ModelUtil.submit(id, entity.getFlowId(), entity.getId(), entity.getFlowTitle(), entity.getFlowUrgent(),
				entity.getBillNo(), entity, null);
	}

	@Override
	public void data(String id, String data) {
		PostBatchTabForm postBatchTabForm = JsonUtil.getJsonToBean(data, PostBatchTabForm.class);
		PostBatchTabEntity entity = JsonUtil.getJsonToBean(postBatchTabForm, PostBatchTabEntity.class);
		entity.setId(id);
		this.saveOrUpdate(entity);
	}
}
