package com.wsm.admin.api;

import com.wsm.admin.constant.MsgCode;
import com.wsm.admin.model.Device;
import com.wsm.admin.model.DeviceEvent;
import com.wsm.admin.service.IDeviceEventService;
import com.wsm.admin.service.IDeviceService;
import com.wsm.common.api.BaseController;
import com.wsm.common.util.AjaxJson;
import com.wsm.common.util.ConstantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
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.ResponseBody;

import java.util.Date;

/**
 * 设备警告管理
 */

@Controller
@RequestMapping("/admin/deviceEvent")
public class DeviceEventController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IDeviceEventService deviceEventService;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(value = {"/list"})
    public String index(Model model) {
        Page<DeviceEvent> deviceEventList = deviceEventService.findAll(getPageRequest(
                new Sort(Sort.Direction.DESC, "id")));
/*        if (deviceEventList != null){
            for (DeviceEvent deviceEvent : deviceEventList){
                deviceEvent.setDevice(null);
            }
        }*/
        model.addAttribute("deviceEventList", deviceEventList);
        return "/admin/deviceEvent/list";
    }

    @RequestMapping(value = {"/detail"}, method = RequestMethod.GET)
    public String detail(@RequestParam(required = false) String deviceEventId, Model model) {
        DeviceEvent deviceEvent = new DeviceEvent();
        if (deviceEventId != null) {
            deviceEvent = deviceEventService.find(Long.valueOf(deviceEventId));
        }
        model.addAttribute("deviceEvent", deviceEvent);
        model.addAttribute("device", deviceEvent.getDevice());
        return "/admin/deviceEvent/form";
    }

    /**
     * 删除
     */
    @RequestMapping(value = {"/remove"}, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson remove(String deviceEventId) {
        AjaxJson ajaxJson = null;
        try {
            if (!StringUtils.isEmpty(deviceEventId)) {
                DeviceEvent dbDeviceEvent = deviceEventService.find(Long.valueOf(deviceEventId));
                dbDeviceEvent.setRecStatus("I");
                deviceEventService.update(dbDeviceEvent);
                ajaxJson =  AjaxJson.success(ConstantUtils.SUCCESS_MSG);
            }else{
                ajaxJson = AjaxJson.failure("id不能为空");
            }
        } catch (Exception e) {
            logger.error("系统异常：", e);
            return AjaxJson.failure("系统异常：" + e);
        }
        return ajaxJson;
    }


    /**
     * 修改
     * @param
     * @return
     */
    @RequestMapping(value = {"/save"}, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson edit(DeviceEvent deviceEvent) {
        logger.info("dto: {}", deviceEvent.toString());
        DeviceEvent event = deviceEventService.find(deviceEvent.getId());
        if (event != null){
          event.setUpdateTime(new Date());
          event.setHandleStatus(deviceEvent.getHandleStatus());
          if (deviceEvent.getHandleStatus() == 3) {
              // 更新事件状态
              event.setStatus(0);

              // 更新设备状态
              Device device = event.getDevice();
              device.setStatus((byte)0);
              device.setUpdateTime(new Date());
              deviceService.update(device);

              // 删除缓存
              redisTemplate.delete(MsgCode.REDIS_CYCLE_DEVICE_LIST);
          }
          deviceEventService.update(event);

        }



        return AjaxJson.success();
    }

}
