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

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




/**
* PowerController
*
*/
@RestController
public class PowerController {

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

    @Autowired
    PowerService powerService;

    

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

         powerBean = this.powerService.add(powerBean);


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

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

    }

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

       powerBean = this.powerService.update(powerBean);


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

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

    }


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

         powerBean = this.powerService.delete(powerBean);

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

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

    }


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

        PagerBean<PowerBean> powerBeanPagerBean = this.powerService.findPager(powerBean,pagerBean);

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

        powerBeanPagerBean.getItems().forEach((e) ->{
             Map powerMap = MapBuilder.newBuilder()
                                            .put("name",e.getName())
                                            .put("flag",e.getFlag())
                                            .put("description",e.getDescription())
                                            .put("type",e.getType())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    powerMapList.add(powerMap);
             });


                return ResponseBean.success("操作成功！").addData("powers",powerMapList).addData("pager",powerBeanPagerBean.simplePager());

    }



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

           powerBean = this.powerService.find(powerBean);

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

                Map powerMap = MapBuilder.newBuilder()
                        .put("name",powerBean.getName())
                        .put("flag",powerBean.getFlag())
                        .put("description",powerBean.getDescription())
                        .put("type",powerBean.getType())
                        .put("code",powerBean.getCode())
                        .put("createTime",powerBean.getCreateTime())
                        .put("updateTime",powerBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("power",powerMap);

    }

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

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

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


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

    

}
