package com.skr.mdm.api.index.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import com.skr.mdm.api.index.service.DeviceAssetService;
import com.skr.mdm.api.license.MdmLicenseVerify;
import com.skr.mdm.bean.bo.EquipmentBO;
import com.skr.mdm.bean.vo.index.DeviceAssetInfoVO;
import com.skr.mdm.bean.vo.index.IndexEquipmentStateVO;
import com.skr.mdm.bean.vo.index.IndexEquipmentTypeVO;
import com.skr.mdm.dao.AuCompanyDao;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.AuCompanyDO;
import com.skr.mdm.enums.EquipmentStateEnum;
import com.skr.mdm.enums.EquipmentStatusEnum;
import com.skr.mdm.enums.EquipmentTypeEnum;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.util.SmUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ProjectName: mdm_new
 * @Package: com.skr.mdm.largeScreenManagement.service.serviceImpl
 * @ClassName: DeviceAssetServiceImpl
 * @Author: Tty
 * @Description:
 * @Date: 2020/9/9 16:24
 * @Version: 1.0
 */
@Slf4j
@Service
public class DeviceAssetServiceImpl implements DeviceAssetService {

	@Value("${mdm.license.company}")
	private Boolean companyLicense;

	@Autowired
	private DeviceEquipmentDao deviceEquipmentDao;
	@Autowired
	private AuCompanyDao companyDao;

	@Override
	public ResponseResult selectByStyleSelect(LoginAdminDTO admin) throws Exception {
		Long authorizationNumber;
		String expirationTime = "";
		if (companyLicense) {
			AuCompanyDO companyDO = companyDao.selectById(admin.getCompanyId());
			SM2 sm2 = SmUtils.sm2();
			authorizationNumber = Long.valueOf(StrUtil.utf8Str(sm2.decryptFromBcd(companyDO.getAllowUseNumber(), KeyType.PrivateKey)));
			expirationTime = StrUtil.utf8Str(sm2.decryptFromBcd(companyDO.getExpirationTime(), KeyType.PrivateKey));
			expirationTime = DateUtil.format(new Date(Long.parseLong(expirationTime)), "yyyy-MM-dd HH:mm:ss");
		} else {
			authorizationNumber = Long.valueOf(MdmLicenseVerify.getAllowUseNumber());
			expirationTime = DateUtil.format(MdmLicenseVerify.getExpireTime(), "yyyy-MM-dd HH:mm:ss");
		}
		List<EquipmentBO> equipmentBOList = deviceEquipmentDao.listEquipmentBO(admin.getCompanyId());
		if (CollectionUtils.isEmpty(equipmentBOList)) {
			DeviceAssetInfoVO deviceAssetInfoVO = DeviceAssetInfoVO.builder()
					.allowUseNumber(authorizationNumber)
					.expirationTime(expirationTime)
					.alreadyUseNumber(equipmentBOList.size())
					.residueUseNumber(authorizationNumber - equipmentBOList.size())
					.build();
			return ResponseResult.success(deviceAssetInfoVO);
		}
		IndexEquipmentStateVO indexEquipmentStateVO = new IndexEquipmentStateVO();
		IndexEquipmentTypeVO indexEquipmentTypeVO = new IndexEquipmentTypeVO();
		for (EquipmentBO equipmentBO : equipmentBOList) {
			Integer equipmentState = equipmentBO.getEquipmentState();
			Integer equipmentType = equipmentBO.getEquipmentType();

			if (EquipmentStateEnum.INACTIVATED.getState().equals(equipmentState)) {
				indexEquipmentStateVO.setInactivatedCount(indexEquipmentStateVO.getInactivatedCount() + 1);
			} else if (EquipmentStateEnum.WS_ONLINE.getState().equals(equipmentState) || EquipmentStateEnum.HTTP_ONLINE.getState().equals(equipmentState)) {
				indexEquipmentStateVO.setOnlineCount(indexEquipmentStateVO.getOnlineCount() + 1);
			} else if (EquipmentStateEnum.OFFLINE.getState().equals(equipmentState)) {
				indexEquipmentStateVO.setOfflineCount(indexEquipmentStateVO.getOfflineCount() + 1);
			} else if (EquipmentStatusEnum.LOST.getState().equals(equipmentState)) {
				indexEquipmentStateVO.setLostCount(indexEquipmentStateVO.getLostCount() + 1);
			} else if (EquipmentStatusEnum.ELIMINATED.getState().equals(equipmentState)) {
				indexEquipmentStateVO.setEliminateCount(indexEquipmentStateVO.getEliminateCount() + 1);
			}

			if (EquipmentTypeEnum.CELL_PHONE.getType().equals(equipmentType)) {
				indexEquipmentTypeVO.setPhoneCount(indexEquipmentTypeVO.getPhoneCount() + 1);
			} else if (EquipmentTypeEnum.WRISTBAND.getType().equals(equipmentType)) {
				indexEquipmentTypeVO.setBraceletCount(indexEquipmentTypeVO.getBraceletCount() + 1);
			} else if (EquipmentTypeEnum.RECORDER.getType().equals(equipmentType)) {
				indexEquipmentTypeVO.setRecorderCount(indexEquipmentTypeVO.getRecorderCount() + 1);
			} else if (EquipmentTypeEnum.WATCHES.getType().equals(equipmentType)) {
				indexEquipmentTypeVO.setWatchesCount(indexEquipmentTypeVO.getWatchesCount() + 1);
			} else if (EquipmentTypeEnum.OTHER.getType().equals(equipmentType)) {
				indexEquipmentTypeVO.setOtherCount(indexEquipmentTypeVO.getOtherCount() + 1);
			}
		}

		//  封装视图信息
		DeviceAssetInfoVO deviceAssetInfoVO = DeviceAssetInfoVO.builder()
				.allowUseNumber(authorizationNumber)
				.expirationTime(expirationTime)
				.alreadyUseNumber(equipmentBOList.size())
				.residueUseNumber(authorizationNumber - equipmentBOList.size())
				.indexEquipmentStateVO(indexEquipmentStateVO)
				.indexEquipmentTypeVO(indexEquipmentTypeVO)
				.build();
		return ResponseResult.success(deviceAssetInfoVO);
	}
}
