package com.ym.mec.student.controller;

import com.alibaba.fastjson.JSONArray;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.StudentPaymentOrderDao;
import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dao.VipGroupActivityDao;
import com.ym.mec.biz.dal.dto.BuyDoubleEleven2021Dto;
import com.ym.mec.biz.dal.dto.Cloud2022ActivityDto;
import com.ym.mec.biz.dal.entity.ActivityDetailDto;
import com.ym.mec.biz.dal.entity.SporadicChargeInfo;
import com.ym.mec.biz.dal.entity.VipGroupActivity;
import com.ym.mec.biz.service.MarketActivityService;
import com.ym.mec.biz.service.SporadicChargeInfoService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.yonge.log.model.AuditLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RequestMapping("activity")
@Api(tags = "活动")
@RestController
public class ActivityController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysConfigDao sysConfigDao;
    @Autowired
    private SporadicChargeInfoService sporadicChargeInfoService;
    @Autowired
    private MarketActivityService marketActivityService;
    @Autowired
    private StudentPaymentOrderDao studentPaymentOrderDao;
    @Autowired
    private VipGroupActivityDao vipGroupActivityDao;

    @ApiOperation(value = "分部双11活动信息")
    @GetMapping("/doubleEleven2020")
    public HttpResponseResult<List<SporadicChargeInfo>> doubleEleven2020(){
        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);
    }

    @ApiOperation(value = "分部双11活动信息")
    @RequestMapping("/doubleEleven2021")
    public HttpResponseResult doubleEleven2021(){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed("请先登录");
        }
        if (sysUser.getOrganId() == null) {
            return failed("用户信息有误，没有分部信息");
        }
        Map<String,Object> resultMap = new HashMap<>(1);
        List<ActivityDetailDto> activityDetailDtos = marketActivityService.queryActivity(2, sysUser.getOrganId());
        resultMap.put("activityDetailList",activityDetailDtos);
        resultMap.put("startTime",null);
        resultMap.put("endTime",null);
        if(activityDetailDtos != null && activityDetailDtos.size() > 0){
            ActivityDetailDto activityDetailDto = activityDetailDtos.get(0);
            resultMap.put("startTime",activityDetailDto.getStartTime());
            resultMap.put("endTime",activityDetailDto.getEndTime());
        }
//        resultMap.put("horseRaceLampList",luckDrawPrizeService.queryHorseRaceLampDto(2));
//        LuckDrawCount luckDrawCount = luckDrawCountService.findByIdAndGroupId(sysUser.getId(),2);
//        if(luckDrawCount != null){
//            resultMap.put("drawNum",luckDrawCount.getAvailableCount());
//        }else {
//            resultMap.put("drawNum",0);
//        }
        return succeed(resultMap);
    }

    @ApiOperation("2021双十一活动购买")
    @PostMapping(value = "/buyDoubleEleven2021")
    @AuditLogAnnotation(operateName = "2021双十一活动购买")
    public HttpResponseResult buyDoubleEleven2021(@RequestBody BuyDoubleEleven2021Dto buyDoubleEleven2021Dto) throws Exception {
//        return practiceGroupService.buyDoubleEleven2021(buyDoubleEleven2021Dto);
        return failed("购买入口已关闭，请联系管理员");
    }

    @ApiOperation("获取2022云教练活动列表")
    @PostMapping(value = "/queryActivity")
    @AuditLogAnnotation(operateName = "获取2022云教练活动列表")
    public HttpResponseResult<List<VipGroupActivity>> queryActivity(){
        String configValue = sysConfigDao.findConfigValue("2022_cloud_activity_config");
        List<Cloud2022ActivityDto> cloud2022ActivityDtos = JSONArray.parseArray(configValue, Cloud2022ActivityDto.class);
        String collect = cloud2022ActivityDtos.stream().map(e -> e.getActivityId()).collect(Collectors.joining(","));
        return succeed(vipGroupActivityDao.queryByIds(collect));
    }

    @ApiOperation(value = "学员已购买的双十一活动信息")
    @GetMapping("/queryStudentDoubleEleven2021Order")
    public Object queryStudentDoubleEleven2021Order() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        List<ActivityDetailDto> activityDetailDtos = marketActivityService.queryActivity(2, user.getOrganId());
        String activitys = studentPaymentOrderDao.queryStudentDoubleEleven2021Order(user.getId());
        List<Integer> activityList = new ArrayList<>();
        if(StringUtils.isNotEmpty(activitys)){
            for (ActivityDetailDto activityDetailDto : activityDetailDtos) {
                if(activitys.contains(activityDetailDto.getActivityId().toString())){
                    if(activityDetailDto.getCategoryName().equals("1v1")){
                        if(activityDetailDto.getCourseNum().intValue() == 20){
                            activityList.add(1);
                        }else {
                            activityList.add(2);
                        }
                    }
                    if(activityDetailDto.getCategoryName().equals("1v2")){
                        if(activityDetailDto.getCourseNum().intValue() == 20){
                            activityList.add(3);
                        }else {
                            activityList.add(4);
                        }
                    }
                    if(activityDetailDto.getCategoryName().equals("乐理课")){
                        activityList.add(5);
                    }
                }
            }
            return succeed(StringUtils.join(activityList,","));
        }
        return succeed("");
    }
}
