package com.kuyuntech.vrv.corerestful.controller.core;

import com.kuyuntech.vrv.coreapi.service.core.DeviceAlarmConfigService;
import com.kuyuntech.vrv.coreapi.bean.core.DeviceAlarmConfigBean;
import com.wbspool.fastboot.core.common.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wbspool.fastboot.core.web.result.ParamErrorResultBuilder;
import com.wbspool.fastboot.core.common.bean.PagerBean;
import java.util.Map;
import com.wbspool.fastboot.core.common.builder.MapBuilder;
import com.wbspool.fastboot.core.web.annotation.ParamErrorAutoResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.Errors;
import com.wbspool.fastboot.core.common.constant.ValidGroup;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




/**
* DeviceAlarmConfigController
*
*/
@RestController
public class DeviceAlarmConfigController {

    private static final Logger logger = LoggerFactory.getLogger(DeviceAlarmConfigController.class);

    @Autowired
    DeviceAlarmConfigService deviceAlarmConfigService;

    

    /**
    * 新增
    * TODO 待实现
    * @param deviceAlarmConfigBean 新增参数
    * @return
    *
    */
    @RequestMapping
    @ParamErrorAutoResponse
    public Object add(@Validated(ValidGroup.Add.class) DeviceAlarmConfigBean deviceAlarmConfigBean){

         deviceAlarmConfigBean = this.deviceAlarmConfigService.add(deviceAlarmConfigBean);


         if (deviceAlarmConfigBean == null) {
               return ResponseBean.serverError("操作失败！");
         }

          return ResponseBean.success("操作成功！").addData("code", deviceAlarmConfigBean.getCode());

    }

    /**
    * 更新
    * TODO 待实现
    * @param deviceAlarmConfigBean 更新参数
    * @return
    */
    @RequestMapping
    @ParamErrorAutoResponse
    public Object update(@Validated(ValidGroup.Update.class) DeviceAlarmConfigBean deviceAlarmConfigBean){

       deviceAlarmConfigBean = this.deviceAlarmConfigService.update(deviceAlarmConfigBean);


       if (deviceAlarmConfigBean == null) {
           return ResponseBean.serverError("操作失败！");
       }

       return ResponseBean.success("操作成功！").addData("code", deviceAlarmConfigBean.getCode());

    }


    /**
    * 删除
    * TODO 待实现
    * @param deviceAlarmConfigBean 删除参数
    * @return
    */
    @RequestMapping
    @ParamErrorAutoResponse
    public Object delete(@Validated(ValidGroup.Delete.class) DeviceAlarmConfigBean deviceAlarmConfigBean){

         deviceAlarmConfigBean = this.deviceAlarmConfigService.delete(deviceAlarmConfigBean);

          if (deviceAlarmConfigBean == null) {
             return ResponseBean.serverError("操作失败！");
          }

          return ResponseBean.success("操作成功！");

    }


    /**
    * 查询列表
    * TODO 待实现
    * @param deviceAlarmConfigBean 查询参数
    * @return
    */
    @RequestMapping
    public Object list(DeviceAlarmConfigBean deviceAlarmConfigBean, PagerBean pagerBean){

        PagerBean<DeviceAlarmConfigBean> deviceAlarmConfigBeanPagerBean = this.deviceAlarmConfigService.findPager(deviceAlarmConfigBean,pagerBean);

        List<Map> deviceAlarmConfigMapList = new ArrayList<>();

        deviceAlarmConfigBeanPagerBean.getItems().forEach((e) ->{
             Map deviceAlarmConfigMap = MapBuilder.newBuilder()
                                            .put("sn",e.getSn())
                                            .put("deviceCode",e.getDeviceCode())
                                            .put("operatorNumber",e.getOperatorNumber())
                                            .put("operatorCode",e.getOperatorCode())
                                            .put("projectNumber",e.getProjectNumber())
                                            .put("projectCode",e.getProjectCode())
                                            .put("gatewayNumber",e.getGatewayNumber())
                                            .put("gatewayCode",e.getGatewayCode())
                                            .put("alarmName",e.getAlarmName())
                                            .put("variableId",e.getVariableId())
                                            .put("alarmType",e.getAlarmType())
                                            .put("condition",e.getConfigCondition())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    deviceAlarmConfigMapList.add(deviceAlarmConfigMap);
             });


                return ResponseBean.success("操作成功！").addData("deviceAlarmConfigs",deviceAlarmConfigMapList).addData("pager",deviceAlarmConfigBeanPagerBean.simplePager());

    }



    /**
    * 查询详情
    * TODO 待实现
    * @param deviceAlarmConfigBean 查询参数
    * @return
    */
    @RequestMapping
    @ParamErrorAutoResponse
    public Object detail(@Validated(ValidGroup.Detail.class) DeviceAlarmConfigBean deviceAlarmConfigBean,Errors errors){

           deviceAlarmConfigBean = this.deviceAlarmConfigService.find(deviceAlarmConfigBean);

                if(deviceAlarmConfigBean == null){
                    return  ResponseBean.serverError("该记录不存在!");
                }

                Map deviceAlarmConfigMap = MapBuilder.newBuilder()
                        .put("sn",deviceAlarmConfigBean.getSn())
                        .put("deviceCode",deviceAlarmConfigBean.getDeviceCode())
                        .put("operatorNumber",deviceAlarmConfigBean.getOperatorNumber())
                        .put("operatorCode",deviceAlarmConfigBean.getOperatorCode())
                        .put("projectNumber",deviceAlarmConfigBean.getProjectNumber())
                        .put("projectCode",deviceAlarmConfigBean.getProjectCode())
                        .put("gatewayNumber",deviceAlarmConfigBean.getGatewayNumber())
                        .put("gatewayCode",deviceAlarmConfigBean.getGatewayCode())
                        .put("alarmName",deviceAlarmConfigBean.getAlarmName())
                        .put("variableId",deviceAlarmConfigBean.getVariableId())
                        .put("alarmType",deviceAlarmConfigBean.getAlarmType())
                        .put("condition",deviceAlarmConfigBean.getConfigCondition())
                        .put("code",deviceAlarmConfigBean.getCode())
                        .put("createTime",deviceAlarmConfigBean.getCreateTime())
                        .put("updateTime",deviceAlarmConfigBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("deviceAlarmConfig",deviceAlarmConfigMap);

    }

    /**
    * 批量删除
    * @param codes 删除唯一标识集合
    * @return
    */
    @RequestMapping
    public Object batchDelete(List<String> codes){

        if(codes.isEmpty()){
            return ParamErrorResultBuilder.newBuilder().message("未选择任何删除记录！").paramError("codes","不能为空！").build();
        }

        try{
            this.deviceAlarmConfigService.batchDeleteByCodes(codes);
        }catch (Exception e){
            return  ResponseBean.serverError("操作失败！");
        }


        return ResponseBean.success("操作成功！");
    }

    

}
