package com.djhk.uav.equ.controller;

import com.djhk.common.annotation.Log;
import com.djhk.common.api.domain.vo.FileVo;
import com.djhk.common.core.controller.BaseController;
import com.djhk.common.core.domain.AjaxResult;
import com.djhk.common.core.domain.R;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.common.enums.BizCodeEnum;
import com.djhk.common.enums.BusinessType;
import com.djhk.common.exception.ServiceException;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.common.utils.poi.ExcelUtil;
import com.djhk.uav.equ.domain.*;
import com.djhk.uav.equ.service.FileUploadService;
import com.djhk.uav.equ.service.IEquModelService;
import com.djhk.uav.equ.service.IEquTypeService;
import com.djhk.uav.equ.service.IMetasploitableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 靶机Controller
 * 
 * @author huangqg
 * @date 2023-06-01
 */
@RestController
@RequestMapping("/metasploitable")
public class MetasploitableController extends BaseController
{
    @Autowired
    private IEquModelService equModelService;

    @Autowired
    private IMetasploitableService metasploitableService;
//
    @Autowired
    private IEquTypeService equTypeService;

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 查询靶机列表
     */
//    @RequiresPermissions("uav:equmodel:list")
    @GetMapping("/list")
    public TableDataInfo list(Metasploitable metasploitable)
    {
        startPage();
        List<Metasploitable> list = metasploitableService.selectMetasploitableList(metasploitable);
        return getDataTable(list);
    }

    @Log(title = "靶机导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Metasploitable metasploitable) {
        List<Metasploitable> list = metasploitableService.selectMetasploitableList(metasploitable);
        ExcelUtil<Metasploitable> util = new ExcelUtil<Metasploitable>(Metasploitable.class);
        util.exportExcel(response, list, "靶机");
    }

    @Log(title = "靶机导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        try {
            ExcelUtil<Metasploitable> util = new ExcelUtil<Metasploitable>(Metasploitable.class);
            List<Metasploitable> userList = util.importExcel(file.getInputStream());
            String message = metasploitableService.importData(userList, updateSupport);
            return success(message);
        } catch (ServiceException e) {
            return new AjaxResult(e.getCode(),e.getMessage());
        }
    }


    @PostMapping("/file")
    public R<FileVo> add(@RequestParam("file") MultipartFile file)
    {
        FileVo vo =  fileUploadService.fileUpload(file, "", "uav", BizCodeEnum.UAV_INFO.getCode(), "", "");
        return R.ok(vo);
    }

//    @GetMapping("/infoByUavTypeName")
//    public R<EquModel> infoByUavTypeName(EquModel equModel) {
//        List<EquModel> list = equModelService.selectEquModelList(equModel);
//        if (CollectionUtil.isEmpty(list)) {
//            return R.ok();
//        }
//        EquModel model = list.get(0);
//        Long modelId = model.getModelId();
//        return this.getInfo(modelId);
//    }
////
//    /**
//     * 导出靶机列表
//     */
//    @RequiresPermissions("system:equmodel:export")
//    @Log(title = "靶机", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, EquModel equModel)
//    {
//        List<EquModel> list = equModelService.selectEquModelList(equModel);
//        ExcelUtil<EquModel> util = new ExcelUtil<EquModel>(EquModel.class);
//        util.exportExcel(response, list, "靶机数据");
//    }

    /**
     * 获取靶机详细信息
     */
//    @RequiresPermissions("uav:equmodel:query")
    @GetMapping(value = "/{id}")
    public R<Metasploitable> getInfo(@PathVariable("id") Long id)
    {
        Metasploitable metasploitable = metasploitableService.selectMetasploitableById(id);
        return R.ok(metasploitable);
    }

    /**
     * 新增靶机
     */
//    @RequiresPermissions("uav:equmodel:add")
    @Log(title = "靶机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Metasploitable metasploitable)
    {
        metasploitable.setCreateBy(SecurityUtils.getUsername());
        return toAjax(metasploitableService.insertMetasploitable(metasploitable));
    }

    /**
     * 修改靶机
     */
//    @RequiresPermissions("uav:equmodel:edit")
    @Log(title = "靶机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Metasploitable metasploitable)
    {
        return toAjax(metasploitableService.updateMetasploitable(metasploitable));
    }

    /**
     * 删除靶机
     */
//    @RequiresPermissions("uav:equmodel:remove")
    @Log(title = "靶机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(metasploitableService.deleteMetasploitableByIds(ids));
    }
//
//
    /**
     * 获取靶机
     */
//    @RequiresPermissions("uav:equmodel:list")
    @GetMapping("/queryEquType")
    public R<List<EquType>> queryEquType()
    {
        List<EquType> list = equTypeService.queryEquType();
        return R.ok(list);
    }

    /**
     * 获取设备默认参数
     */
//    @RequiresPermissions("uav:equmodel:edit")
    @GetMapping("/getEqumodelDefine/{typeId}")
    public R<List<EquParamDefine>> getEqumodelDefine(@PathVariable("typeId") Long typeId)
    {
        List<EquParamDefine> list = equTypeService.getEqumodelDefine(typeId);
        return R.ok(list);
    }

    /**
     * 获取设备参数
     */
//    @RequiresPermissions("uav:equmodel:edit")
    @PostMapping("/getEqumodelParam")
    public R<List<EquModelParam>> getEqumodelParam(@RequestBody Map map)
    {
        Long modelId = Long.parseLong(map.get("modelId").toString());
        Long typeId = Long.parseLong(map.get("typeId").toString());
        List<EquModelParam> list = equTypeService.getEqumodelParam(modelId);
        List<EquParamDefine> equmodelDefine = equTypeService.getEqumodelDefine(typeId);
        List<EquModelParam> resultList = new ArrayList<>();
        if (list.size()==0){
            return R.ok(list);
        }else{
            for (EquParamDefine item1:equmodelDefine){
                EquModelParam equModelParam = new EquModelParam();
                for (EquModelParam item2:list){
                    if (item1.getParamCode().toString().equals(item2.getParamCode().toString())){
                        equModelParam.setParamValue(item2.getParamValue());
                        equModelParam.setParamSeq(item2.getParamSeq());
                        continue;
                    }
                }
                equModelParam.setParamCode(item1.getParamCode());
                equModelParam.setDataType(item1.getDataType());
                equModelParam.setParamName(item1.getParamName());
                equModelParam.setDecCnt(item1.getDecCnt()==null?null:Integer.valueOf(item1.getDecCnt().toString()));
                equModelParam.setMaxVal(item1.getMaxVal()==null?null:Integer.valueOf(item1.getMaxVal().toString()));
                equModelParam.setMinVal(item1.getMinVal()==null?null:Integer.valueOf(item1.getMinVal().toString()));
                equModelParam.setModelId(modelId);
                resultList.add(equModelParam);
            }
            return R.ok(resultList);
        }
    }


//    @RequiresPermissions("uav:equmodel:query")
    @GetMapping(value = "/checkModelCode/{modeCode}")
    public R<List<EquModel>> checkModelCode(@PathVariable(value = "modeCode",required = false) String modeCode)
    {
        List<EquModel> equModel = equModelService.checkModelCode(modeCode);
        return R.ok(equModel);
    }

//    @RequiresPermissions("uav:equmodel:query")
    @GetMapping(value = "/checkModelCode/")
    public R<List<EquModel>> checkModelCodes(@PathVariable(value = "modeCode",required = false) String modeCode)
    {
        List<EquModel> equModel = equModelService.checkModelCode(modeCode);
        return R.ok(equModel);
    }


//    @PostMapping("/updateEqumodelParam")
    public AjaxResult updateEqumodelParam(@RequestBody List<Map> vo){

        return toAjax(equModelService.updateEqumodelParamList(vo));
    }
}
