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


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




/**
* VerifyCodeController
*
*/
@RestController
public class VerifyCodeController {

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

    @Autowired
    VerifyCodeService verifyCodeService;

    

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

         verifyCodeBean = this.verifyCodeService.add(verifyCodeBean);


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

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

    }

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

       verifyCodeBean = this.verifyCodeService.update(verifyCodeBean);


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

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

    }


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

         verifyCodeBean = this.verifyCodeService.delete(verifyCodeBean);

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

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

    }


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

        PagerBean<VerifyCodeBean> verifyCodeBeanPagerBean = this.verifyCodeService.findPager(verifyCodeBean,pagerBean);

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

        verifyCodeBeanPagerBean.getItems().forEach((e) ->{
             Map verifyCodeMap = MapBuilder.newBuilder()
                                            .put("type",e.getType())
                                            .put("verifyCode",e.getVerifyCode())
                                            .put("expireTime",e.getExpireTime())
                                            .put("verified",e.getVerified())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    verifyCodeMapList.add(verifyCodeMap);
             });


                return ResponseBean.success("操作成功！").addData("verifyCodes",verifyCodeMapList).addData("pager",verifyCodeBeanPagerBean.simplePager());

    }



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

           verifyCodeBean = this.verifyCodeService.find(verifyCodeBean);

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

                Map verifyCodeMap = MapBuilder.newBuilder()
                        .put("type",verifyCodeBean.getType())
                        .put("verifyCode",verifyCodeBean.getVerifyCode())
                        .put("expireTime",verifyCodeBean.getExpireTime())
                        .put("verified",verifyCodeBean.getVerified())
                        .put("code",verifyCodeBean.getCode())
                        .put("createTime",verifyCodeBean.getCreateTime())
                        .put("updateTime",verifyCodeBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("verifyCode",verifyCodeMap);

    }

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

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

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


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

    

}
