package com.huimv.admin.device.controller;

import com.huimv.admin.device.service.IDevice;
import com.huimv.admin.device.utils.DateUtil;
import com.huimv.common.utils.Result;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@RestController
@RequestMapping(value = "/farm/device")
@Slf4j
public class DeviceController {
    @Autowired
    private IDevice iDevice;
    @Autowired
    private DateUtil dateUtil;

    //新建设备信息
    @RequestMapping(value = "/newDevice",method = RequestMethod.GET)
    public Result saveDevice(@RequestParam(value = "deviceName",required = true) String deviceName, @RequestParam(value = "deviceCode",required = true) String deviceCode, @RequestParam(value = "deviceType",required = true) String deviceType,
                             @RequestParam(value = "factory",required = true) String factory, @RequestParam(value = "worker",required = true) String worker, @RequestParam(value = "mainParams",required = true) String mainParams,
                             @RequestParam(value = "state",required = true) Integer state, @RequestParam(value = "record",required = true) String record, @RequestParam(value = "farmId",required = true) Integer farmId) throws UnsupportedEncodingException {
        //
        return iDevice.newDevice(deviceName,deviceCode,deviceType,factory,worker,mainParams,state,record,farmId);
    }

    //编辑设备信息
    @RequestMapping(value = "/editDevice",method = RequestMethod.GET)
    public Result editDevice(@RequestParam(value = "deviceName",required = true) String deviceName, @RequestParam(value = "deviceCode",required = true) String deviceCode, @RequestParam(value = "deviceType",required = true) String deviceType,
                             @RequestParam(value = "factory",required = true) String factory, @RequestParam(value = "worker",required = true) String worker, @RequestParam(value = "mainParams",required = true) String mainParams,
                             @RequestParam(value = "state",required = true) Integer state, @RequestParam(value = "record",required = true) String record, @RequestParam(value = "farmId",required = true) Integer farmId,
                             @RequestParam(value = "id",required = true) Integer id) throws UnsupportedEncodingException {
            //
        return iDevice.editDevice(deviceName,deviceCode,deviceType,factory,worker,mainParams,state,record,farmId,id);
    }

    //删除设备信息
    @RequestMapping(value = "/removeDevice",method = RequestMethod.GET)
    public Result removeDevice(@RequestParam(value = "id",required = true) Integer id, @RequestParam(value = "farmId",required = true) Integer farmId) throws UnsupportedEncodingException {
        //
        return iDevice.removeDevice(id,farmId);
    }

    //列表展示设备
    @RequestMapping(value = "/listDevice",method = RequestMethod.GET)
    public Result listDevice(@RequestParam(value = "deviceName",required = false) String deviceName, @RequestParam(value = "deviceCode",required = false) String deviceCode, @RequestParam(value = "deviceType",required = false) String deviceType,
                             @RequestParam(value = "factory",required = false) String factory, @RequestParam(value = "worker",required = false) String worker,
                             @RequestParam(value = "state",required = false) Integer state, @RequestParam(value = "farmId",required = true) Integer farmId,
                             @RequestParam(value = "startDate",required = false) String startDate, @RequestParam(value = "endDate",required = false) String endDate,
                             @RequestParam(value = "pageNo",required = true) Integer pageNo, @RequestParam(value = "pageSize",required = true) Integer pageSize) throws ParseException {
        //
        return iDevice.listDevice(deviceName,deviceCode,deviceType,factory,worker,state,farmId,startDate,endDate,pageNo,pageSize);
    }

    //计算设备总数，设备故障数，故障率
    @RequestMapping(value = "/getCountDeviceFailureRate",method = RequestMethod.GET)
    public Result getCountDeviceFailureRate(@RequestParam(value = "farmId",required = true) Integer farmId, @RequestParam(value = "bit",required = true) Integer bit){
        //
        return iDevice.getCountDeviceFailureRate(farmId,bit);
    }

}
