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

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

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




/**
* DeviceAlarmRecordController
*
*/
@RestController
public class DeviceAlarmRecordController {

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

    @Autowired
    DeviceAlarmRecordService deviceAlarmRecordService;

    

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

         deviceAlarmRecordBean = this.deviceAlarmRecordService.add(deviceAlarmRecordBean);


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

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

    }

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

       deviceAlarmRecordBean = this.deviceAlarmRecordService.update(deviceAlarmRecordBean);


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

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

    }


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

         deviceAlarmRecordBean = this.deviceAlarmRecordService.delete(deviceAlarmRecordBean);

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

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

    }


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

        PagerBean<DeviceAlarmRecordBean> deviceAlarmRecordBeanPagerBean = this.deviceAlarmRecordService.findPager(deviceAlarmRecordBean,pagerBean);

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

        deviceAlarmRecordBeanPagerBean.getItems().forEach((e) ->{
             Map deviceAlarmRecordMap = MapBuilder.newBuilder()
                                            .put("deviceCode",e.getDeviceCode())
                                            .put("sn",e.getSn())
                                            .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("alarmType",e.getAlarmType())
                                            .put("variableId",e.getVariableId())
                                            .put("condition",e.getRecordCondition())
                                            .put("alarmConfigCode",e.getAlarmConfigCode())
                                            .put("startTime",e.getStartTime())
                                            .put("endTime",e.getEndTime())
                                            .put("status",e.getStatus())
                                            .put("content",e.getContent())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    deviceAlarmRecordMapList.add(deviceAlarmRecordMap);
             });


                return ResponseBean.success("操作成功！").addData("deviceAlarmRecords",deviceAlarmRecordMapList).addData("pager",deviceAlarmRecordBeanPagerBean.simplePager());

    }



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

           deviceAlarmRecordBean = this.deviceAlarmRecordService.find(deviceAlarmRecordBean);

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

                Map deviceAlarmRecordMap = MapBuilder.newBuilder()
                        .put("deviceCode",deviceAlarmRecordBean.getDeviceCode())
                        .put("sn",deviceAlarmRecordBean.getSn())
                        .put("operatorNumber",deviceAlarmRecordBean.getOperatorNumber())
                        .put("operatorCode",deviceAlarmRecordBean.getOperatorCode())
                        .put("projectNumber",deviceAlarmRecordBean.getProjectNumber())
                        .put("projectCode",deviceAlarmRecordBean.getProjectCode())
                        .put("gatewayNumber",deviceAlarmRecordBean.getGatewayNumber())
                        .put("gatewayCode",deviceAlarmRecordBean.getGatewayCode())
                        .put("alarmName",deviceAlarmRecordBean.getAlarmName())
                        .put("alarmType",deviceAlarmRecordBean.getAlarmType())
                        .put("variableId",deviceAlarmRecordBean.getVariableId())
                        .put("condition",deviceAlarmRecordBean.getRecordCondition())
                        .put("alarmConfigCode",deviceAlarmRecordBean.getAlarmConfigCode())
                        .put("startTime",deviceAlarmRecordBean.getStartTime())
                        .put("endTime",deviceAlarmRecordBean.getEndTime())
                        .put("status",deviceAlarmRecordBean.getStatus())
                        .put("content",deviceAlarmRecordBean.getContent())
                        .put("code",deviceAlarmRecordBean.getCode())
                        .put("createTime",deviceAlarmRecordBean.getCreateTime())
                        .put("updateTime",deviceAlarmRecordBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("deviceAlarmRecord",deviceAlarmRecordMap);

    }

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

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

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


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

    

}
