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

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




/**
* DeviceController
*
*/
@RestController
public class DeviceController {

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

    @Autowired
    DeviceService deviceService;

    

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

         deviceBean = this.deviceService.add(deviceBean);


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

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

    }

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

       deviceBean = this.deviceService.update(deviceBean);


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

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

    }


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

         deviceBean = this.deviceService.delete(deviceBean);

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

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

    }


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

        PagerBean<DeviceBean> deviceBeanPagerBean = this.deviceService.findPager(deviceBean,pagerBean);

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

        deviceBeanPagerBean.getItems().forEach((e) ->{
             Map deviceMap = MapBuilder.newBuilder()
                                            .put("name",e.getName())
                                            .put("ip",e.getIp())
                                            .put("sn",e.getSn())
                                            .put("type",e.getType())
                                            .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("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    deviceMapList.add(deviceMap);
             });


                return ResponseBean.success("操作成功！").addData("devices",deviceMapList).addData("pager",deviceBeanPagerBean.simplePager());

    }



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

           deviceBean = this.deviceService.find(deviceBean);

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

                Map deviceMap = MapBuilder.newBuilder()
                        .put("name",deviceBean.getName())
                        .put("ip",deviceBean.getIp())
                        .put("sn",deviceBean.getSn())
                        .put("type",deviceBean.getType())
                        .put("operatorNumber",deviceBean.getOperatorNumber())
                        .put("operatorCode",deviceBean.getOperatorCode())
                        .put("projectNumber",deviceBean.getProjectNumber())
                        .put("projectCode",deviceBean.getProjectCode())
                        .put("gatewayNumber",deviceBean.getGatewayNumber())
                        .put("gatewayCode",deviceBean.getGatewayCode())
                        .put("code",deviceBean.getCode())
                        .put("createTime",deviceBean.getCreateTime())
                        .put("updateTime",deviceBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("device",deviceMap);

    }

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

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

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


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

    

}
