package org.security.trans.feign;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.ht.feign.IHtClient;
import org.security.trans.common.service.FlowableService;
import org.security.trans.securityCommandCenter.entity.SccAnswerAlarm;
import org.security.trans.securityCommandCenter.service.ISccAlarmPushService;
import org.security.trans.securityCommandCenter.service.ISccAnswerAlarmService;
import org.security.trans.securityCommandCenter.service.ISccCheckService;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * @Author: chenchao
 * @Create: 2022/8/26
 * @Description:
 */
@NonDS
@ApiIgnore
@RestController
@AllArgsConstructor
@Slf4j
public class HtClient implements IHtClient {

	private final ISccAnswerAlarmService sccAnswerAlarmService;
	private final ISccAlarmPushService sccAlarmPushService;
	private final ISccCheckService sccCheckService;

	@Override
	@GetMapping(UPDATE_STATE_BY_ID)
	public R updateStateById(String serviceBeanName, String id, String state) {
		ApplicationContext context = SpringUtil.getContext();
		if (!context.containsBean(serviceBeanName)) {
			throw new ServiceException("找不到服务实例“" + serviceBeanName + "”，请重新配置后再试！");
		}
		FlowableService service = (FlowableService) context.getBean(serviceBeanName);
		Boolean success = service.updateStateById(id, state);
		return R.status(success);
	}

	@Override
	@GetMapping(SEND_ALARM_NOTICE)
	public R sendAlarmNotice(String answerAlarmId) {
		SccAnswerAlarm sccAnswerAlarm = sccAnswerAlarmService.getById(answerAlarmId);
		boolean success = sccAlarmPushService.senAlarmNotice(sccAnswerAlarm);
		return R.status(success);
	}

	@Override
	@GetMapping(SEND_SCC_CHECK_NOTICE)
	public R sendSccCheckNotice(String sccCheckId) {
		boolean success = sccCheckService.createdCheackNotice(sccCheckId);
		return R.status(success);
	}

	@Override
	@PostMapping(AFTER_FLOW_FINISHED)
	public R afterFlowFinished(String serviceBeanName, String id, Boolean pass, Map<String, Object> variables) {
		ApplicationContext context = SpringUtil.getContext();
		if (!context.containsBean(serviceBeanName)) {
			throw new ServiceException("找不到服务实例“" + serviceBeanName + "”，请重新配置后再试！");
		}
		FlowableService service = (FlowableService) context.getBean(serviceBeanName);
		return service.afterFlowFinished(id, pass, variables);
	}
}
