package com.wsm.admin.api;

import com.wsm.admin.model.DataDictionary;
import com.wsm.admin.model.Device;
import com.wsm.admin.service.IDataDictionaryService;
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.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;

@Controller
@RequestMapping("/admin/device")
public class DeviceController extends BaseController {

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

    @Autowired
    private IDeviceService deviceService;

    @RequestMapping(value = {"/list"})
    public String index(Model model) {
        Page<Device> deviceList = deviceService.findAll(getPageRequest(
                new Sort(Sort.Direction.DESC, "id")));
        model.addAttribute("deviceList", deviceList);
        return "/admin/device/list";
    }

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

    @RequestMapping(value = {"/save"}, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson edit(Device device) {
        try {
            if (device.getId() != null) {
                Device dbDevice = deviceService.find(Long.valueOf(device.getId()));
                dbDevice.setDeviceId16Hex(device.getDeviceId16Hex());
                dbDevice.setDeviceId(device.getDeviceId());
                dbDevice.setDeviceType(device.getDeviceType());
                deviceService.update(device);
            }else{
                deviceService.save(device);
            }
        } catch (Exception e) {
            logger.error("系统异常：", e);
            return AjaxJson.failure("系统异常："+e);
        }
        return AjaxJson.success();
    }

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

}

