package com.gyee.alarm.controller;



import com.gyee.alarm.model.auto.ProBasicDeviceFaultMode;
import com.gyee.alarm.model.vo.AjaxResult;
import com.gyee.alarm.service.DeviceFaultModeService;
import com.gyee.alarm.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备故障模型 前端控制器
 * </p>
 *
 * @author shilin
 * @since 2023-09-14
 */
@RestController
@RequestMapping("//device/faultmode")
public class ProBasicDeviceFaultModeController {


    @Autowired
    private DeviceFaultModeService deviceFaultModeService;


    @GetMapping(value = "/{deviceId}")
    public List<ProBasicDeviceFaultMode> getDeviceFaultModeByDeviceId(@PathVariable("deviceId")  String deviceId) {
        return deviceFaultModeService.getDeviceFaultModeByDeviceId(deviceId);
    }

    @GetMapping(value = "/{deviceId}/{structurecode}")
    public List<ProBasicDeviceFaultMode> getDeviceFaultModeBySturctureCode(@PathVariable("deviceId")  String deviceId,
                                                                   @PathVariable("structurecode")  String structurecode) {
        return deviceFaultModeService.getDeviceFaultModeByStructureCode(deviceId, structurecode);
    }


    @GetMapping(value = "/single/{code}")
    public ProBasicDeviceFaultMode getDeviceFaultModeByCode(@PathVariable("code")  String code) {
        return deviceFaultModeService.getDeviceFaultModeByCode(code);
    }


    @DeleteMapping(value = "/delete/{id}")
    public boolean deleteDeviceFaultMode(@PathVariable("id") String id) {
        boolean result = deviceFaultModeService.deleteDeviceFaultMode(id);
        return result;
    }

    @PostMapping()
    @ResponseBody
    public AjaxResult saveDeviceFaultMode(@RequestBody ProBasicDeviceFaultMode ds) {


         deviceFaultModeService.saveorUpdateDeviceFaultMode(ds);

        AjaxResult  wrapper = dataCheck(ds);

        return wrapper;
    }

    private AjaxResult  dataCheck(ProBasicDeviceFaultMode ds) {

        AjaxResult  wrapper = null;
        String msg = "";
        boolean result = true;

        if (ds.getCode() == null || ds.getCode().equals("")) {
            msg = "结构编码不能为空";
            result = false;
        } else if (ds.getDeviceId() == null || ds.getDeviceId().equals("")) {
            msg = "设备ID不能为空";
            result = false;
        } else if (ds.getName() == null || ds.getName().equals("")) {
            msg = "名称不能为空";
            result = false;
        }
        if (result) {
            wrapper = AjaxResult.success(msg);
        } else {
            wrapper = AjaxResult.error(msg);
        }
        return wrapper;
    }



    private AjaxResult  dataCheck2(ProBasicDeviceFaultMode ds) {

        AjaxResult  wrapper = null;
        String msg = "";
        boolean result = true;

        if (ds.getCode() == null || ds.getCode().equals("")) {
            msg = "结构编码不能为空";
            result = false;
        }  else if (ds.getName() == null || ds.getName().equals("")) {
            msg = "名称不能为空";
            result = false;
        }
        if (result) {
            wrapper = AjaxResult.success(msg);
        } else {
            wrapper = AjaxResult.error(msg);
        }
        return wrapper;
    }

    @PostMapping(value ="/input")
    @ResponseBody
    public AjaxResult InputExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        if (!file.isEmpty()) {
            try {
                //获取原始的文件名
                String originalFilename = file.getOriginalFilename();
                //获取文件类型
                String fileType = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
                //默认从第一行开始读取
                Integer startRows = 1;
                //获取输入流
                InputStream is = file.getInputStream();
                List<ProBasicDeviceFaultMode> bindingList = new ArrayList<>();
                //List<Bookcase> bookcaseList = new ArrayList<>();
                //Excel导入导出的单元类
                List<String[]> strings = ExcelUtil.readData(fileType, startRows, true, is);
                //遍历Excel表每一行的数据
                for (String[] str : strings) {
                    ProBasicDeviceFaultMode deviceFaultMode = new ProBasicDeviceFaultMode();
                    if(str[0]==null||str[0].length()<=0){
                        deviceFaultMode.setStructureCode("1010102");
                    }else {
                        deviceFaultMode.setStructureCode(str[0]);
                    }
                    deviceFaultMode.setName(str[1]);
                    deviceFaultMode.setCode(str[2]);
                    deviceFaultMode.setOccurence(Integer.parseInt(str[3]));
                    deviceFaultMode.setSeverity(Integer.parseInt(str[4]));
                    deviceFaultMode.setDetection(Integer.parseInt(str[5]));
                    deviceFaultMode.setCause(str[6]);
                    deviceFaultMode.setEffects(str[7]);
                    deviceFaultMode.setMeasure(str[8]);
                    bindingList.add(deviceFaultMode);
                }
                boolean bookState = deviceFaultModeService.insertOrUpdate(bindingList);
                if(bookState){
                    return AjaxResult.success("上传文件成功！");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.error("上传文件失败！");
    }
}
