package cn.com.capcloud.api.action.myself.system.data;

import cn.com.capcloud.api.action.myself.system.TestTransactionService;
import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.cache.KHCRedisCache;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.system.user.UserService;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataTestNewFeaturesAction extends DefaultAction {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DataTestNewFeaturesAction.class);

    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private ApiService apiService;
    @Resource
    private KHCRedisCache khcRedisCache;
    @Resource
    private TestTransactionService testTransactionService;
    private static DataTestNewFeaturesAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.khcRedisCache =this.khcRedisCache;
        action.testTransactionService =this.testTransactionService;
    }

    /**
     * 测试
     */
    public Map<String,Object> testApiMethod(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        Map<String, Object> returnMap=new HashMap<>();
        returnMap.put("message","成功");
        returnMap.put("data","");
        returnMap.put("status","ok");
        return returnMap;
    }

    /**
     * 测试事务
     */
    public Map<String,Object> testTransaction(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        map.put("message","测试事务");
        try {
            action.testTransactionService.updateTableData();
        } catch (Exception e) {
            //e.printStackTrace();
            logger.error("SQL异常了，ccccc字段名不存在，回滚所有操作");
        }
        String dataSource =this.getJdbcTemplate().queryForObject("select data_source from tbl_data_test_new_features where id=1",String.class);
        logger.debug("回滚后："+dataSource);
        return map;
    }
}
