package com.friday.tooldriver.service.rpa.smartRPA;

import com.alibaba.fastjson.JSONObject;
import com.friday.base.common.response.DubboResult;
import com.friday.operation.api.keepalive.dto.taieoc.OcGroupFilterDto;
import com.friday.operation.api.tooldriver.dto.common.DriverBase;
import com.friday.operation.api.tooldriver.dto.rpa.GroupDto;
import com.friday.operation.api.tooldriver.dto.rpa.GroupFilter;
import com.friday.tooldriver.constants.TaieOcManageCode;
import com.friday.tooldriver.mapper.taieoc.OcGroupMapper;
import com.friday.tooldriver.po.taieoc.OcGroupPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SmartRPAGroupManage {
    @Autowired
    private OcGroupMapper ocGroupMapper;


    public DubboResult<GroupDto> addGroup(DriverBase driverBase, GroupDto group){
        // 组信息重复性校验
        OcGroupPo groupPo = ocGroupMapper.selectProcessGroup(group.getName(), Long.parseLong(driverBase.getTenantKey()));
        if(!ObjectUtils.isEmpty(groupPo)){
            return DubboResult.newFailure(TaieOcManageCode.DRIVER_GROUP_ALREADY_EXIST.getCode(), TaieOcManageCode.DRIVER_GROUP_ALREADY_EXIST.getMessage());
        }
        OcGroupPo ocGroupPo = OcGroupPo.builder()
                .ocTenantInfoId(Long.parseLong(driverBase.getTenantKey()))
                .groupName(group.getName())
                .parentId(StringUtils.isEmpty(group.getParentKey())?null:Long.parseLong(group.getParentKey()))
                .extInfo(ObjectUtils.isEmpty(group.getExtInfo())?null:JSONObject.toJSONString(group.getExtInfo()))
                .build();
        ocGroupMapper.insert(ocGroupPo);
        group.setKey(String.valueOf(ocGroupPo.getId()));
        return DubboResult.newSuccess(group);
    }

    public DubboResult<GroupDto> queryGroupByKey(String key) {
        OcGroupPo ocGroupPo = ocGroupMapper.selectByPrimaryKey(Long.parseLong(key));
        return DubboResult.newSuccess(ocGroupPo.createDto());
    }

    public DubboResult<ArrayList<GroupDto>> queryGroupInfo(DriverBase driverBase, GroupFilter filter){
        ArrayList<GroupDto> groupDtos = new ArrayList<>();
                List<OcGroupPo> ocGroupPos = ocGroupMapper.selectGroup(OcGroupFilterDto.ocGroupFilterBuilder()
                .ocGroup(filter.getName())
                .tenantId(Long.parseLong(driverBase.getTenantKey()))
                .parentId(StringUtils.isEmpty(filter.getParentKey())?null:Long.parseLong(filter.getParentKey()))
                .build());

        if(!CollectionUtils.isEmpty(ocGroupPos)){
            groupDtos =  ocGroupPos.stream().map(OcGroupPo::createDto).collect(Collectors.toCollection(ArrayList::new));
        }
        return DubboResult.newSuccess(groupDtos);
    }

    public void deleteGroupByKey(String key){
        ocGroupMapper.deleteByPrimaryKey(Long.parseLong(key));
    }
}
