package com.skr.mdm.service.impl.certificate;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.skr.mdm.annotation.CmdHandler;
import com.skr.mdm.bean.so.SysChangeCertificateSO;
import com.skr.mdm.constant.CmdTypeConstants;
import com.skr.mdm.dao.SysChangeCertificateDao;
import com.skr.mdm.dao.SysChangeCertificateUserDao;
import com.skr.mdm.dto.CheckCertificateDTO;
import com.skr.mdm.dto.SysChangeCertificateDTO;
import com.skr.mdm.entity.SysChangeCertificateDO;
import com.skr.mdm.entity.SysChangeCertificateUserDO;
import com.skr.mdm.enums.ResultCodeEnum;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.service.CmdHandlerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * @ProjectName: mdm_new
 * @Package: com.skr.mdm.service.impl.certificate
 * @ClassName: SysChangeCertificate
 * @Author: Tty
 * @Description: 服务器变更
 * @Date: 2021/3/10 11:04
 * @Version: 1.0
 */
@Slf4j
@CmdHandler(cmdType = CmdTypeConstants.SYS_CHANGE_CERTIFICATE)
public class SysChangeCertificateImpl implements CmdHandlerService {

	@Autowired
	private SysChangeCertificateUserDao sysChangeCertificateUserDao;
	@Autowired
	private SysChangeCertificateDao sysChangeCertificateDao;

	@Override
	public ResponseResult cmdHandler(Integer equipmentId, String data) {
		JSONArray jsonArray = JSONUtil.parseArray(data);
		List<SysChangeCertificateDTO> sysChangeCertificateDTOList = JSONUtil.toList(jsonArray, SysChangeCertificateDTO.class);
		List<Integer> cIdList = new ArrayList<>(sysChangeCertificateDTOList.size());
		sysChangeCertificateDTOList.forEach(sysChangeCertificateDTO -> {
			LambdaUpdateChainWrapper<SysChangeCertificateUserDO> updateChainWrapper = new LambdaUpdateChainWrapper<>(sysChangeCertificateUserDao);
			updateChainWrapper
					.set(SysChangeCertificateUserDO::getState, sysChangeCertificateDTO.getState())
					.eq(SysChangeCertificateUserDO::getChangeCertificateId, sysChangeCertificateDTO.getSysChangeId())
					.eq(SysChangeCertificateUserDO::getEquipmentId, equipmentId)
					.update();
			cIdList.add(sysChangeCertificateDTO.getCid());
		});
		return ResponseResult.success(JSONUtil.toJsonStr(cIdList));
	}

	public ResponseResult changeCertificateCheck(Integer equipmentId, String data) {
		try {
			CheckCertificateDTO checkCertificateDTO = JSONUtil.toBean(data, CheckCertificateDTO.class);
			if (Objects.isNull(checkCertificateDTO)) {
				return ResponseResult.failure(ResultCodeEnum.SYS_CHANGE_CERTIFICATE_NOT_EXIT);
			} else {
				SysChangeCertificateDO sysChangeCertificateDO = sysChangeCertificateDao
						.queryChangeCertificate(checkCertificateDTO.getIp()
								, checkCertificateDTO.getClientPort()
								, checkCertificateDTO.getSocketPort()
								, equipmentId);
				if (Objects.isNull(sysChangeCertificateDO)) {
					return ResponseResult.failure(ResultCodeEnum.SYS_CHANGE_CERTIFICATE_NOT_EXIT);
				} else {
					SysChangeCertificateSO sysChangeCertificateSO = new SysChangeCertificateSO();
					BeanUtils.copyProperties(sysChangeCertificateDO, sysChangeCertificateSO);
					return ResponseResult.success(sysChangeCertificateSO);
				}
			}
		} catch (Exception e) {
			log.error("客户端检测服务器变更异常:" + equipmentId + data, e);
			return ResponseResult.failure();
		}
	}
}