package com.djhk.uav.equ.controller;

import cn.hutool.core.collection.CollectionUtil;
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.EquModel;
import com.djhk.uav.equ.domain.EquModelParam;
import com.djhk.uav.equ.domain.EquParamDefine;
import com.djhk.uav.equ.domain.EquType;
import com.djhk.uav.equ.domain.vo.EquModelExportVo;
import com.djhk.uav.equ.service.FileUploadService;
import com.djhk.uav.equ.service.IEquModelService;
import com.djhk.uav.equ.service.IEquTypeService;
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("/equmodel")
public class EquModelController extends BaseController
{
    @Autowired
    private IEquModelService equModelService;
//
    @Autowired
    private IEquTypeService equTypeService;

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 查询设备型号列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EquModel equModel)
    {
        startPage();
        List<EquModel> list = equModelService.selectEquModelList(equModel);
        return getDataTable(list);
    }

    @Log(title = "设备型号导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquModelExportVo equInfoExportVo) {
        List<EquModel> list = equModelService.selectEquInfoExportList(equInfoExportVo);
        ExcelUtil<EquModel> util = new ExcelUtil<EquModel>(EquModel.class);
        util.exportExcel(response, list, "设备型号");
    }

    @Log(title = "设备型号导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        try {
            ExcelUtil<EquModel> util = new ExcelUtil<>(EquModel.class);
            List<EquModel> userList = util.importExcel(file.getInputStream());
            String message = equModelService.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, "设备型号数据");
//    }

    /**
     * 获取设备型号详细信息
     */
    @GetMapping(value = "/{modelId}")
    public R<EquModel> getInfo(@PathVariable("modelId") Long modelId)
    {
        EquModel equModel = equModelService.selectEquModelByModelId(modelId);
        return R.ok(equModel);
    }

    /**
     * 新增设备型号
     */
    @Log(title = "设备型号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquModel equModel)
    {
        equModel.setCreateBy(SecurityUtils.getUsername());
        return toAjax(equModelService.insertEquModel(equModel));
    }

    /**
     * 修改设备型号
     */
    @Log(title = "设备型号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquModel equModel)
    {
        return toAjax(equModelService.updateEquModel(equModel));
    }

    /**
     * 删除设备型号
     */
    @Log(title = "设备型号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds)
    {
        return toAjax(equModelService.deleteEquModelByModelIds(modelIds));
    }
//
//
    /**
     * 获取设备型号
     */
    @GetMapping("/queryEquType")
    public R<List<EquType>> queryEquType()
    {
        List<EquType> list = equTypeService.queryEquType();
        return R.ok(list);
    }

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

    /**
     * 获取设备参数
     */
    @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);
        }
    }



    @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);
    }


    @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));
    }
}
