package com.huimv.eartag.controller;

import com.alibaba.fastjson.JSONObject;
import com.huimv.eartag.service.IDeviceRegisterService;
import com.huimv.eartag.vo.Result;
import com.huimv.eartag.vo.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
//@CrossOrigin
@RestController
@RequestMapping(value = "/deviceRegister")
@Slf4j
public class DeviceRegisterController {
    @Autowired
    private IDeviceRegisterService deviceRegisterService;

    @GetMapping("/addDevice")
    public Result addDevice(@RequestParam(value = "deviceCode",required = true) String deviceCode,
                            @RequestParam(value = "chipId",required = true) String chipId,
                            @RequestParam(value = "deviceStatus",required = true) Integer deviceStatus,
                            @RequestParam(value = "activeStatus",required = true) Integer activeStatus,
                            @RequestParam(value = "deviceAlias",required = false) String deviceAlias,
                            @RequestParam(value = "location",required = false) String location,
                            @RequestParam(value = "remark",required = false) String remark,
                            @RequestParam(value = "farmId",required = true) String farmId) throws ParseException {
        log.info("farmId>>"+farmId);
        log.info("chipId>>"+chipId);
        log.info("deviceCode>>"+deviceCode);
        log.info("deviceStatus>>"+deviceStatus);
        log.info("activeStatus>>"+activeStatus);
        //deviceStatus
        if(deviceStatus == null){
            //待机状态
            deviceStatus = 0;
        }
        //activeStatus
        if(activeStatus == null){
            //未激活
            activeStatus = 0;
        }
        //{添加设备}
        return deviceRegisterService.addDevice(chipId,deviceCode,deviceStatus,activeStatus,deviceAlias,location,remark,farmId);
    }


    @PostMapping("/addDevice1")
    public Result addDevice1(@RequestBody JSONObject json){
        System.out.println("## json>>"+json);
        String msg = "";
        return new Result(ResultCode.SUCCESS,msg);
    }

    @GetMapping("/editDevice")
    public Result editDevice(@RequestParam(value = "deviceCode",required = true) String deviceCode,
                             @RequestParam(value = "chipId",required = true) String chipId,
                             @RequestParam(value = "LastDeviceStatus",required = true) Integer LastDeviceStatus,
                             @RequestParam(value = "deviceStatus",required = true) Integer deviceStatus,
                             @RequestParam(value = "activeStatus",required = true) Integer activeStatus,
                             @RequestParam(value = "deviceAlias",required = false) String deviceAlias,
                             @RequestParam(value = "location",required = false) String location,
                             @RequestParam(value = "remark",required = false) String remark,
                             @RequestParam(value = "id",required = true) Integer id) throws ParseException {

        log.info("(editDevice).deviceCode>>"+deviceCode);
        log.info("(editDevice).chipId>>"+chipId);
        log.info("(editDevice).LastDeviceStatus>>"+LastDeviceStatus);
        log.info("(editDevice).deviceStatus>>"+deviceStatus);
        log.info("(editDevice).activeStatus>>"+activeStatus);
        log.info("(editDevice).deviceAlias>>"+deviceAlias);
        log.info("(editDevice).location>>"+location);
        log.info("(editDevice).remark>>"+remark);
        log.info("(editDevice).id>>"+id);
        //{编辑}
        return deviceRegisterService.editDevice(chipId,deviceCode,LastDeviceStatus,deviceStatus,activeStatus,deviceAlias,location,remark,id);
    }

    @GetMapping("/removeDevice")
    public Result removeDevice(@RequestParam(value = "ids",required = true) String ids){
        //{移除设备}
        return deviceRegisterService.removeDevice(ids);
    }

//    @GetMapping("/listDevice")
    @RequestMapping(value = "/listAllDeviceInPager",method = RequestMethod.GET)
    public Result listDevice(@RequestParam(value = "farmId",required = true) String farmId,
                             @RequestParam(value = "deviceCode",required = true) String deviceCode,
                             @RequestParam(value = "deviceStatus",required = false) Integer deviceStatus,
                             @RequestParam(value = "activeStatus",required = false) Integer activeStatus,
                             @RequestParam(value = "deviceAlias",required = false) String deviceAlias,
                             @RequestParam(value = "location",required = false) String location,
                             @RequestParam(value = "pageNo",required = true) Integer pageNo,
                             @RequestParam(value = "pageSize",required = true) Integer pageSize){
        //
        if(pageNo == null || pageNo.toString().length()==0 || pageSize == null || pageSize.toString().length() == 0){
            return new Result(10010,"页码或页码尺寸不能为空.",false);
        }
        //{展示设备列表}
        return deviceRegisterService.listDevice(farmId,deviceCode,deviceStatus,activeStatus,deviceAlias,location,pageNo,pageSize);
    }

    @GetMapping("/setDeviceStatus")
    public Result setDeviceStatus(@RequestParam(value = "id",required = true) Integer id,
                               @RequestParam(value = "deviceStatus",required = true) Integer deviceStatus) throws ParseException {
        log.info("(setDeviceStatus).id>>"+id);
        log.info("(setDeviceStatus).deviceStatus>>"+deviceStatus);
        //{注销设备}
        return deviceRegisterService.setDeviceStatus(id,deviceStatus);
    }

}
