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

import java.util.ArrayList;
import java.util.List;

import org.springblade.flow.base.service.BillRuleService;
import org.springblade.flow.engine.util.ModelUtil;
import org.springblade.flow.exception.WorkFlowException;
import org.springblade.flow.form.entity.MaterialEntryEntity;
import org.springblade.flow.form.entity.MaterialRequisitionEntity;
import org.springblade.flow.form.mapper.MaterialRequisitionMapper;
import org.springblade.flow.form.model.materialrequisition.MaterialEntryEntityInfoModel;
import org.springblade.flow.form.model.materialrequisition.MaterialRequisitionForm;
import org.springblade.flow.form.service.MaterialEntryService;
import org.springblade.flow.form.service.MaterialRequisitionService;
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 MaterialRequisitionServiceImpl extends ServiceImpl<MaterialRequisitionMapper, MaterialRequisitionEntity>
		implements MaterialRequisitionService {

	@Autowired
	private BillRuleService billRuleService;
	@Autowired
	private MaterialEntryService materialEntryService;

	@Override
	public List<MaterialEntryEntity> getMaterialEntryList(String id) {
		QueryWrapper<MaterialEntryEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(MaterialEntryEntity::getLeadeId, id).orderByDesc(MaterialEntryEntity::getSortCode);
		return materialEntryService.list(queryWrapper);
	}

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

	@Override
	@Transactional(rollbackFor = WorkFlowException.class)
	public void save(String id, MaterialRequisitionEntity entity, List<MaterialEntryEntity> materialEntryEntityList)
			throws WorkFlowException {
		// 表单信息
		if (id == null) {
			entity.setId(RandomUtil.uuId());
			for (int i = 0; i < materialEntryEntityList.size(); i++) {
				materialEntryEntityList.get(i).setId(RandomUtil.uuId());
				materialEntryEntityList.get(i).setLeadeId(entity.getId());
				materialEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
				materialEntryService.save(materialEntryEntityList.get(i));
			}
			// 创建
			this.save(entity);
			billRuleService.useBillNumber("WF_MaterialRequisitionNo");
		} else {
			entity.setId(id);
			QueryWrapper<MaterialEntryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(MaterialEntryEntity::getLeadeId, entity.getId());
			materialEntryService.remove(queryWrapper);
			for (int i = 0; i < materialEntryEntityList.size(); i++) {
				materialEntryEntityList.get(i).setId(RandomUtil.uuId());
				materialEntryEntityList.get(i).setLeadeId(entity.getId());
				materialEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
				materialEntryService.save(materialEntryEntityList.get(i));
			}
			// 编辑
			this.updateById(entity);
		}
		// 流程信息
		ModelUtil.save(id, entity.getFlowId(), entity.getId(), entity.getFlowTitle(), entity.getFlowUrgent(),
				entity.getBillNo(), entity);
	}

	@Override
	@Transactional(rollbackFor = WorkFlowException.class)
	public void submit(String id, MaterialRequisitionEntity entity, List<MaterialEntryEntity> materialEntryEntityList)
			throws WorkFlowException {
		// 表单信息
		if (id == null) {
			entity.setId(RandomUtil.uuId());
			for (int i = 0; i < materialEntryEntityList.size(); i++) {
				materialEntryEntityList.get(i).setId(RandomUtil.uuId());
				materialEntryEntityList.get(i).setLeadeId(entity.getId());
				materialEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
				materialEntryService.save(materialEntryEntityList.get(i));
			}
			// 创建
			this.save(entity);
			billRuleService.useBillNumber("WF_MaterialRequisitionNo");
		} else {
			entity.setId(id);
			QueryWrapper<MaterialEntryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(MaterialEntryEntity::getLeadeId, entity.getId());
			materialEntryService.remove(queryWrapper);
			for (int i = 0; i < materialEntryEntityList.size(); i++) {
				materialEntryEntityList.get(i).setId(RandomUtil.uuId());
				materialEntryEntityList.get(i).setLeadeId(entity.getId());
				materialEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
				materialEntryService.save(materialEntryEntityList.get(i));
			}
			// 编辑
			this.updateById(entity);
		}
		// 流程信息
		ModelUtil.submit(id, entity.getFlowId(), entity.getId(), entity.getFlowTitle(), entity.getFlowUrgent(),
				entity.getBillNo(), entity, null);
	}

	@Override
	public void data(String id, String data) {
		MaterialRequisitionForm materialRequisitionForm = JsonUtil.getJsonToBean(data, MaterialRequisitionForm.class);
		MaterialRequisitionEntity entity = JsonUtil.getJsonToBean(materialRequisitionForm,
				MaterialRequisitionEntity.class);
		List<MaterialEntryEntityInfoModel> entryList = materialRequisitionForm.getEntryList() != null
				? materialRequisitionForm.getEntryList()
				: new ArrayList<>();
		List<MaterialEntryEntity> materialEntryEntityList = JsonUtil.getJsonToList(entryList,
				MaterialEntryEntity.class);
		entity.setId(id);
		QueryWrapper<MaterialEntryEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(MaterialEntryEntity::getLeadeId, entity.getId());
		materialEntryService.remove(queryWrapper);
		for (int i = 0; i < materialEntryEntityList.size(); i++) {
			materialEntryEntityList.get(i).setId(RandomUtil.uuId());
			materialEntryEntityList.get(i).setLeadeId(entity.getId());
			materialEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
			materialEntryService.save(materialEntryEntityList.get(i));
		}
		this.saveOrUpdate(entity);
	}
}
