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.WarehouseEntryEntity;
import org.springblade.flow.form.entity.WarehouseReceiptEntity;
import org.springblade.flow.form.mapper.WarehouseReceiptMapper;
import org.springblade.flow.form.model.warehousereceipt.WarehouseReceiptEntityInfoModel;
import org.springblade.flow.form.model.warehousereceipt.WarehouseReceiptForm;
import org.springblade.flow.form.service.WarehouseEntryService;
import org.springblade.flow.form.service.WarehouseReceiptService;
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("warehouseReceipt")
public class WarehouseReceiptServiceImpl extends ServiceImpl<WarehouseReceiptMapper, WarehouseReceiptEntity>
		implements WarehouseReceiptService {

	@Autowired
	private BillRuleService billRuleService;
	@Autowired
	private WarehouseEntryService warehouseEntryService;

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

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

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