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

import com.kuyuntech.vrv.coreapi.service.core.DeviceParamService;
import com.kuyuntech.vrv.coreapi.bean.core.DeviceParamBean;
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;




/**
* DeviceParamController
*
*/
@RestController
public class DeviceParamController {

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

    @Autowired
    DeviceParamService deviceParamService;

    

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

         deviceParamBean = this.deviceParamService.add(deviceParamBean);


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

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

    }

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

       deviceParamBean = this.deviceParamService.update(deviceParamBean);


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

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

    }


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

         deviceParamBean = this.deviceParamService.delete(deviceParamBean);

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

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

    }


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

        PagerBean<DeviceParamBean> deviceParamBeanPagerBean = this.deviceParamService.findPager(deviceParamBean,pagerBean);

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

        deviceParamBeanPagerBean.getItems().forEach((e) ->{
             Map deviceParamMap = 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("name",e.getName())
                                            .put("value",e.getValue())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    deviceParamMapList.add(deviceParamMap);
             });


                return ResponseBean.success("操作成功！").addData("deviceParams",deviceParamMapList).addData("pager",deviceParamBeanPagerBean.simplePager());

    }



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

           deviceParamBean = this.deviceParamService.find(deviceParamBean);

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

                Map deviceParamMap = MapBuilder.newBuilder()
                        .put("sn",deviceParamBean.getSn())
                        .put("deviceCode",deviceParamBean.getDeviceCode())
                        .put("operatorNumber",deviceParamBean.getOperatorNumber())
                        .put("operatorCode",deviceParamBean.getOperatorCode())
                        .put("projectNumber",deviceParamBean.getProjectNumber())
                        .put("projectCode",deviceParamBean.getProjectCode())
                        .put("gatewayNumber",deviceParamBean.getGatewayNumber())
                        .put("gatewayCode",deviceParamBean.getGatewayCode())
                        .put("name",deviceParamBean.getName())
                        .put("value",deviceParamBean.getValue())
                        .put("code",deviceParamBean.getCode())
                        .put("createTime",deviceParamBean.getCreateTime())
                        .put("updateTime",deviceParamBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("deviceParam",deviceParamMap);

    }

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

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

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


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

    

}
