package cc.smtweb.system.bpm.web.engine.model.common;

import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * @Author: tanghp
 * @Date: 2022-09-15 17:27
 * @Desc: 模型明细处理类
 */
public class ModelDetail<T extends DefaultEntity> {
    //页面上新增的行
    protected List<T> insertList = new ArrayList<T>();
    //页面上修改的行
    protected List<T> updateList = new ArrayList<T>();
    //页面上删除的行Id
    protected List<Long> deleteList = new ArrayList<Long>();

    public List<T> getInsertList() {
        return insertList;
    }

    public List<T> getUpdateList() {
        return updateList;
    }

    public List<Long> getDeleteList() {
        return deleteList;
    }

    public void setInsertList(List<T> insertList) {
        this.insertList = insertList;
    }

    public void setUpdateList(List<T> updateList) {
        this.updateList = updateList;
    }

    public void setDeleteList(List<Long> deleteList) {
        this.deleteList = deleteList;
    }

    public void addInsert(T t) {
        insertList.add(t);
    }

    public void addUpdate(T t) {
        updateList.add(t);
    }

    public void addDelete(long id) {
        deleteList.add(id);
    }

    //从页面对象读取
    public void readFromMap(String key, SwMap pageData, IModelDetailReadIntf<T> intf){
        SwMap details = pageData.readMap(key);
        List<Map<String,Object>> inserted = details.readListMap("inserted");
        if(!CommUtil.isEmpty(inserted)){
            inserted.forEach((row)-> {
                T bean = intf.getNewInstance();
                bean.init();
                bean.getData().putAll(row);
                intf.doAfterRead(true,bean);
            });
        }
        List<Map<String,Object>> updated = details.readListMap("updated");
        if(!CommUtil.isEmpty(updated)){
            updated.forEach((row)-> {
                T bean = intf.getNewInstance();
                bean.init();
                bean.getData().putAll(row);
                intf.doAfterRead(false,bean);
            });
        }
        Set<Long> deleted = details.readLongSet("deleted");
        if(!CommUtil.isEmpty(deleted)){
            deleteList.addAll(deleted);
        }
    }

    public void batchSave(EntityDao<T> dao) {
        if (!CommUtil.isEmpty(this.deleteList)) {
            dao.deleteEntity(this.deleteList);
        }
        if (!CommUtil.isEmpty(this.updateList)) {
            dao.batchUpdateEntity(this.updateList);
        }
        if (!CommUtil.isEmpty(this.insertList)) {
            dao.batchInsertEntity(this.insertList);
        }
    }

    public void readFromMap(PageDataset pageDataset, SwMap pageData, BiConsumer<Boolean,T> consumer){
        ModelTable table = ModelTableCache.getInstance().get(pageDataset.masterTable);
        if (table == null) throw new BizException("没有找到待保存的表定义：" + pageDataset.name);
        SwMap details = pageData.readMap(pageDataset.name);
        if (details == null) throw new BizException("没有找到待保存的表数据：" + pageDataset.name);
        EntityDao<T> dao = DbEngine.getInstance().findDao(table.getName());
        List<Map<String,Object>> inserted = details.readListMap("inserted");
        if(!CommUtil.isEmpty(inserted)){
            inserted.forEach((row)-> {
                T bean = dao.createBean();
                bean.init();
                bean.getData().putAll(row);
                bean.setEntityId(DbEngine.getInstance().nextId());
                if(consumer!=null){
                    consumer.accept(true,bean);
                }
                insertList.add(bean);
            });
        }
        List<Map<String,Object>> updated = details.readListMap("updated");
        if(!CommUtil.isEmpty(updated)){
            updated.forEach((row)-> {
                T bean = dao.createBean();
                bean.init();
                bean.getData().putAll(row);
                if(consumer!=null){
                    consumer.accept(false,bean);
                }
                updateList.add(bean);
            });
        }
        Set<Long> deleted = details.readLongSet("deleted");
        if(!CommUtil.isEmpty(deleted)){
            deleteList.addAll(deleted);
        }
    }

    public void batchSave(PageDataset pageDataset) {
        ModelTable table = ModelTableCache.getInstance().get(pageDataset.masterTable);
        if (table == null) throw new BizException("没有找到待保存的表定义：" + pageDataset.name);
        EntityDao<T> dao = DbEngine.getInstance().findDao(table.getName());
        batchSave(dao);
    }
}
