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

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




/**
* OperatorLogController
*
*/
@RestController
public class OperatorLogController {

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

    @Autowired
    OperatorLogService operatorLogService;

    

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

         operatorLogBean = this.operatorLogService.add(operatorLogBean);


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

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

    }

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

       operatorLogBean = this.operatorLogService.update(operatorLogBean);


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

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

    }


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

         operatorLogBean = this.operatorLogService.delete(operatorLogBean);

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

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

    }


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

        PagerBean<OperatorLogBean> operatorLogBeanPagerBean = this.operatorLogService.findPager(operatorLogBean,pagerBean);

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

        operatorLogBeanPagerBean.getItems().forEach((e) ->{
             Map operatorLogMap = MapBuilder.newBuilder()
                                            .put("userCode",e.getUserCode())
                                            .put("userRealname",e.getUserRealname())
                                            .put("userAccount",e.getUserAccount())
                                            .put("content",e.getContent())
                                            .put("operatorNumber",e.getOperatorNumber())
                                            .put("operatorCode",e.getOperatorCode())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    operatorLogMapList.add(operatorLogMap);
             });


                return ResponseBean.success("操作成功！").addData("operatorLogs",operatorLogMapList).addData("pager",operatorLogBeanPagerBean.simplePager());

    }



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

           operatorLogBean = this.operatorLogService.find(operatorLogBean);

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

                Map operatorLogMap = MapBuilder.newBuilder()
                        .put("userCode",operatorLogBean.getUserCode())
                        .put("userRealname",operatorLogBean.getUserRealname())
                        .put("userAccount",operatorLogBean.getUserAccount())
                        .put("content",operatorLogBean.getContent())
                        .put("operatorNumber",operatorLogBean.getOperatorNumber())
                        .put("operatorCode",operatorLogBean.getOperatorCode())
                        .put("code",operatorLogBean.getCode())
                        .put("createTime",operatorLogBean.getCreateTime())
                        .put("updateTime",operatorLogBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("operatorLog",operatorLogMap);

    }

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

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

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


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

    

}
