package com.mortals.xhx.base.system.task.web;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.task.model.TaskEntity;
import com.mortals.xhx.base.system.task.model.TaskQuery;
import com.mortals.xhx.base.system.task.service.TaskService;
import com.mortals.xhx.common.code.DataSatusEnum;
import com.mortals.xhx.common.code.TaskExcuteStatusEnum;
import com.mortals.xhx.common.code.TaskExcuteStrategyEnum;
import com.mortals.xhx.common.code.TaskInterimExcuteStatusEnum;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 任务信息
 *
 * @author: zxfei
 * @date: 2022/5/7 15:39
 */
@RestController
@RequestMapping("task")
public class TaskController extends BaseCRUDJsonBodyMappingController<TaskService, TaskEntity, Long> {

    public TaskController() {
        super.setFormClass(TaskForm.class);
        super.setModuleDesc("任务信息");
    }


    @Override
    protected void init(Map<String, Object> model, Context context) {
        Map<String, Object> status = new HashMap<String, Object>();
        status.put("status", TaskExcuteStatusEnum.getEnumMap());
        status.put("excuteStrategy", TaskExcuteStrategyEnum.getEnumMap());
        status.put("interimExcuteStatus", TaskInterimExcuteStatusEnum.getEnumMap());
        Map<String, ITaskExcuteService> serviceMap = GlobalSysInfo.getBeansByType(ITaskExcuteService.class);
        Map<String, Object> serviceList = new HashMap<String, Object>();
        if (serviceMap != null) {
            try {
                serviceMap.forEach((k, v) -> {
                    if (null != v) {
                        serviceList.put(k, k);
                    }
                });
            } catch (Exception ex) {
                log.error("", ex);
            }
        }
        model.put("excuteService", serviceList);
        model.put(KEY_RESULT_DICT, status);
    }

    /**
     * 改变状态
     */
    @RequestMapping(value = "change/status")
    public String changeStatus(TaskQuery query) {
        JSONObject ret = new JSONObject();
        Context context = getContext();
        try {
            TaskEntity entity = this.service.get(query.getId(), context);// .doSubmitAudit(form.getEntity(),
            // context);
            if (null == entity) {
                throw new AppException("任务不存在!");
            }
            if (null == query.getStatus()) {
                throw new AppException("任务状态不能为空!");
            }
            if (query.getStatus() != DataSatusEnum.ENABLE.getValue()
                    && query.getStatus() != DataSatusEnum.DISENABLE.getValue()) {
                throw new AppException("非法任务状态!");
            }
            String busiDesc = DataSatusEnum.getByValue(query.getStatus()).getDesc();
            entity.setStatus(query.getStatus());
            this.service.update(entity, context);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_MSG, busiDesc + "成功");
            recordSysLog(request, busiDesc + " 【成功】 [id:" + entity.getId() + "]");

        } catch (Exception e) {
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return ret.toJSONString();
    }

}