package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.SporadicChargeInfo;
import com.ym.mec.biz.service.SporadicChargeInfoService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;

@RequestMapping("activity")
@Api(tags = "活动")
@RestController
public class ActivityController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SporadicChargeInfoService sporadicChargeInfoService;

    @ApiOperation(value = "分部双11活动信息")
    @GetMapping("/doubleEleven2020")
    public HttpResponseResult<List<SporadicChargeInfo>> doubleEleven2020() throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("请先登录");
        }
        if (sysUser.getOrganId() == null) {
            return failed("用户信息有误，没有分部信息");
        }
        Integer organId = sysUser.getOrganId();

        List<SporadicChargeInfo> activeInfos = sporadicChargeInfoService.getOrganActiveInfo(organId, 12);
        activeInfos.sort(Comparator.comparing(SporadicChargeInfo::getAmount).reversed());
        if (activeInfos.size() <= 0) {
            return failed("分部活动不存在");
        }
        return succeed(activeInfos);
    }
}
