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.ApplyDeliverGoodsEntity;
import org.springblade.flow.form.entity.ApplyDeliverGoodsEntryEntity;
import org.springblade.flow.form.mapper.ApplyDeliverGoodsMapper;
import org.springblade.flow.form.model.applydelivergoods.ApplyDeliverGoodsEntryInfoModel;
import org.springblade.flow.form.model.applydelivergoods.ApplyDeliverGoodsForm;
import org.springblade.flow.form.service.ApplyDeliverGoodsEntryService;
import org.springblade.flow.form.service.ApplyDeliverGoodsService;
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 ApplyDeliverGoodsServiceImpl extends ServiceImpl<ApplyDeliverGoodsMapper, ApplyDeliverGoodsEntity> implements ApplyDeliverGoodsService {

    @Autowired
    private ApplyDeliverGoodsEntryService applyDeliverGoodsEntryService;
    @Autowired
    private BillRuleService billRuleService;

    @Override
    public List<ApplyDeliverGoodsEntryEntity> getDeliverEntryList(String id) {
        QueryWrapper<ApplyDeliverGoodsEntryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApplyDeliverGoodsEntryEntity::getInvoiceId, id).orderByDesc(ApplyDeliverGoodsEntryEntity::getSortCode);
        return applyDeliverGoodsEntryService.list(queryWrapper);
    }

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

    @Override
    @Transactional(rollbackFor = WorkFlowException.class)
    public void save(String id, ApplyDeliverGoodsEntity entity, List<ApplyDeliverGoodsEntryEntity> applyDeliverGoodsEntryEntityList) throws WorkFlowException {
        //表单信息
        if (id == null) {
            entity.setId(RandomUtil.uuId());
            for (int i = 0; i < applyDeliverGoodsEntryEntityList.size(); i++) {
                applyDeliverGoodsEntryEntityList.get(i).setId(RandomUtil.uuId());
                applyDeliverGoodsEntryEntityList.get(i).setInvoiceId(entity.getId());
                applyDeliverGoodsEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
                applyDeliverGoodsEntryService.save(applyDeliverGoodsEntryEntityList.get(i));
            }
            this.save(entity);
            billRuleService.useBillNumber("WF_ApplyDeliverGoodsNo");
        } else {
            entity.setId(id);
            QueryWrapper<ApplyDeliverGoodsEntryEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApplyDeliverGoodsEntryEntity::getInvoiceId, entity.getId());
            applyDeliverGoodsEntryService.remove(queryWrapper);
            for (int i = 0; i < applyDeliverGoodsEntryEntityList.size(); i++) {
                applyDeliverGoodsEntryEntityList.get(i).setId(RandomUtil.uuId());
                applyDeliverGoodsEntryEntityList.get(i).setInvoiceId(entity.getId());
                applyDeliverGoodsEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
                applyDeliverGoodsEntryService.save(applyDeliverGoodsEntryEntityList.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, ApplyDeliverGoodsEntity entity, List<ApplyDeliverGoodsEntryEntity> applyDeliverGoodsEntryEntityList) throws WorkFlowException {
        //表单信息
        if (id == null) {
            entity.setId(RandomUtil.uuId());
            for (int i = 0; i < applyDeliverGoodsEntryEntityList.size(); i++) {
                applyDeliverGoodsEntryEntityList.get(i).setId(RandomUtil.uuId());
                applyDeliverGoodsEntryEntityList.get(i).setInvoiceId(entity.getId());
                applyDeliverGoodsEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
                applyDeliverGoodsEntryService.save(applyDeliverGoodsEntryEntityList.get(i));
            }
            this.save(entity);
            billRuleService.useBillNumber("WF_ApplyDeliverGoodsNo");
        } else {
            entity.setId(id);
            QueryWrapper<ApplyDeliverGoodsEntryEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApplyDeliverGoodsEntryEntity::getInvoiceId, entity.getId());
            applyDeliverGoodsEntryService.remove(queryWrapper);
            for (int i = 0; i < applyDeliverGoodsEntryEntityList.size(); i++) {
                applyDeliverGoodsEntryEntityList.get(i).setId(RandomUtil.uuId());
                applyDeliverGoodsEntryEntityList.get(i).setInvoiceId(entity.getId());
                applyDeliverGoodsEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
                applyDeliverGoodsEntryService.save(applyDeliverGoodsEntryEntityList.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) {
        ApplyDeliverGoodsForm applyDeliverGoodsForm = JsonUtil.getJsonToBean(data, ApplyDeliverGoodsForm.class);
        ApplyDeliverGoodsEntity entity = JsonUtil.getJsonToBean(applyDeliverGoodsForm, ApplyDeliverGoodsEntity.class);
        List<ApplyDeliverGoodsEntryInfoModel> entryList = applyDeliverGoodsForm.getEntryList() != null ? applyDeliverGoodsForm.getEntryList() : new ArrayList<>();
        List<ApplyDeliverGoodsEntryEntity> applyDeliverGoodsEntryEntityList = JsonUtil.getJsonToList(entryList, ApplyDeliverGoodsEntryEntity.class);
        entity.setId(id);
        QueryWrapper<ApplyDeliverGoodsEntryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApplyDeliverGoodsEntryEntity::getInvoiceId, entity.getId());
        applyDeliverGoodsEntryService.remove(queryWrapper);
        for (int i = 0; i < applyDeliverGoodsEntryEntityList.size(); i++) {
            applyDeliverGoodsEntryEntityList.get(i).setId(RandomUtil.uuId());
            applyDeliverGoodsEntryEntityList.get(i).setInvoiceId(entity.getId());
            applyDeliverGoodsEntryEntityList.get(i).setSortCode(Long.parseLong(i + ""));
            applyDeliverGoodsEntryService.save(applyDeliverGoodsEntryEntityList.get(i));
        }
        this.saveOrUpdate(entity);
    }
}
