package com.ho.business.controller;

import com.ho.business.entity.DeviceTypeConfig;
import com.ho.business.entity.ModelDevice;
import com.ho.business.entity.ModelType;
import com.ho.business.service.ModelTypeService;
import com.ho.business.vo.req.modelDevice.ModelDeviceAddReq;
import com.ho.business.vo.req.modelDevice.ModelDeviceDeleteReq;
import com.ho.business.vo.req.modelType.ModelTypeAddReq;
import com.ho.business.vo.req.modelType.ModelTypeQueryReq;
import com.ho.business.vo.resp.modelType.ModelTypeResp;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageResult;
import com.ho.user.api.entity.SysSubDict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xwz
 * @desc: 模型配置
 * @DateTime: 2023/4/20
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "modelType")
@Api(tags = "模型配置")
public class ModelTypeController {

    @Autowired
    ModelTypeService modelTypeService;

    //用于左边列表展示
    @PostMapping("pageModelType")
    @ApiOperation(value = "查询模型配置")
    public DataResult pageModelType(@RequestBody ModelTypeQueryReq vo){
        PageResult<ModelTypeResp> modelTypeRespPageResult = modelTypeService.selectPage(vo);
        return DataResult.success(modelTypeRespPageResult);
    }

    @PostMapping("addModelType")
    @ApiOperation(value = "新增模型配置")
    @LogAnnotation(title = "模型配置", action = "新增模型配置")
    public DataResult addModelType(@RequestBody ModelTypeAddReq vo){
        ModelType modelType = new ModelType();
        modelType.setId(vo.getId());
        modelType.setModelType(vo.getModelType());
        modelType.setModelName(vo.getModelName());
        modelTypeService.insetModelType(modelType);
        return DataResult.success();
    }

    //根据model_type 查询 device_type 所对应的device_type_config 表中的name
    @PostMapping("selectDeviceType")
    @ApiOperation(value = "根据model_type查询device_type列表")
    public DataResult<List<ModelTypeAddReq>> selectDeviceType(@RequestBody ModelTypeAddReq vo){
        //返回数据包括：model_type device_type name
        List<ModelTypeAddReq> list=  modelTypeService.selectDeviceType(vo);
        return DataResult.success(list);
    }

    @PostMapping("updateModelType")
    @ApiOperation(value = "更新模型配置")
    public DataResult updateModelType(){
        return null;
    }

    //右边新增时 需要传入电站Id modelType 查询 devicetypeconfig的表中所对应的name
    @PostMapping("selectList")
    @ApiOperation(value = "查询该电站下的数据类型")
    public DataResult<List<DeviceTypeConfig>> selectList(@RequestBody ModelTypeAddReq vo){
        //返回数据包括：model_type device_type name
        List<DeviceTypeConfig> list=  modelTypeService.selectList(vo);
        return DataResult.success(list);
    }



    @PostMapping("addModelDevice")
    @ApiOperation(value = "新增模型设备类型")
    @LogAnnotation(title = "模型配置", action = "新增模型设备类型")
    public DataResult addModelDevice(@RequestBody ModelDeviceAddReq vo){
        //先判断是否有相同value的值
        ModelDevice modelValue = modelTypeService.selectByDict(vo.getDeviceType(), vo.getModelType());
        if (modelValue != null) {
            throw new BusinessException(BaseResponseCode.DICT_ALREADY_EXISTS);
        }
        ModelDevice modelDevice = new ModelDevice();
        modelDevice.setModelType(vo.getModelType());
        modelDevice.setDeviceType(vo.getDeviceType());
        modelTypeService.insetModelDevice(modelDevice);
        return DataResult.success();
    }

    @PostMapping("updateModelDevice")
    @ApiOperation(value = "更新模型设备类型")
    @LogAnnotation(title = "模型配置", action = "更新模型设备类型")
    public DataResult updateModelDevice(@RequestBody ModelDeviceAddReq vo){
        ModelDevice modelDevice = new ModelDevice();
        modelDevice.setModelType(vo.getModelType());
        modelDevice.setDeviceType(vo.getDeviceType());
        modelDevice.setId(vo.getId());
        modelTypeService.updateModelDevice(modelDevice);
        return DataResult.success();
    }

    @PostMapping("deleteModelDevice")
    @ApiOperation(value = "删除模型设备类型")
    @LogAnnotation(title = "模型配置", action = "删除模型设备类型")
    public DataResult deleteModelDevice(@RequestBody ModelDeviceDeleteReq vo){
        modelTypeService.deleteModelDevice(vo.getIds());
        return DataResult.success();
    }
}
