package com.boman.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.SecurityUtils;
import com.boman.system.common.MainTableRecord;
import com.boman.system.common.RowResult;
import com.boman.system.mapper.StandardlyMapper;
import com.boman.system.service.IBaseSaveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

/**
 * @author shiqian
 * @description
 * @date 2021年03月22日 11:25
 **/
@Component
public class BaseSaveServiceImpl implements IBaseSaveService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseSaveServiceImpl.class);

    @Autowired
    private StandardlyMapper mapper;

    /**
     * 功能描述: 保存一行
     *
     * @param tableName tableName
     * @param pkName    主键名称
     * @param maxId     最大的SEQ
     * @param row       要保存的数据
     * @return com.boman.system.common.RowResult
     */
    @Override
    public RowResult insertRow(String tableName, String pkName, Long maxId, MainTableRecord row) {
        JSONObject commitData = row.getMainData().getCommitData();
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        commitData.put("UPDATE_BY", SecurityUtils.getUserId());
        commitData.put("UPDATE_TIME", currentTime);
        // 新增
        if (row.getId() > 0) {
            commitData.put("CREATE_TIME", currentTime);
            commitData.put("CREATE_BY", SecurityUtils.getUserId());
        }

        commitData.put(pkName.toUpperCase(), maxId);
        int ret = mapper.insert(tableName, commitData);
        if (ret > 0) {
            commitData.put("successCnt", ret);
            return RowResult.ok("保存成功", commitData);
        }

        return RowResult.error("失败");
    }

}
