package com.zjrs.zwnw.psam.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.zwnw.psam.bpo.PsamAzjgwhBPO;
import com.zjrs.zwnw.psam.dto.PsamAzjgDTO;
import com.zjrs.zwnw.psam.dto.PsamAzjgwhDTO;
import com.zjrs.zwnw.psam.dto.PsamQueryFjDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

/**
 * @author liangxiudong
 * @since 2021-04-07
 */
@Api(tags = "安装机构维护")
@RestController
@RequestMapping("/api/psam/azjgwh")
public class PsamAzjgwhController {
  private static final Logger LOG = LoggerFactory.getLogger(PsamAzjgwhController.class);
  @Resource
  private PsamAzjgwhBPO psamAzjgwhBPO;

  @ApiOperation(value = "查询列表")
  @PostMapping("/getList")
  public AjaxResponse getList(@RequestBody @Valid PsamAzjgDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    Integer pagenum = dto.getPagenum();
    Integer pagesize = dto.getPagesize();
    try {
      PageResult pageResult = psamAzjgwhBPO.getList(dto, pagenum, pagesize);
      ajaxResponse.setData("data", pageResult);
      ajaxResponse.setData("pagesize", pagesize);
      ajaxResponse.setData("pagenum", pagenum);
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e.getMessage());
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("系统异常");
    }
    return ajaxResponse;
  }
  @ApiOperation(value = "详细信息查询")
  @PostMapping("/getInfo")
  public AjaxResponse getInfo(@RequestBody PsamQueryFjDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamAzjgwhBPO.getInfo(dto);
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e.getMessage());
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("系统异常");
    }
    return ajaxResponse;
  }
  @ApiOperation(value = "保存")
  @PostMapping("/saveData")
  public AjaxResponse saveData(@RequestBody @Valid PsamAzjgwhDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamAzjgwhBPO.saveData(dto);
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e.getMessage());
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("系统异常");
    }
    return ajaxResponse;
  }
}
