package com.wsm.admin.api;

import com.wsm.admin.constant.MsgCode;
import com.wsm.admin.model.Device;
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 com.wsm.common.util.RedisUtil;
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;

/**
 * 设备管理
 */
@Controller
@RequestMapping("/admin/device")
public class DeviceController extends BaseController {

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

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private RedisTemplate redisTemplate;

    @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) {
//        logger.info("dto: {}", device.toString());
        try {
//            String device16hex = null;
//            if ("YG".equals(device.getDeviceType())){
//                String deviceId = device.getDeviceId();
//                device16hex = "5947" + deviceId.substring(5, deviceId.length());
//                device.setDeviceId16Hex(device16hex);
//            }
            if (device.getId() != null) {
                Device dbDevice = deviceService.find(Long.valueOf(device.getId()));
                dbDevice.setDeviceId16Hex(device.getDeviceId16Hex());
                dbDevice.setDeviceId(device.getDeviceId());
                dbDevice.setDeviceType(device.getDeviceType());
                dbDevice.setLongitude(device.getLongitude());
                dbDevice.setLatitude(device.getLatitude());
                dbDevice.setAddress(device.getAddress());
                deviceService.update(dbDevice);
            }else{
                device.setStatus((byte)0);
                deviceService.save(device);
            }

            // 删除缓存
            redisTemplate.delete(MsgCode.REDIS_CYCLE_DEVICE_LIST);

        } 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不能为空");
            }

            // 删除缓存
            redisTemplate.delete(MsgCode.REDIS_CYCLE_DEVICE_LIST);
        } catch (Exception e) {
            logger.error("系统异常：", e);
            return AjaxJson.failure("系统异常：" + e);
        }
        return ajaxJson;
    }

}

