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

import com.casic.missiles.modular.system.model.DeviceModels;
import com.casic.missiles.modular.system.service.IDeviceModelsService;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.base.response.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import java.util.List;
import com.casic.missiles.core.common.constant.factory.PageFactory;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 设备型号控制器
 *
 * @author dev
 * @Date 2019-05-23 18:09:24
 */
@Controller
@RequestMapping("/deviceModels")
public class DeviceModelsController extends BaseController {


    @Autowired
    private IDeviceModelsService deviceModelsService;


    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Long deviceType) {
        return deviceModelsService.selectList(deviceType);
    }

    /**
     * 获取分页列表
     */
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(String condition) {
        Page<DeviceModels> page = new PageFactory<DeviceModels>().defaultPage();
        EntityWrapper<DeviceModels> query = new EntityWrapper<>();
        page =  deviceModelsService.selectPage(page,query);
        return super.packForBT(page);
    }
    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DeviceModels deviceModels) {
        deviceModelsService.insert(deviceModels);
         return ResponseData.success();
    }

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

    /**
     * 批量删除
     */
    @RequestMapping(value = "/batchDelete")
    @ResponseBody
    public Object delete(@RequestParam("deviceModelsIds") List<String> deviceModelsIds) {
        for (String deviceModelsId : deviceModelsIds) {
            int count = deviceModelsService.countDeviceByModels(deviceModelsId);
            if(count > 0) {
                String name = deviceModelsService.selectById(deviceModelsId).getModelName();
                return ResponseData.error(String.format("型号 %s 存在绑定的设备，请先解除绑定", name));
            }
        }
        deviceModelsService.deleteBatchIds(deviceModelsIds);
        return ResponseData.success();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DeviceModels deviceModels) {
        deviceModelsService.updateById(deviceModels);
         return ResponseData.success();
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{deviceModelsId}")
    @ResponseBody
    public Object detail(@PathVariable("deviceModelsId") String deviceModelsId) {

        return ResponseData.success(deviceModelsService.selectById(deviceModelsId));
    }
}
