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

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




/**
* ProjectController
*
*/
@RestController
public class ProjectController {

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

    @Autowired
    ProjectService projectService;

    

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

         projectBean = this.projectService.add(projectBean);


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

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

    }

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

       projectBean = this.projectService.update(projectBean);


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

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

    }


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

         projectBean = this.projectService.delete(projectBean);

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

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

    }


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

        PagerBean<ProjectBean> projectBeanPagerBean = this.projectService.findPager(projectBean,pagerBean);

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

        projectBeanPagerBean.getItems().forEach((e) ->{
             Map projectMap = MapBuilder.newBuilder()
                                            .put("name",e.getName())
                                            .put("description",e.getDescription())
                                            .put("province",e.getProvince())
                                            .put("city",e.getCity())
                                            .put("address",e.getAddress())
                                            .put("customer",e.getCustomer())
                                            .put("customerTel",e.getCustomerTel())
                                            .put("operatorNumber",e.getOperatorNumber())
                                            .put("operatorCode",e.getOperatorCode())
                                            .put("number",e.getNumber())
                                            .put("code",e.getCode())
                                            .put("createTime",e.getCreateTime())
                                            .put("updateTime",e.getUpdateTime())
                                            .build();
                    projectMapList.add(projectMap);
             });


                return ResponseBean.success("操作成功！").addData("projects",projectMapList).addData("pager",projectBeanPagerBean.simplePager());

    }



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

           projectBean = this.projectService.find(projectBean);

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

                Map projectMap = MapBuilder.newBuilder()
                        .put("name",projectBean.getName())
                        .put("description",projectBean.getDescription())
                        .put("province",projectBean.getProvince())
                        .put("city",projectBean.getCity())
                        .put("address",projectBean.getAddress())
                        .put("customer",projectBean.getCustomer())
                        .put("customerTel",projectBean.getCustomerTel())
                        .put("operatorNumber",projectBean.getOperatorNumber())
                        .put("operatorCode",projectBean.getOperatorCode())
                        .put("number",projectBean.getNumber())
                        .put("code",projectBean.getCode())
                        .put("createTime",projectBean.getCreateTime())
                        .put("updateTime",projectBean.getUpdateTime())
                        .build();

        return ResponseBean.success("操作成功！").addData("project",projectMap);

    }

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

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

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


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

    

}
