/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.
 *
 */
package net.northking.cctp.device.db.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.cache.EntityNameCatalog;
import net.northking.cctp.common.db.BasicDao;
import net.northking.cctp.common.db.DefaultPagination;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.db.entity.EntityService;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.common.saas.Tenant;
import net.northking.cctp.common.saas.cache.SaaSTenantCache;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.device.bus.feign.CctpPlatformFeign;
import net.northking.cctp.device.bus.feign.TenantFeign;
import net.northking.cctp.device.config.SystemConfig;
import net.northking.cctp.device.db.dao.CdPcDeviceDao;
import net.northking.cctp.device.db.entity.CdPcDevice;
import net.northking.cctp.device.db.service.CdPcDeviceService;
import net.northking.cctp.device.dto.pcDevice.CdPcDeviceDetailDto;
import net.northking.cctp.device.dto.pcDevice.CdPcDeviceDto;
import net.northking.cctp.device.dto.pcDevice.CdPcDeviceScriptDto;
import net.northking.cctp.device.dto.pcDevice.ScriptPcResultDto;
import net.northking.cctp.device.dto.tenant.TenantDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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


/**
 * PC设备管理
 *
 * <p>文件内容由代码生成器产生，请不要手动修改！ <br>
 * createdate:  2022-09-26 10:56:47  <br>
 *
 * @author: maven-cctp-plugin  <br>
 * @since: 1.0 <br>
 */
@Service
@ConditionalOnMissingBean(name = "CdPcDeviceServiceImpl")
public class CdPcDeviceServiceImpl extends EntityService<CdPcDevice> implements CdPcDeviceService {
    private static final Logger logger = LoggerFactory.getLogger(CdPcDeviceServiceImpl.class);

    private final CdPcDeviceDao cdPcDeviceDao;

    public CdPcDeviceServiceImpl(CdPcDeviceDao cdPcDeviceDao) {
        this.cdPcDeviceDao = cdPcDeviceDao;
    }

    public BasicDao<CdPcDevice> getDao() {
        return cdPcDeviceDao;
    }

    public CdPcDevice createEntity() {
        return new CdPcDevice();
    }

    @Autowired
    private EntityNameCache entityNameCache;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SaaSTenantCache saaSTenantCache;

    @Autowired
    private SystemConfig systemConfig;

    @Autowired
    private CctpPlatformFeign cctpPlatformFeign;

    @Autowired
    private TenantFeign tenantFeign;

    @Override
    public Pagination<CdPcDeviceDetailDto> queryPages(QueryByPage<CdPcDeviceDto> dto) {
        //选出对应的系统类型
        if (dto.getQuery().getOsName()!=null){
            if (dto.getQuery().getOsName().size()>0){
                List<String> osName = new ArrayList<>();
                for (String temp : dto.getQuery().getOsName()) {
                    switch (temp){
                        case "1":
                            osName.addAll(systemConfig.getWin7());
                            break;
                        case "2":
                            osName.addAll(systemConfig.getWin10());
                            break;
                        case "3":
                            osName.addAll(systemConfig.getWin11());
                            break;
                        case "4":
                            osName.addAll(systemConfig.getMACOS());
                            break;
                        case "5":
                            osName.addAll(systemConfig.getLinux());
                            break;
                        case "6":
                            osName.addAll(systemConfig.getWinServer());
                            break;
                        default:
                            break;
                    }
                }
                dto.getQuery().setOsName(osName);
            }
        }
        DefaultPagination<CdPcDeviceDetailDto> pageQuery = new DefaultPagination<>();
        Page<CdPcDeviceDetailDto> page = PageMethod.startPage(dto.getPageNo(), dto.getPageSize());
        List<CdPcDeviceDetailDto> result = cdPcDeviceDao.pagesQuery(dto.getQuery());
        for (CdPcDeviceDetailDto detailDto : result) {
            if (!StringUtils.isEmpty(detailDto.getProjectId())) {
                detailDto.setProjectName(entityNameCache.translateById(EntityNameCatalog.MONSTER_SYSTEM, detailDto.getProjectId()));
            }
            if (!StringUtils.isEmpty(detailDto.getTenantId())) {
                ResultWrapper<TenantDetail> tenant = tenantFeign.findByPK(detailDto.getTenantId());
                detailDto.setTenantName(tenant.getData().getTenantName());
            }
            if (!StringUtils.isEmpty(detailDto.getRamSize())){
                String size = detailDto.getRamSize();
                String[] split = size.split("M");
                double splitSize = 0;
                try {
                    if (!StringUtils.isEmpty(split[0])) {
                        double num = Integer.parseInt(split[0]);
                        splitSize =  num / 1024;
                    }
                } catch (NumberFormatException e) {
                    logger.error("转换异常:",e);
                }
                String ramSize = String.format("%.2f",splitSize)+ "G";
                detailDto.setRamSize(ramSize);
            }
        }
        pageQuery.setPageNo(dto.getPageNo());
        pageQuery.setPageSize(dto.getPageSize());
        pageQuery.setRecordCount(page.getTotal());
        pageQuery.setRecords(result);
        return pageQuery;
    }


    @Override
    public Pagination<ScriptPcResultDto> scriptPcDevice(QueryByPage<CdPcDeviceScriptDto> queryByPage) {
        if (queryByPage.getQuery().getOsNames() != null && queryByPage.getQuery().getOsNames().size() > 0) {
            //选系统
            List<String> osName = new ArrayList<>();
            for (String temp : queryByPage.getQuery().getOsNames()) {
                switch (temp) {
                    case "1":
                        osName.addAll(systemConfig.getWin7());
                        break;
                    case "2":
                        osName.addAll(systemConfig.getWin10());
                        break;
                    case "3":
                        osName.addAll(systemConfig.getWin11());
                        break;
                    case "4":
                        osName.addAll(systemConfig.getMACOS());
                        break;
                    case "5":
                        osName.addAll(systemConfig.getLinux());
                        break;
                    default:
                        break;
                }
            }
            queryByPage.getQuery().setOsNames(osName);
        }
        DefaultPagination<ScriptPcResultDto> pageQuery = new DefaultPagination<>();
        Page<ScriptPcResultDto> page = PageMethod.startPage(queryByPage.getPageNo(), queryByPage.getPageSize());
        List<ScriptPcResultDto> result = new ArrayList<>();
        //判断设备所属
        queryByPage.getQuery().setTenantId(NKSecurityContext.getTenantId());
        //默认全选与全选的情况
        if (queryByPage.getQuery().getTenantCheck() == null || queryByPage.getQuery().getTenantCheck().size() != 1) {
            result = this.cdPcDeviceDao.queryAllPcDevice(queryByPage.getQuery());
        } else if (queryByPage.getQuery().getTenantCheck().get(0).equals("1")) {
            //公有
            result = this.cdPcDeviceDao.queryPublicPcDevice(queryByPage.getQuery());
        } else if (queryByPage.getQuery().getTenantCheck().get(0).equals("2")) {
            //私有
            result = this.cdPcDeviceDao.queryPrivatePcDevice(queryByPage.getQuery());
        }

        for (ScriptPcResultDto detailDto : result) {
            if (!StringUtils.isEmpty(detailDto.getProjectId())) {
                detailDto.setProjectName(entityNameCache.translateById(EntityNameCatalog.MONSTER_SYSTEM, detailDto.getProjectId()));
                detailDto.setTenantCheck("2");
            } else {
                detailDto.setTenantCheck("1");
            }
            if (!StringUtils.isEmpty(detailDto.getTenantId())) {
                Tenant tenant = saaSTenantCache.findTenantById(detailDto.getTenantId());
                detailDto.setTenantName(tenant.getTenantName());
            }
            if (detailDto.getUsageStatus().equals("2")){
                if (detailDto.getUserBy()!=null && detailDto.getUserId()!=null) {
                    if (detailDto.getUserBy().equals("0")) {
                        String translate = entityNameCache.translateById(EntityNameCatalog.MONSTER_USER, detailDto.getUserId());
                        detailDto.setUserId(translate);
                    }else if (detailDto.getUserBy().equals("1")){
                        detailDto.setUserId("执行调用");
                    }
                }
            }else{
                detailDto.setUserId(null);
            }
        }
        pageQuery.setPageNo(queryByPage.getPageNo());
        pageQuery.setPageSize(queryByPage.getPageSize());
        pageQuery.setRecordCount(page.getTotal());
        pageQuery.setRecords(result);
        return pageQuery;
    }

    @Override
    public List<CdPcDevice> queryDevice() {
        return cdPcDeviceDao.queryDevice();
    }

    @Override
    public Integer selectPcDevice(String ipAddr) {
        return this.cdPcDeviceDao.queryDeviceIpAddr(ipAddr);
    }




}

