package com.boman.web.core.domain;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.SpringUtils;
import com.boman.common.redis.RedisKey;
import com.boman.common.redis.service.RedisService;
import com.boman.gen.domain.GenTable;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author shiqian
 * @description
 * @date 2021年03月22日 09:58
 **/
@Service
public class TableServiceContext {

    private final static String PREFIX = "Controller:/event/do";

    /**
     * 当前表
     */
    private GenTable table;

    /**
     * 真实表名
     */
    private String realTableName;

    /**
     * 动作名（ADD,SAVE,SUBMIT,VOID,UNSUBMIT）
     */
    private String actionName;   //Controller:/event/doAdd

    /**
     * 业务发生的时间戳
     */
    private Timestamp currentTime;

    /**
     * 上行的数据
     * 每个row对象对应到一条单据（如果存在事务，应该在这个级别上体现）
     */
    private List<MainTableRecord> rows;

    /**
     * 本次请求存放的临时数据
     * ！！慎用，最好不要在各个对象间传递
     */
    private Map<String, Object> values;

    private boolean isDelMtable;

    /**
     * 是否批量新增
     */
    private boolean isInsertBacth = false;

    private TableServiceContext() {
        values = Maps.newHashMap();
    }


    @Resource
    private RedisService redisService;

    public static TableServiceContext createFrom(BaseTableDTO baseTableDTO) {
        TableServiceContext result = new TableServiceContext();
        String tableName = baseTableDTO.getTable();
        if (StringUtils.isEmpty(tableName)) {
            throw new IllegalArgumentException("表名参数不能为空");
        }

        //从redis中获取表信息
        RedisService redisService = SpringUtils.getBean(RedisService.class);
        result.table = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);

        if (result.table == null) {
            throw new IllegalArgumentException(tableName + "表信息不存在");
        }

        result.realTableName = result.table.getTableName();
        if (Strings.isNullOrEmpty(result.realTableName)) {
            throw new IllegalArgumentException(tableName + "表名不存在");
        }

        // 前台传过来的数据
        JSONObject fixedData = baseTableDTO.getFixedData();
        //删除
        Boolean isdelmtable = baseTableDTO.getDelMTable();
        JSONObject tabItem = baseTableDTO.getTabItem();
        if (isdelmtable != null) {
            result.isDelMtable = isdelmtable;
        }

        // 获取objid判断 新增或更新
        Long objid = baseTableDTO.getObjId();
        MainTableRecord mainRowData = new MainTableRecord(result, result.table, objid, fixedData, tabItem);
        result.rows = Lists.newArrayList();
        result.rows.add(mainRowData);

        return result;
    }

    public GenTable getTable() {
        return table;
    }

    public void setTable(GenTable table) {
        this.table = table;
    }

    public String getRealTableName() {
        return realTableName;
    }

    public void setRealTableName(String realTableName) {
        this.realTableName = realTableName;
    }

    public String getActionName() {
        return actionName;
    }

    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    public Timestamp getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(Timestamp currentTime) {
        this.currentTime = currentTime;
    }

    public List<MainTableRecord> getRows() {
        return rows;
    }

    public void setRows(List<MainTableRecord> rows) {
        this.rows = rows;
    }

    public Map<String, Object> getValues() {
        return values;
    }

    public void setValues(Map<String, Object> values) {
        this.values = values;
    }

    public boolean isDelMtable() {
        return isDelMtable;
    }

    public void setDelMtable(boolean delMtable) {
        isDelMtable = delMtable;
    }

    public boolean isInsertBacth() {
        return isInsertBacth;
    }

    public void setInsertBacth(boolean insertBacth) {
        isInsertBacth = insertBacth;
    }
//
//    public Collection<Filter> getFilters() {
//        return filters;
//    }
//
//    public void setFilters(Collection<Filter> filters) {
//        this.filters = filters;
//    }
}
