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

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




/**
* UserController
*
*/
@RestController
public class UserController {

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

    @Autowired
    UserService userService;

    

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

         userBean = this.userService.add(userBean);


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

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

    }

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

       userBean = this.userService.update(userBean);


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

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

    }


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

         userBean = this.userService.delete(userBean);

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

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

    }


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

        PagerBean<UserBean> userBeanPagerBean = this.userService.findPager(userBean,pagerBean);

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

        userBeanPagerBean.getItems().forEach((e) ->{
             Map userMap = MapBuilder.newBuilder()
                                            .put("realname",e.getRealname())
                                            .put("account",e.getAccount())
                                            .put("phone",e.getPhone())
                                            .put("password",e.getPassword())
                                            .put("roleCode",e.getRoleCode())
                                            .put("roleFlag",e.getRoleFlag())
                                            .put("operatorCode",e.getOperatorCode())
                                            .put("operatorNumber",e.getOperatorNumber())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    userMapList.add(userMap);
             });


                return ResponseBean.success("操作成功！").addData("users",userMapList).addData("pager",userBeanPagerBean.simplePager());

    }



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

           userBean = this.userService.find(userBean);

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

                Map userMap = MapBuilder.newBuilder()
                        .put("realname",userBean.getRealname())
                        .put("account",userBean.getAccount())
                        .put("phone",userBean.getPhone())
                        .put("password",userBean.getPassword())
                        .put("roleCode",userBean.getRoleCode())
                        .put("roleFlag",userBean.getRoleFlag())
                        .put("operatorCode",userBean.getOperatorCode())
                        .put("operatorNumber",userBean.getOperatorNumber())
                        .put("code",userBean.getCode())
                        .put("createTime",userBean.getCreateTime())
                        .put("updateTime",userBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("user",userMap);

    }

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

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

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


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

    

}
