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.Labor;
import com.gyee.frame.model.ticket.TokenUser;
import com.gyee.frame.model.ticket.Wfrole;
import com.gyee.frame.service.ticket.LaborService;
import com.gyee.frame.service.ticket.ShiroService;
import com.gyee.frame.service.ticket.WfroleService;
import com.gyee.frame.util.ticket.TicketUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

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

    @Autowired
    LaborService laborService;
    @Autowired
    WfroleService wfroleService;
    @Autowired
    ShiroService shiroService;

    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "group")
    public AjaxResult getLaborGroup(@RequestParam("role") String role, @RequestParam("dept") String dept) {
        Wfrole wfrole = wfroleService.getRole(role);
        List<Labor> labors = new ArrayList<>();
        if (wfrole != null){
            switch (wfrole.getRoletype()) {
                case "自定义类":
                    labors = laborService.getLaborOfDept(dept, role);
                    break;
                case "人员组":
                    labors = laborService.getLaborOfPersonGroup(role);
                    break;
                case "人员":
                    labors = laborService.getLaborOfPerson(role);
                    break;
            }
        } else {
            labors = laborService.getLabors(dept);
        }

        if (labors.size() == 0)
            return AjaxResult.error(4004, "数据为空");

        return AjaxResult.successData(labors);
    }

    @DataSource(value = DataSourceType.TICKET)
    @PostMapping(value = "single")
    public AjaxResult getLaborSingle(@RequestParam("info") String info) {
        JSONObject jsonObject = JSONObject.parseObject(info);
        Double instId = jsonObject.getDouble("instid");
        String role = jsonObject.getString("role");

        List<Labor> labors = new ArrayList<>();
        if (role.contains("流程启动者") || role.contains("流程人员变更"))
            labors = laborService.getFlowStarter(instId);
        else
            labors = laborService.getLaborSingle(instId, role);

        if (labors == null || labors.size() == 0)
            return AjaxResult.error(4004, "数据为空");

        return AjaxResult.successData(labors);
    }

    @DataSource(value = DataSourceType.TICKET)
    @GetMapping(value = "list")
    public AjaxResult getLabors(@RequestHeader("token") String header){
        TokenUser token = shiroService.findToken(header);
        List<Labor> labors = laborService.getLabors(token.getDepartment());

        if (labors == null || labors.size() == 0)
            return AjaxResult.error(4004, "数据为空");

        return AjaxResult.successData(labors);
    }
}

