package cn.com.capcloud.core.quartz;

import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.quartz.config.TaskJob;
import cn.com.capcloud.core.system.user.User;
import cn.com.capcloud.core.system.user.UserService;
import cn.com.capcloud.core.util.TblLogNames;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling
public class UpdateUserKhcRootPasswordTask implements TaskJob {
    private String userId="";
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    private static UpdateUserKhcRootPasswordTask task;
    @PostConstruct
    public void init(){
        task=this;
        task.baseService =this.baseService;
        task.userService =this.userService;
    }

    @Override
    public void run() {
        Map<String, Object> objectMap = new HashMap<>();
        String taskExecutionResult;
        String taskExecutionMessage;
        String thisBeanClass=this.getClass().getName();
        if(!userId.equals("")){
            User user=task.userService.getUser(Long.valueOf(userId));
            objectMap.put("task_executor_user", user.getUserName());
            userId="";//用完重置
        }else{
            objectMap.put("task_executor_user", "系统");
        }
        List<Map<String, Object>> taskList=getJdbcTemplate().queryForList("select * from tbl_data_task where status=0 and task_bean_class='"+thisBeanClass+"'");
        Map<String, Object> taskMap=taskList.get(0);
        objectMap.put("task_execution_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//执行时间
        objectMap.put("page_parent_uuid", taskMap.get("uuid"));

        //objectMap.put("task_execution_params", "执行参数");
        long startTime = System.currentTimeMillis();//获取开始时间
        try {
            taskExecutionResult = "Y";
            taskExecutionMessage = "执行成功";
            String password="Please change your password again";// khcroot 用户用于演示使用，当天有效，第二天还想使用请重置新的密码，或开通自己的用户
            this.getJdbcTemplate().execute("update tbl_system_user set user_password='"+password+"' where user_login_name='khcroot'");
        } catch (Exception e) {
            taskExecutionResult = "N";
            taskExecutionMessage = "执行失败";
        }
        objectMap.put("task_execution_duration_ms", (System.currentTimeMillis() - startTime) + "");
        objectMap.put("task_execution_result", taskExecutionResult);
        objectMap.put("task_result_message", taskExecutionMessage);
        objectMap.put("task_execution_end_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        objectMap.put("task_bean_class", thisBeanClass);
        task.baseService.save(TblLogNames.TBL_LOG_TASK_EXECUTOR,objectMap);
    }
    @Override
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate=jdbcTemplate;
    }

    @Override
    public JdbcTemplate getJdbcTemplate() {
        return this.jdbcTemplate;
    }

    @Override
    public void setUserId(String userId) {
        this.userId=userId;
    }

    @Override
    public String getUserId() {
        return this.userId;
    }
}

