package com.mortals.xhx.module.task.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.Result;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.FlowStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.module.task.model.TaskTodoEntity;
import com.mortals.xhx.module.task.service.TaskTodoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;


/**
* <p>Title: 工作代办表</p>
* <p>Description: TaskTodoController  </p>
* <p>Copyright: Copyright &reg;  </p>
* <p>Company: </p>
* @author zxfei
* @version 1.0.0
*/
@RestController
@RequestMapping("task/todo")
public class TaskTodoController extends BaseCRUDJsonMappingController<TaskTodoService,TaskTodoForm,TaskTodoEntity,Long> {

    @Autowired
    private UserService userService;

    public TaskTodoController(){
        super.setFormClass(TaskTodoForm.class);
        super.setModuleDesc("工作代办表");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, TaskTodoForm form, Map<String, Object> model, Context context) {

        this.addDict(model, "startUserName",userService.find(new UserQuery(), getContext()).stream()
                .collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getRealName(), (o, n) -> n)));
    }

    /**
     * 查询代办列表
     */
    @PostMapping(value = "list")
    @Override
    public String list( TaskTodoForm form) {
        Map<String, Object> model = new HashMap();
        JSONObject ret = new JSONObject();
        int code = VALUE_RESULT_SUCCESS;
        String busiDesc = "查询" + this.getModuleDesc();
        try {
            Result result = this.service.todoList(form.getQuery(), form.getPageInfo(), getContext());
            this.init(request, response, form, model, getContext());
            model.put("result", result.getList());
            model.put("pageInfo", result.getPageInfo());
            model.putAll(form.getModel());
            this.recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error("查询流程列表异常", e);
            this.doException(request, busiDesc, model, e);
            code = VALUE_RESULT_FAILURE;
        }

        ret.put("code", code);
        ret.put("query", form.getQuery());
        ret.put("data", model);
        return ret.toJSONString();
    }

}