package com.gyee.frame.controller.ticket;


import com.alibaba.fastjson.JSONObject;
import com.gyee.frame.common.dataSources.DataSource;
import com.gyee.frame.common.dataSources.DataSourceType;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.ticket.TokenUser;
import com.gyee.frame.service.ticket.ShiroService;
import com.gyee.frame.service.ticket.WfinstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenmh
 * @since 2020-07-21
 */
@RestController
@RequestMapping("/wfinstance")
public class WfinstanceController {

    @Autowired
    ShiroService shiroService;
    @Autowired
    WfinstanceService wfinstanceService;

    @DataSource(DataSourceType.TICKET)
    @PostMapping(value = "init/flow")
    public AjaxResult initFlow(@RequestHeader("token") String header, @RequestParam("info") String info){
        JSONObject jsonObject = JSONObject.parseObject(info);
        int status = jsonObject.getInteger("status");
        double ownerId = jsonObject.getDouble("ownerid");
        String wfName = jsonObject.getString("wfname");
        String stater = jsonObject.getString("stater");
        String principal = jsonObject.getString("principal");
        String role = jsonObject.getString("role");
        String ticketNum = jsonObject.getString("ticketnum");
        List<String> labors = (List<String>) jsonObject.get("labors");

        TokenUser token = shiroService.findToken(header);

        // 流程初始化
        if (status == 0){
            boolean result = wfinstanceService.initInstance(ownerId, wfName, ticketNum, principal, stater, labors, role, token.getLaborName());
            if (!result)
                return AjaxResult.error(4004, "流程发送失败");
        }

        return AjaxResult.success("流程发送成功");
    }
}

