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

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




/**
* OperatorController
*
*/
@RestController
public class OperatorController {

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

    @Autowired
    OperatorService operatorService;

    

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

         operatorBean = this.operatorService.add(operatorBean);


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

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

    }

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

       operatorBean = this.operatorService.update(operatorBean);


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

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

    }


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

         operatorBean = this.operatorService.delete(operatorBean);

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

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

    }


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

        PagerBean<OperatorBean> operatorBeanPagerBean = this.operatorService.findPager(operatorBean,pagerBean);

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

        operatorBeanPagerBean.getItems().forEach((e) ->{
             Map operatorMap = MapBuilder.newBuilder()
                                            .put("name",e.getName())
                                            .put("province",e.getProvince())
                                            .put("city",e.getCity())
                                            .put("address",e.getAddress())
                                            .put("contact",e.getContact())
                                            .put("tel",e.getTel())
                                            .put("number",e.getNumber())
                                            .put("status",e.getStatus())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    operatorMapList.add(operatorMap);
             });


                return ResponseBean.success("操作成功！").addData("operators",operatorMapList).addData("pager",operatorBeanPagerBean.simplePager());

    }



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

           operatorBean = this.operatorService.find(operatorBean);

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

                Map operatorMap = MapBuilder.newBuilder()
                        .put("name",operatorBean.getName())
                        .put("province",operatorBean.getProvince())
                        .put("city",operatorBean.getCity())
                        .put("address",operatorBean.getAddress())
                        .put("contact",operatorBean.getContact())
                        .put("tel",operatorBean.getTel())
                        .put("number",operatorBean.getNumber())
                        .put("status",operatorBean.getStatus())
                        .put("code",operatorBean.getCode())
                        .put("createTime",operatorBean.getCreateTime())
                        .put("updateTime",operatorBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("operator",operatorMap);

    }

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

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

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


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

    

}
