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

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




/**
* RoleController
*
*/
@RestController
public class RoleController {

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

    @Autowired
    RoleService roleService;

    

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

         roleBean = this.roleService.add(roleBean);


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

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

    }

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

       roleBean = this.roleService.update(roleBean);


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

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

    }


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

         roleBean = this.roleService.delete(roleBean);

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

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

    }


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

        PagerBean<RoleBean> roleBeanPagerBean = this.roleService.findPager(roleBean,pagerBean);

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

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


                return ResponseBean.success("操作成功！").addData("roles",roleMapList).addData("pager",roleBeanPagerBean.simplePager());

    }



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

           roleBean = this.roleService.find(roleBean);

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

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

        return ResponseBean.success("操作成功！").addData("role",roleMap);

    }

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

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

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


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

    

}
