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

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




/**
* RolePowerController
*
*/
@RestController
public class RolePowerController {

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

    @Autowired
    RolePowerService rolePowerService;

    

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

         rolePowerBean = this.rolePowerService.add(rolePowerBean);


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

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

    }

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

       rolePowerBean = this.rolePowerService.update(rolePowerBean);


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

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

    }


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

         rolePowerBean = this.rolePowerService.delete(rolePowerBean);

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

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

    }


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

        PagerBean<RolePowerBean> rolePowerBeanPagerBean = this.rolePowerService.findPager(rolePowerBean,pagerBean);

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

        rolePowerBeanPagerBean.getItems().forEach((e) ->{
             Map rolePowerMap = MapBuilder.newBuilder()
                                            .put("roleFlag",e.getRoleFlag())
                                            .put("powerFlag",e.getPowerFlag())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    rolePowerMapList.add(rolePowerMap);
             });


                return ResponseBean.success("操作成功！").addData("rolePowers",rolePowerMapList).addData("pager",rolePowerBeanPagerBean.simplePager());

    }



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

           rolePowerBean = this.rolePowerService.find(rolePowerBean);

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

                Map rolePowerMap = MapBuilder.newBuilder()
                        .put("roleFlag",rolePowerBean.getRoleFlag())
                        .put("powerFlag",rolePowerBean.getPowerFlag())
                        .put("code",rolePowerBean.getCode())
                        .put("createTime",rolePowerBean.getCreateTime())
                        .put("updateTime",rolePowerBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("rolePower",rolePowerMap);

    }

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

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

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


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

    

}
