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.FinishedProductEntity;
import org.springblade.flow.form.entity.FinishedProductEntryEntity;
import org.springblade.flow.form.mapper.FinishedProductMapper;
import org.springblade.flow.form.model.finishedproduct.FinishedProductEntryEntityInfoModel;
import org.springblade.flow.form.model.finishedproduct.FinishedProductForm;
import org.springblade.flow.form.service.FinishedProductEntryService;
import org.springblade.flow.form.service.FinishedProductService;
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 FinishedProductServiceImpl extends ServiceImpl<FinishedProductMapper, FinishedProductEntity>
		implements FinishedProductService {

	@Autowired
	private BillRuleService billRuleService;
	@Autowired
	private FinishedProductEntryService finishedProductEntryService;

	@Override
	public List<FinishedProductEntryEntity> getFinishedEntryList(String id) {
		QueryWrapper<FinishedProductEntryEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(FinishedProductEntryEntity::getWarehouseId, id)
				.orderByDesc(FinishedProductEntryEntity::getSortCode);
		return finishedProductEntryService.list(queryWrapper);
	}

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

	@Override
	@Transactional(rollbackFor = WorkFlowException.class)
	public void save(String id, FinishedProductEntity entity,
			List<FinishedProductEntryEntity> finishedProductEntryEntityList) throws WorkFlowException {
		// 表单信息
		if (id == null) {
			entity.setId(RandomUtil.uuId());
			for (int i = 0; i < finishedProductEntryEntityList.size(); i++) {
				finishedProductEntryEntityList.get(i).setId(RandomUtil.uuId());
				finishedProductEntryEntityList.get(i).setWarehouseId(entity.getId());
				finishedProductEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
				finishedProductEntryService.save(finishedProductEntryEntityList.get(i));
			}
			// 创建
			this.save(entity);
			billRuleService.useBillNumber("WF_FinishedProductNo");
		} else {
			entity.setId(id);
			QueryWrapper<FinishedProductEntryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(FinishedProductEntryEntity::getWarehouseId, entity.getId());
			finishedProductEntryService.remove(queryWrapper);
			for (int i = 0; i < finishedProductEntryEntityList.size(); i++) {
				finishedProductEntryEntityList.get(i).setId(RandomUtil.uuId());
				finishedProductEntryEntityList.get(i).setWarehouseId(entity.getId());
				finishedProductEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
				finishedProductEntryService.save(finishedProductEntryEntityList.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, FinishedProductEntity entity,
			List<FinishedProductEntryEntity> finishedProductEntryEntityList) throws WorkFlowException {
		// 表单信息
		if (id == null) {
			entity.setId(RandomUtil.uuId());
			for (int i = 0; i < finishedProductEntryEntityList.size(); i++) {
				finishedProductEntryEntityList.get(i).setId(RandomUtil.uuId());
				finishedProductEntryEntityList.get(i).setWarehouseId(entity.getId());
				finishedProductEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
				finishedProductEntryService.save(finishedProductEntryEntityList.get(i));
			}
			// 创建
			this.save(entity);
			billRuleService.useBillNumber("WF_FinishedProductNo");
		} else {
			entity.setId(id);
			QueryWrapper<FinishedProductEntryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(FinishedProductEntryEntity::getWarehouseId, entity.getId());
			finishedProductEntryService.remove(queryWrapper);
			for (int i = 0; i < finishedProductEntryEntityList.size(); i++) {
				finishedProductEntryEntityList.get(i).setId(RandomUtil.uuId());
				finishedProductEntryEntityList.get(i).setWarehouseId(entity.getId());
				finishedProductEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
				finishedProductEntryService.save(finishedProductEntryEntityList.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) {
		FinishedProductForm finishedProductForm = JsonUtil.getJsonToBean(data, FinishedProductForm.class);
		FinishedProductEntity entity = JsonUtil.getJsonToBean(finishedProductForm, FinishedProductEntity.class);
		List<FinishedProductEntryEntityInfoModel> entryList = finishedProductForm.getEntryList() != null
				? finishedProductForm.getEntryList()
				: new ArrayList<>();
		List<FinishedProductEntryEntity> finishedProductEntryEntityList = JsonUtil.getJsonToList(entryList,
				FinishedProductEntryEntity.class);
		entity.setId(id);
		QueryWrapper<FinishedProductEntryEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(FinishedProductEntryEntity::getWarehouseId, entity.getId());
		finishedProductEntryService.remove(queryWrapper);
		for (int i = 0; i < finishedProductEntryEntityList.size(); i++) {
			finishedProductEntryEntityList.get(i).setId(RandomUtil.uuId());
			finishedProductEntryEntityList.get(i).setWarehouseId(entity.getId());
			finishedProductEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
			finishedProductEntryService.save(finishedProductEntryEntityList.get(i));
		}
		this.saveOrUpdate(entity);
	}
}
