package com.zjrs.zwnw.rlzysc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.rlzysc.bpo.SylbtpglBPO;
import com.zjrs.zwnw.rlzysc.dto.SylbtpglQueryDTO;
import com.zjrs.zwnw.rlzysc.dto.SylbtpglUDTO;
import com.zjrs.zwnw.rlzysc.dto.ZjDTO;
import com.zjrs.zwnw.rlzysc.dto.ZjQueryDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

/*
首页轮播图片管理
 */
@Api("首页轮播图片管理")
@RestController
@RequestMapping("/api/rlzysc/sylbtpgl")
public class SylbtpglController {
    private static final Logger LOG = LoggerFactory.getLogger(SylbtpglController.class);

    @Resource
    private SylbtpglBPO sylbtpglBPO;
    /*
    获取列表
     */
    @ApiOperation(value = "获取列表", notes = "获取列表")
    @PostMapping(value = "/getSylbtpglList")
    public AjaxResponse getSylbtpglList(@RequestBody @Valid SylbtpglQueryDTO 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", sylbtpglBPO.getSylbtpglList(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;
    }

    /*
    更新置顶状态
     */
    @ApiOperation(value = "更新置顶状态", notes = "更新置顶状态")
    @PostMapping(value = "/updateSylbtpglZdzt")
    public AjaxResponse updateSylbtpglZdzt(@RequestBody @Valid ZjDTO 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 {
            sylbtpglBPO.updateSylbtpglZdzt(dto);
        } 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;
    }
    /*
    删除首页轮播图片信息
     */
    @ApiOperation(value = "删除首页轮播图片信息", notes = "删除首页轮播图片信息")
    @PostMapping(value = "/delSylbtpglXx")
    public AjaxResponse delSylbtpglXx(@RequestBody @Valid ZjDTO 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 {
            sylbtpglBPO.delSylbtpglXx(dto);
        } 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;
    }
    /*
    获取首页轮播图片信息
     */
    @ApiOperation(value = "获取首页轮播图片信息", notes = "获取首页轮播图片信息")
    @PostMapping(value = "/getSylbtpglXx")
    public AjaxResponse getSylbtpglXx(@RequestBody @Valid ZjQueryDTO 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", sylbtpglBPO.getSylbtpglXx(dto));
        } 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;
    }
    /*
    保存首页轮播图片信息
     */
    @ApiOperation(value = "保存首页轮播图片信息", notes = "保存首页轮播图片信息")
    @PostMapping(value = "/saveSylbtpglXx")
    public AjaxResponse saveSylbtpglXx(@RequestBody @Valid SylbtpglUDTO 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("zjlsh", sylbtpglBPO.saveSylbtpglXx(dto));
        } 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;
    }
}
