package com.chinaweal.aiccs.door.subscribe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinaweal.aiccs.door.entity.TUserportal;
import com.chinaweal.aiccs.door.service.ITUserportalService;
import com.chinaweal.aiccs.outside.api.XCDoorApi;
import com.chinaweal.aiccs.outside.util.DoorResult;
import com.dtflys.forest.http.ForestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户消息处理
 *
 * @author ymc
 * @date 2023年01月17日 16:32
 */
@Component
@Slf4j
public class UserXCMessageHandler implements XCMessageHandler {

    @Resource
    private XCDoorApi xcDoorApi;
    @Resource
    private ITUserportalService itUserportalService;

    @Override
    public boolean support(String opType) {
        return "2".equals(opType);
    }

    @Override
    public void handle(JSONObject paramObj) {
        ForestResponse<DoorResult<List<TUserportal>>> forestResponse = xcDoorApi.usersByUpdateAfter(paramObj.getLongValue("updateAfter"));
        if (forestResponse.isSuccess()) {
            DoorResult<List<TUserportal>> doorResult = forestResponse.getResult();

            if (doorResult.isQuerySuccess()) {

                List<TUserportal> tUserportalList = doorResult.getResult();

                for (TUserportal tUserportalDto : tUserportalList) {

                    try {
                        itUserportalService.saveUserByPortal(tUserportalDto);
                    } catch (Exception e) {
                        log.error("接收信创门户用户信息出错, 参数: {}。原因：", JSON.toJSONString(tUserportalDto), e);
                    }

                }
            }

        }
    }
}
