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

import com.kuyuntech.vrv.coreapi.bean.core.GatewayBean;
import com.kuyuntech.vrv.coreapi.bean.core.ProjectBean;
import com.kuyuntech.vrv.coreapi.service.core.GatewayService;
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;




/**
* GatewayController
*
*/
@RestController
public class GatewayController {

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

    @Autowired
    GatewayService gatewayService;

    

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

         gatewayBean = this.gatewayService.add(gatewayBean);


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

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

    }

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

       gatewayBean = this.gatewayService.update(gatewayBean);


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

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

    }


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

         gatewayBean = this.gatewayService.delete(gatewayBean);

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

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

    }


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

        PagerBean<GatewayBean> gatewayBeanPagerBean = this.gatewayService.findPager(gatewayBean,pagerBean);

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

        gatewayBeanPagerBean.getItems().forEach((e) ->{
             Map gatewayMap = MapBuilder.newBuilder()
                                            .put("number",e.getNumber())
                                            .put("ip",e.getIp())
                                            .put("location",e.getLocation())
                                            .put("useStatus",e.getUseStatus())
                                            .put("operatorNumber",e.getOperatorNumber())
                                            .put("operatorCode",e.getOperatorCode())
                                            .put("projectNumber",e.getProjectNumber())
                                            .put("projectCode",e.getProjectCode())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    gatewayMapList.add(gatewayMap);
             });


                return ResponseBean.success("操作成功！").addData("gateways",gatewayMapList).addData("pager",gatewayBeanPagerBean.simplePager());

    }



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

           gatewayBean = this.gatewayService.find(gatewayBean);

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

                Map gatewayMap = MapBuilder.newBuilder()
                        .put("number",gatewayBean.getNumber())
                        .put("ip",gatewayBean.getIp())
                        .put("location",gatewayBean.getLocation())
                        .put("useStatus",gatewayBean.getUseStatus())
                        .put("operatorNumber",gatewayBean.getOperatorNumber())
                        .put("operatorCode",gatewayBean.getOperatorCode())
                        .put("projectNumber",gatewayBean.getProjectNumber())
                        .put("projectCode",gatewayBean.getProjectCode())
                        .put("code",gatewayBean.getCode())
                        .put("createTime",gatewayBean.getCreateTime())
                        .put("updateTime",gatewayBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("gateway",gatewayMap);

    }

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

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

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


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

}
