package com.docus.server.collection.receiver;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.docus.server.collection.dto.DeptDto;
import com.docus.server.collection.rpc.MzZyyRocCurrWebCommonRpc;
import com.docus.server.collection.service.IPowerDeptService;
import com.docus.server.collection.service.IPowerUserService;
import com.docus.server.collection.xml.parsers.MzZyParser;
import com.neusoft.nhip.ibmmq.jms.JmsIbmListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author jiashi
 * 梅州中医医院 MQ 监听
 */
@Component
@Slf4j
public class MzZyMqReceiver {

    @Autowired
    private IPowerUserService powerUserService;
    @Autowired
    private IPowerDeptService powerDeptService;
    @Autowired
    private MzZyyRocCurrWebCommonRpc mzZyyRocCurrWebCommonRpc;

    /**
     * 订阅队列（注册科室）
     *
     * @param message 科室信息
     */
    @JmsIbmListener(destination = "TJ_createDepartment")
    public void revCreateDepartment(String message) {
        log.info("注册科室 队列接收信息：{}", message);
        try {
            DeptDto deptDto = MzZyParser.parseCreateDept(message);
            // 只保存住院的，如果接口异常也保存
            JSONObject jsonObject = mzZyyRocCurrWebCommonRpc.queryDept(deptDto.getDeptCode(), "I");
            if (jsonObject == null) {
                powerDeptService.register(deptDto);
                return;
            }
            boolean success = "200".equals(jsonObject.getString("code"));
            JSONArray data = jsonObject.getJSONArray("data");
            if (success && data.size() > 0) {
                powerDeptService.register(deptDto);
            }

        } catch (Exception ex) {
            log.error("处理注册科室 队列接收信息:{},出现异常:{}", message, ex.getMessage());
        }

    }


    /**
     * 订阅队列（变更科室）
     *
     * @param message 科室信息
     */
    @JmsIbmListener(destination = "TJ_updateDepartment")
    public void revUpdateDepartment(String message) {
        log.info("变更科室 队列接收信息：{}", message);
        try {
            DeptDto deptDto = MzZyParser.parseUpdateDept(message);
            // 只保存住院的，如果接口异常也保存
            JSONObject jsonObject = mzZyyRocCurrWebCommonRpc.queryDept(deptDto.getDeptCode(), "I");
            if (jsonObject == null) {
                powerDeptService.register(deptDto);
                return;
            }
            boolean success = "200".equals(jsonObject.getString("code"));
            JSONArray data = jsonObject.getJSONArray("data");
            if (success && data.size() > 0) {
                powerDeptService.register(deptDto);
            }
        } catch (Exception ex) {
            log.error("处理变更科室 队列接收信息:{},出现异常:{}", message, ex.getMessage());
        }

    }


    /**
     * 订阅队列（注册人员）
     *
     * @param message 人员信息
     */
    @JmsIbmListener(destination = "TJ_createPractitioner")
    public void revCreatePractitioner(String message) {
        log.info("人员注册 队列接收信息：{}", message);
        /*
        try {
            UserDto userDto=MzZyParser.parseCreateUser(message);
            powerUserService.register(userDto);
        }catch (Exception ex){
            log.error("处理人员注册 队列接收信息:{},出现异常:{}",message,ex.getMessage());
        }
         */

    }


    /**
     * 订阅队列（变更人员）
     *
     * @param message 人员信息
     */
    @JmsIbmListener(destination = "TJ_updatePractitioner")
    public void revUpdatePractitioner(String message) {
        log.info("人员变更 队列接收信息：{}", message);
        /*
        try {
            UserDto userDto=MzZyParser.parseUpdateUser(message);
            powerUserService.register(userDto);
        }catch (Exception ex){
            log.error("处理人员注册 队列接收信息:{},出现异常:{}",message,ex.getMessage());
        }
         */

    }
}
