package com.djhk.uav.equ.service.impl;


import com.djhk.common.api.RemoteSystemFileService;
import com.djhk.common.api.domain.SysFile;
import com.djhk.common.api.domain.vo.FileVo;
import com.djhk.common.enums.BizCodeEnum;
import com.djhk.common.exception.ServiceException;
import com.djhk.common.utils.DateUtils;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.common.utils.bean.BeanUtils;
import com.djhk.uav.equ.domain.Metasploitable;
import com.djhk.uav.equ.mapper.EquModelMapper;
import com.djhk.uav.equ.mapper.EquModelParamMapper;
import com.djhk.uav.equ.mapper.EquTypeMapper;
import com.djhk.uav.equ.mapper.MetasploitableMapper;
import com.djhk.uav.equ.service.FileUploadService;
import com.djhk.uav.equ.service.IMetasploitableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 靶机Service业务层处理
 * 
 * @author huangqg
 * @date 2023-06-01
 */
@Service
public class MetasploitableServiceImpl implements IMetasploitableService
{
    @Autowired
    private EquModelMapper equModelMapper;
    @Autowired
    private MetasploitableMapper metasploitableMapper;

    @Autowired
    private RemoteSystemFileService sysFileMapper;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private EquModelParamMapper equModelParamMapper;

    @Autowired
    private RemoteSystemFileService remoteSystemFileService;

    @Autowired
    private EquTypeMapper equTypeMapper;

//    @Value("${minio.endpoint}")
    @Value("${minio.endpoint:http://47.109.103.110:9005}")
    private String baseUrl;
    /**
     * 查询靶机
     *
     * @param id 靶机主键
     * @return 靶机
//     */

    @Override
    public Metasploitable selectMetasploitableById(Long id)
    {
        Metasploitable metasploitable = metasploitableMapper.selectMetasploitableById(id);
        if (metasploitable != null && metasploitable.getPhoto() != null) {
//            SysFile iconFile = remoteSystemFileService.getFileInfo(metasploitable.getPhoto()).getData();
            SysFile iconFile = sysFileMapper.getFileInfo(metasploitable.getPhoto()).getData();
            if (iconFile != null) {
                metasploitable.setPhotoFilePath(baseUrl + "/" + iconFile.getBucketName() + iconFile.getFilePath());
                metasploitable.setPhotoFileName(iconFile.getFileName());
            }
        }
        return metasploitable;
    }

    /**
     * 查询靶机列表
     * 
     * @param metasploitable 靶机
     * @return 靶机
     */
    @Override
    public List<Metasploitable> selectMetasploitableList(Metasploitable metasploitable)
    {
        List<Metasploitable> metasploitables = metasploitableMapper.selectMetasploitableList(metasploitable);
        if (metasploitables!=null){
            for (Metasploitable item : metasploitables) {
                if(item.getPhoto()!=null){
                    SysFile iconFile = sysFileMapper.getFileInfo(item.getPhoto()).getData();
                    item.setPhotoFilePath(iconFile!=null?baseUrl+ "/" + iconFile.getBucketName() +iconFile.getFilePath():"");
                }
            }
        }
        return metasploitables;
    }

    @Override
    public FileVo uploadFile(MultipartFile file) {
        if(file != null){
            return fileUploadService.fileUpload(file, "", "Uav", BizCodeEnum.UAV_INFO.getCode(), "", "");
        }
        return null;
    }

    /**
     * 新增靶机
     *
     * @param metasploitable 靶机
     * @return 结果
     */
    @Override
    public int insertMetasploitable(Metasploitable metasploitable)
    {
        metasploitable.setCreateTime(DateUtils.getNowDate());
        metasploitable.setCreateBy(SecurityUtils.getUsername().toString());
        return metasploitableMapper.insertMetasploitable(metasploitable);
    }

    /**
     * 查询靶机编码
     *
     * @param modeCode 型号编码
     * @return 结果
     */
    @Override
    public List<Metasploitable> checkModelCode(String modeCode) {
        return metasploitableMapper.checkModelCode(modeCode);
    }

    /**
     * 修改靶机
     *
     * @param metasploitable 靶机
     * @return 结果
     */
    @Override
    public int updateMetasploitable(Metasploitable metasploitable)
    {
        metasploitable.setUpdateBy(SecurityUtils.getUsername().toString());
        return metasploitableMapper.updateMetasploitable(metasploitable);
    }

    /**
     * 批量删除靶机
     *
     * @param ids 需要删除的靶机主键
     * @return 结果
     */
    @Override
    public int deleteMetasploitableByIds(Long[] ids)
    {
        return metasploitableMapper.deleteMetasploitableByIds(ids);
    }

    @Override
    public int updateMetasploitableParamList(List<Map> ParamList) {
        return 0;
    }

    @Override
    public List<Metasploitable> selectMetasploitableExportList(Metasploitable metasploitable) {
        return null;
    }
//
//    /**
//     * 删除靶机信息
//     *
//     * @param modelId 靶机主键
//     * @return 结果
//     */
//    @Override
//    public int deleteEquModelByModelId(Long modelId)
//    {
//        return equModelMapper.deleteEquModelByModelId(modelId);
//    }


    /**
     * 新增修改靶机参数表
     *
     * @param
     * @return 结果
     */
//    @Override
//    public int updateEqumodelParamList(List<Map> paramList)
//    {
//        for (Map item:paramList){
//            if (item.get("paramSeq") != null && StringUtils.isNotBlank(item.get("paramSeq").toString())){
//                // 修改
//                EquModelParam equModelParam = new EquModelParam();
//                equModelParam.setParamSeq(Long.parseLong(item.get("paramSeq").toString()));
//                equModelParam.setParamCode(item.get("param_code").toString());
//                equModelParam.setParamName(item.get("param_name").toString());
//                equModelParam.setParamValue(item.get("param_value").toString());
//                equModelParam.setModelId(Long.parseLong(item.get("modelId").toString()));
//                equModelParamMapper.updateEquModelParam(equModelParam);
//            }else{
//                EquModelParam equModelParam = new EquModelParam();
//                equModelParam.setParamCode(item.get("param_code").toString());
//                equModelParam.setParamValue(item.get("param_value").toString());
//                equModelParam.setModelId(Long.parseLong(item.get("modelId").toString()));
//                equModelParamMapper.insertEquModelParam(equModelParam);
//            }
//        }
//        return paramList.size();
//    }


    @Override
    public String importData(List<Metasploitable> userList, Boolean isUpdateSupport){
        if (userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！",500);
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<Metasploitable> uavNames = metasploitableMapper.selectMetasploitableType();
//        List<String> typeNameList = equTypes.stream().map(EquType::getTypeName).collect(Collectors.toList());
        for (int i = 0; i < userList.size(); i++) {
            try
            {
                // 验证是否存在数据
                Metasploitable target = new Metasploitable();
                BeanUtils.copyBeanProp(target,userList.get(i));
//                if (!typeNameList.contains(userList.get(i).getTypeId()))
//                {
//                    failureNum++;
//                    failureMsg.append("第" + (i+1) + "条数据类别标识【"+ userList.get(i).getTypeId()+"】不存在，如要新增请联系管理员");
//                    continue;
//                }
                /*for (Metasploitable metasploitable : uavNames) {
                    if (!metasploitable.getUavName().contains(userList.get(i).getUavName()))
                    {
                        target.setCreateTime(DateUtils.getNowDate());
                        target.setCreateBy(SecurityUtils.getUsername().toString());
                        metasploitableMapper.insertMetasploitables(target);
                        successNum++;
                    }else
                    {
                        failureNum++;
                        failureMsg.append("第" + (i+1) + "条数据" +  "无人机已存在,");
                    }
                }*/

                target.setCreateTime(DateUtils.getNowDate());
                target.setCreateBy(SecurityUtils.getUsername().toString());
                metasploitableMapper.insertMetasploitables(target);
                successNum++;

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "第" + (i+1) + "条数据 " + "导入失败,";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "数据导入失败！共" + failureNum + "条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString(),10002);
        }
        else
        {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }


}
