package com.template.controller;


import com.template.api.AlumniAdminApplyControllerAPI;
import com.template.model.enumModel.eModelType;
import com.template.model.pojo.AlumniAdminApply;
import com.template.model.request.setAdminRequest;
import com.template.model.result.CommonResult;
import com.template.model.vo.UserVo;
import com.template.services.AlumniAdminApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-11-25
 */
@RestController
public class AlumniAdminApplyController implements AlumniAdminApplyControllerAPI {

    @Autowired
    private AlumniAdminApplyService alumniAdminApplyService;

    @Override
    public CommonResult setImageAdmin(setAdminRequest sar, BindingResult bindingResult) {
        AlumniAdminApply result = alumniAdminApplyService.queryImageAdmin();
        if(result == null){
            result = new AlumniAdminApply();
        }

        result.setAdmin(StringUtils.join( sar.getAdmins().stream().map(UserVo::getId).collect(Collectors.toList()),","));
        result.setAdminName(StringUtils.join( sar.getAdmins().stream().map(UserVo::getName).collect(Collectors.toList()),","));
        result.setModelId(eModelType.Image.getValue());
        boolean insertOrUpdate = alumniAdminApplyService.insertOrUpdateAdmin(result);
        if(!insertOrUpdate){
            return CommonResult.fail("设置失败");
        }

        return CommonResult.ok("设置成功");
    }

    @Override
    public CommonResult setActivityAdmin(setAdminRequest sar, BindingResult bindingResult) {
        AlumniAdminApply result = alumniAdminApplyService.queryActivityAdmin();
        if(result == null){
            result = new AlumniAdminApply();
        }

        result.setAdmin(StringUtils.join( sar.getAdmins().stream().map(UserVo::getId).collect(Collectors.toList()),","));
        result.setAdminName(StringUtils.join( sar.getAdmins().stream().map(UserVo::getName).collect(Collectors.toList()),","));
        result.setModelId(eModelType.Activity.getValue());
        boolean insertOrUpdate = alumniAdminApplyService.insertOrUpdateAdmin(result);
        if(!insertOrUpdate){
            return CommonResult.fail("设置失败");
        }

        return CommonResult.ok("设置成功");
    }

    @Override
    public CommonResult setEndorseAdmin(setAdminRequest sar, BindingResult bindingResult) {
        AlumniAdminApply result = alumniAdminApplyService.queryEndorseAdmin();
        if(result == null){
            result = new AlumniAdminApply();
        }

        result.setAdmin(StringUtils.join( sar.getAdmins().stream().map(UserVo::getId).collect(Collectors.toList()),","));
        result.setAdminName(StringUtils.join( sar.getAdmins().stream().map(UserVo::getName).collect(Collectors.toList()),","));
        result.setModelId(eModelType.Endorse.getValue());
        boolean insertOrUpdate = alumniAdminApplyService.insertOrUpdateAdmin(result);
        if(!insertOrUpdate){
            return CommonResult.fail("设置失败");
        }

        return CommonResult.ok("设置成功");
    }
}

