package com.qmth.sop.business.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.entity.TFCustomFlowAllocation;
import com.qmth.sop.business.mapper.TFCustomFlowAllocationMapper;
import com.qmth.sop.business.service.TFCustomFlowAllocationService;
import com.qmth.sop.common.enums.RoleTypeEnum;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 流程角色分配表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-10-18
 */
@Service
public class TFCustomFlowAllocationServiceImpl extends ServiceImpl<TFCustomFlowAllocationMapper, TFCustomFlowAllocation> implements TFCustomFlowAllocationService {

    /**
     * 根据流程步骤获取审批角色
     *
     * @param roleAllocation
     * @param setup
     * @return
     */
    @Override
    public List<RoleTypeEnum> getRoleTypes(String roleAllocation, Integer setup) {
        JSONObject jsonObject = JSONObject.parseObject(roleAllocation);
        JSONArray jsonArray = jsonObject.getJSONArray("approveConfig");
        List<RoleTypeEnum> roleTypeEnumList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            Integer setupJs = object.getInteger("setup");
            if (Objects.nonNull(setupJs) && Objects.nonNull(setup) && setupJs.intValue() == setup.intValue()) {
                JSONArray jsonArrayRoleTypes = object.getJSONArray("roleTypes");
                for (int y = 0; y < jsonArray.size(); y++) {
                    JSONObject objectRole = jsonArrayRoleTypes.getJSONObject(y);
                    roleTypeEnumList.add(RoleTypeEnum.valueOf(objectRole.getString("role")));
                }
            }
        }
        return roleTypeEnumList;
    }
}
