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

import com.casic.missiles.model.response.SuccessResponseData;
import com.casic.missiles.modular.system.model.DeviceModels;
import com.casic.missiles.modular.system.model.DeviceType;
import com.casic.missiles.modular.system.service.IDeviceModelsService;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.service.IDeviceTypeService;
import lombok.extern.slf4j.Slf4j;
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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

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

import com.casic.missiles.core.page.PageFactory;
import org.springframework.web.bind.annotation.RequestParam;

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

    private final IDeviceModelsService deviceModelsService;

    private final IDeviceTypeService deviceTypeService;

    public DeviceModelsController(IDeviceTypeService deviceTypeService, IDeviceModelsService deviceModelsService) {
        this.deviceTypeService = deviceTypeService;
        this.deviceModelsService = deviceModelsService;
    }

    /**
     * 获取分页列表
     */
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(String deviceType, String communication) {
        Page<DeviceModels> page = PageFactory.defaultPage();
        QueryWrapper<DeviceModels> query = new QueryWrapper<DeviceModels>()
                .like("MODEL_NAME", deviceType == null ? "" : deviceType)
                .like("COMMUNICATION", communication == null ? "" : communication);
        page = deviceModelsService.page(page, query);
        Map<String, String> dictMap = deviceModelsService.getDictNameByCode();
        page.getRecords().forEach(
                deviceModels -> {
                    DeviceType deviceTypeModel = deviceTypeService.getById(deviceModels.getDeviceType());
                    deviceModels.setDeviceTypeName(deviceTypeModel == null ? "" : deviceTypeModel.getTypeName());
                    String[] d = deviceModels.getCommunication().split(",");
                    if (d.length > 0) {
                        deviceModels.setCommunNameGroup(dictMap.get(d[0]));
                    }
                    for (int i = 1; i < d.length; i++) {
                        deviceModels.setCommunNameGroup(deviceModels.getCommunNameGroup() + "," + dictMap.get(d[i]));
                    }
                }
        );
        return new SuccessResponseData(super.packForBT(page));
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DeviceModels deviceModels) {
        ResponseData responseData = new ResponseData();
        try {
            deviceModelsService.save(deviceModels);
            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 deviceModelsId) {
        int count = deviceModelsService.countDeviceByModels(deviceModelsId);
        if (count > 0) {
            return ResponseData.error("该型号存在绑定的设备，请先解除绑定");
        }
        deviceModelsService.removeById(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.getById(deviceModelsId).getModelName();
                return ResponseData.error(String.format("型号 %s 存在绑定的设备，请先解除绑定", name));
            }
        }
        deviceModelsService.removeByIds(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.getById(deviceModelsId));
    }
}
