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.SyzdqyglBPO;
import com.zjrs.zwnw.rlzysc.dto.SyzdqyglQueryDTO;
import com.zjrs.zwnw.rlzysc.dto.ZjUDTO;
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/syzdqygl")
public class SyzdqyglController {
    private static final Logger LOG = LoggerFactory.getLogger(SyzdqyglController.class);

    @Resource
    private SyzdqyglBPO syzdqyglBPO;
    /*
    获取列表
     */
    @ApiOperation(value = "获取列表", notes = "获取列表")
    @PostMapping(value = "/getSyzdqyglList")
    public AjaxResponse getSyzdqyglList(@RequestBody @Valid SyzdqyglQueryDTO 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", syzdqyglBPO.getSyzdqyglList(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 = "/updateSyzdqyglTjzt")
    public AjaxResponse updateSyzdqyglTjzt(@RequestBody @Valid ZjUDTO 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 {
            syzdqyglBPO.updateSyzdqyglTjzt(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;
    }
}
