/*
 * 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.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.saas.db.TenantPaginationService;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.device.bus.feign.TenantFeign;
import net.northking.cctp.device.db.dao.CdMobileDeviceDao;
import net.northking.cctp.device.db.entity.CdMobileDevice;
import net.northking.cctp.device.db.service.CdMobileDeviceService;
import net.northking.cctp.device.dto.device.*;
import net.northking.cctp.device.dto.tenant.TenantDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

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


/**
 * 移动设备信息表
 *
 * <p>文件内容由代码生成器产生，请不要手动修改！ <br>
 * createdate:  2022-09-21 14:10:19  <br>
 *
 * @author: maven-cctp-plugin  <br>
 * @since: 1.0 <br>
 */
@Service
@ConditionalOnMissingBean(name = "CdMobileDeviceServiceImpl")
public class CdMobileDeviceServiceImpl extends TenantPaginationService<CdMobileDevice> implements CdMobileDeviceService {
    private static final Logger logger = LoggerFactory.getLogger(CdMobileDeviceServiceImpl.class);

    private final CdMobileDeviceDao cdMobileDeviceDao;

    public CdMobileDeviceServiceImpl(CdMobileDeviceDao cdMobileDeviceDao) {
        this.cdMobileDeviceDao = cdMobileDeviceDao;
    }

    public BasicDao<CdMobileDevice> getDao() {
        return cdMobileDeviceDao;
    }

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

    @Autowired
    private EntityNameCache entityNameCache;

    @Autowired
    private SaaSTenantCache saaSTenantCache;

    @Autowired
    private TenantFeign tenantFeign;

    @Override
    public Pagination<CdMobileDeviceDetailDto> queryPages(QueryByPage<CdMobileDeviceQueryDto> queryByPage) {
        DefaultPagination<CdMobileDeviceDetailDto> result = new DefaultPagination<>();
        Page<CdMobileDeviceDetailDto> page = PageMethod.startPage(queryByPage.getPageNo(), queryByPage.getPageSize());
        List<CdMobileDeviceDetailDto> list = this.cdMobileDeviceDao.queryPage(queryByPage.getQuery());
        for (CdMobileDeviceDetailDto dto : list) {
            if (dto.getTenantId() != null) {
                ResultWrapper<TenantDetail> tenant = tenantFeign.findByPK(dto.getTenantId());
                dto.setTenantName(tenant.getData().getTenantName());
            }
            if (dto.getProjectId() != null) {
                String proName = entityNameCache.translateById(EntityNameCatalog.MONSTER_SYSTEM, dto.getProjectId());
                dto.setProjectName(proName);
            }
        }
        result.setPageNo(queryByPage.getPageNo());
        result.setPageSize(queryByPage.getPageSize());
        result.setRecords(list);
        result.setRecordCount(page.getTotal());
        return result;
    }

    @Override
    public Pagination<ScriptResultDto> queryDeviceByPro(QueryByPage<CdMobileDeviceScriptDto> queryByPage) {
        queryByPage.getQuery().setTenantId(NKSecurityContext.getTenantId());
        DefaultPagination<ScriptResultDto> result = new DefaultPagination<>();
        Page<CdMobileDeviceDetailDto> page = PageMethod.startPage(queryByPage.getPageNo(), queryByPage.getPageSize());
        //默认全选与全选的情况
        List<CdMobileDeviceDetailDto> list = new ArrayList<>();
        if (queryByPage.getQuery().getTenantCheck() == null || queryByPage.getQuery().getTenantCheck().size() != 1) {
            list = this.cdMobileDeviceDao.queryAllMobDevice(queryByPage.getQuery());
        } else if (queryByPage.getQuery().getTenantCheck().get(0).equals("1")) {
            //公有
            list = this.cdMobileDeviceDao.queryPublicMobDevice(queryByPage.getQuery());
        } else if (queryByPage.getQuery().getTenantCheck().get(0).equals("2")) {
            //私有
            list = this.cdMobileDeviceDao.queryPrivateMobDevice(queryByPage.getQuery());
        }
        List<ScriptResultDto> resultDtoList = new ArrayList<>();
        for (CdMobileDeviceDetailDto dto : list) {
            if (dto.getTenantId() != null) {
                Tenant tenant = saaSTenantCache.findTenantById(dto.getTenantId());
                dto.setTenantName(tenant.getTenantName());
            }
            if (dto.getProjectId() != null) {
                String proName = entityNameCache.translateById(EntityNameCatalog.MONSTER_SYSTEM, dto.getProjectId());
                dto.setProjectName(proName);
            }
            ScriptResultDto resultDto = new ScriptResultDto();
            BeanUtils.copyProperties(dto,resultDto);
            //System
            StringBuffer sb = new StringBuffer();
            switch (dto.getPlatform()) {
                case "0":
                    sb.append("Android");
                    resultDto.setPlatform("0");
                    break;
                case "1":
                    sb.append("IOS");
                    resultDto.setPlatform("1");
                    break;
                case "2":
                    sb.append("HarmonyOS");
                    resultDto.setPlatform("2");
                    break;
            }
            sb.append(dto.getPlatformVersion());
            resultDto.setSystemCode(sb.toString());
            if (dto.getProjectId()==null) {
                resultDto.setTenantCheck("1");
            }else {
                resultDto.setTenantCheck("2");
            }
            //设备使用中返回使用人
            if (resultDto.getUsageStatus().equals("2")){
                if (dto.getUserBy()!=null && dto.getUserId()!=null){
                    if (dto.getUserBy().equals("0")){
                        String translate = entityNameCache.translateById(EntityNameCatalog.MONSTER_USER, dto.getUserId());
                        resultDto.setUserId(translate);
                    }  else if (dto.getUserBy().equals("1")){
                        resultDto.setUserId("执行调用");
                    }
                }
            }else {
                resultDto.setUserId(null);
            }
            resultDtoList.add(resultDto);
        }
        result.setPageNo(queryByPage.getPageNo());
        result.setPageSize(queryByPage.getPageSize());
        result.setRecords(resultDtoList);
        result.setRecordCount(page.getTotal());
        return result;
    }

    @Override
    public List<CdMobileDevice> queryDeviceList(List<String> ids) {
        return this.cdMobileDeviceDao.queryDeviceList(ids);
    }

    @Override
    public List<CdMobileDevice> queryRestartList(List<String> ids) {
        return this.cdMobileDeviceDao.queryMobileList(ids);
    }

    @Override
    public List<String> queryCtrlIds(List<String> ids) {
        return this.cdMobileDeviceDao.queryCtrlIds(ids);
    }

    @Override
    public void updateCtrlByDevice(List<String> ids,String ctrlId) {
        this.cdMobileDeviceDao.updateDeviceCtrl(ids,ctrlId);
    }

    @Override
    public Pagination<ScriptResultDto> queryDeviceByPro2(QueryByPage<CdMobileDeviceScriptDto2> queryByPage) {
        queryByPage.getQuery().setTenantId(NKSecurityContext.getTenantId());
        DefaultPagination<ScriptResultDto> result = new DefaultPagination<>();
        Page<CdMobileDeviceDetailDto> page = PageMethod.startPage(queryByPage.getPageNo(), queryByPage.getPageSize());
        //默认全选与全选的情况
        List<CdMobileDeviceDetailDto> list = new ArrayList<>();
        if (queryByPage.getQuery().getTenantCheck() == null || queryByPage.getQuery().getTenantCheck().size() != 1) {
            list = this.cdMobileDeviceDao.queryAllMobDevice2(queryByPage.getQuery());
        } else if (queryByPage.getQuery().getTenantCheck().get(0).equals("1")) {
            //公有
            list = this.cdMobileDeviceDao.queryPublicMobDevice2(queryByPage.getQuery());
        } else if (queryByPage.getQuery().getTenantCheck().get(0).equals("2")) {
            //私有
            list = this.cdMobileDeviceDao.queryPrivateMobDevice2(queryByPage.getQuery());
        }
        List<ScriptResultDto> resultDtoList = new ArrayList<>();
        for (CdMobileDeviceDetailDto dto : list) {
            if (dto.getTenantId() != null) {
                Tenant tenant = saaSTenantCache.findTenantById(dto.getTenantId());
                dto.setTenantName(tenant.getTenantName());
            }
            if (dto.getProjectId() != null) {
                String proName = entityNameCache.translateById(EntityNameCatalog.MONSTER_SYSTEM, dto.getProjectId());
                dto.setProjectName(proName);
            }
            ScriptResultDto resultDto = new ScriptResultDto();
            BeanUtils.copyProperties(dto,resultDto);
            //System
            StringBuffer sb = new StringBuffer();
            switch (dto.getPlatform()) {
                case "0":
                    sb.append("Android");
                    resultDto.setPlatform("0");
                    break;
                case "1":
                    sb.append("IOS");
                    resultDto.setPlatform("1");
                    break;
                case "2":
                    sb.append("HarmonyOS");
                    resultDto.setPlatform("2");
                    break;
            }
            sb.append(dto.getPlatformVersion());
            resultDto.setSystemCode(sb.toString());
            if (dto.getProjectId()==null) {
                resultDto.setTenantCheck("1");
            }else {
                resultDto.setTenantCheck("2");
            }
            //设备使用中返回使用人
            if (resultDto.getUsageStatus().equals("2")){
                if (dto.getUserBy()!=null && dto.getUserId()!=null){
                    if (dto.getUserBy().equals("0")){
                        String translate = entityNameCache.translateById(EntityNameCatalog.MONSTER_USER, dto.getUserId());
                        resultDto.setUserId(translate);
                    }  else if (dto.getUserBy().equals("1")){
                        resultDto.setUserId("执行调用");
                    }
                }
            }else {
                resultDto.setUserId(null);
            }
            resultDtoList.add(resultDto);
        }
        result.setPageNo(queryByPage.getPageNo());
        result.setPageSize(queryByPage.getPageSize());
        result.setRecords(resultDtoList);
        result.setRecordCount(page.getTotal());
        return result;
    }

    @Override
    public List<CdMobileDevice> queryMobileDeviceList(List<String> ids) {
        return this.cdMobileDeviceDao.queryMobileList(ids);
    }





}

