package com.casic.missiles.modular.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.model.response.SuccessResponseData;
import com.casic.missiles.modular.system.model.DeviceType;
import com.casic.missiles.modular.system.service.IDeviceTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.AccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 设备类型控制器
 *
 * @author dev
 * @Date 2019-05-23 18:09:17
 */
@Slf4j
@Controller
@RequestMapping("/deviceType")
public class DeviceTypeController extends BaseController {


    @Autowired
    private IDeviceTypeService deviceTypeService;


    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return deviceTypeService.list(null);
    }

    /**
     * 获取分页列表
     */
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(String typeName, String isValid) {
        Page<Map<String, Object>> page = PageFactory.defaultPage();
        typeName = typeName == null ? "" : typeName;
        isValid = isValid == null ? "" : isValid;
        ResponseData responseData = deviceTypeService.listPage(page, typeName, isValid);
        responseData.setData(super.packForBT(page));
        return responseData;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DeviceType deviceType) {
        ResponseData responseData = new ResponseData();
        try {
            DeviceType resultObjct = deviceTypeService.selectDeviceTypeById(deviceType.getId());
            if (resultObjct != null) {
                responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
                responseData.setMessage("设备类型编号重复");
            } else {
                deviceTypeService.save(deviceType);
                responseData.setCode(ResponseData.DEFAULT_SUCCESS_CODE);
                responseData.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            }
        } catch (Exception ex) {
            log.error("主题：新增数据库异常，异常信息{}", ex);
            responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
            responseData.setMessage("数据库异常");
        }
        return responseData;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String deviceTypeId) {
        int count = deviceTypeService.countDeviceByType(deviceTypeId);
        if (count > 0) {
            return ResponseData.error("该类型存在绑定设备，请先解除绑定");
        }
        deviceTypeService.removeById(Long.valueOf(deviceTypeId));
        return ResponseData.success();
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/batchDelete")
    @ResponseBody
    public Object delete(@RequestParam("deviceTypeIds") List<String> deviceTypeIds) {
        for (String deviceTypeId : deviceTypeIds) {
            int count = deviceTypeService.countDeviceByType(deviceTypeId);
            if (count > 0) {
                String name = deviceTypeService.getById(deviceTypeId).getTypeName();
                return ResponseData.error(String.format("类型 %s 存在绑定设备，请先解除绑定", name));
            }
        }
        deviceTypeService.removeByIds(deviceTypeIds);
        return ResponseData.success();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DeviceType deviceType) {
        deviceTypeService.updateById(deviceType);
        return ResponseData.success();
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{deviceTypeId}")
    @ResponseBody
    public Object detail(@PathVariable("deviceTypeId") String deviceTypeId) {
        return ResponseData.success(deviceTypeService.getById(deviceTypeId));
    }

}
