package com.zjrs.ggfw.rlzysc.controller;

import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.ggfw.rlzysc.bpo.ZzzdBPO;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.ZjQueryListDTO;
import com.zjrs.ggfw.rlzysc.dto.zzzd.ZzzdZphListQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.zzzd.ZzzdZwListQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.zzzd.ZzzdZwListQueryForDwDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
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.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/rlzysc/dwzp/zzzd")
public class ZzzdController {

    private static final Logger LOG = LoggerFactory.getLogger(ZzzdController.class);

    @Autowired
    private ZzzdBPO zzzdBPO;

    @PostMapping("/getzphlist")
    public AjaxResponse getZphlist(@RequestBody @Valid ZzzdZphListQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            String errmsg = "";
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg = errmsg + fieldErrors.get(i).getDefaultMessage();
                } else {
                    errmsg = errmsg + "；" + fieldErrors.get(i).getDefaultMessage();
                }
            }
            ajaxResponse.setErrorMsg(errmsg);
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", zzzdBPO.getZphlist(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        }catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @PostMapping("/getZwListToQy")
    public AjaxResponse getZwListToQy(@RequestBody @Valid ZzzdZwListQueryForDwDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            String errmsg = "";
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg = errmsg + fieldErrors.get(i).getDefaultMessage();
                } else {
                    errmsg = errmsg + "；" + fieldErrors.get(i).getDefaultMessage();
                }
            }
            ajaxResponse.setErrorMsg(errmsg);
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", zzzdBPO.getZwListToQy(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        }catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @PostMapping("/getZwList")
    public AjaxResponse getZwList(@RequestBody @Valid ZzzdZwListQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            String errmsg = "";
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg = errmsg + fieldErrors.get(i).getDefaultMessage();
                } else {
                    errmsg = errmsg + "；" + fieldErrors.get(i).getDefaultMessage();
                }
            }
            ajaxResponse.setErrorMsg(errmsg);
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", zzzdBPO.getZwList(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        }catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @PostMapping("/getQyList")
    public AjaxResponse getQyList(@RequestBody @Valid ZjQueryListDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            String errmsg = "";
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg = errmsg + fieldErrors.get(i).getDefaultMessage();
                } else {
                    errmsg = errmsg + "；" + fieldErrors.get(i).getDefaultMessage();
                }
            }
            ajaxResponse.setErrorMsg(errmsg);
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", zzzdBPO.getQyList(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        }catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
}

