package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.StudentDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.ActivityQueryDto;
import com.ym.mec.biz.dal.dto.OrganDoubleEleven2021StatisDto;
import com.ym.mec.biz.dal.dto.VipGroupActivityAddDto;
import com.ym.mec.biz.dal.entity.Student;
import com.ym.mec.biz.dal.entity.VipGroupActivity;
import com.ym.mec.biz.dal.page.DoubleEleven2021QueryInfo;
import com.ym.mec.biz.dal.page.VipGroupActivityQueryInfo;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.biz.service.VipGroupActivityService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
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.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Author Joburgess
 * @Date 2019/10/1
 */

@Api(tags = "vip课活动")
@RequestMapping("vipGroupActivity")
@RestController
public class VipGroupActivityController extends BaseController {

    @Autowired
    private VipGroupActivityService vipGroupActivityService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
    private TeacherDao teacherDao;
	@Autowired
    private StudentDao studentDao;

    @ApiOperation(value = "分部双11活动统计")
    @GetMapping("/organDoubleEleven2021Statis")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/organDoubleEleven2021Statis')")
    public HttpResponseResult organDoubleEleven2021Statis(OrganDoubleEleven2021StatisDto queryInfo){
        return succeed(vipGroupActivityService.organDoubleEleven2021Statis(queryInfo));
    }

    @ApiOperation(value = "分部2021双11活动学员列表")
    @GetMapping("/doubleEleven2021OrderDetail")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/doubleEleven2021OrderDetail')")
    public HttpResponseResult doubleEleven2021OrderDetail(DoubleEleven2021QueryInfo queryInfo){
        return succeed(vipGroupActivityService.doubleEleven2021OrderDetail(queryInfo));
    }

    @ApiOperation(value = "双11活动统计")
    @GetMapping("/doubleEleven2021Statis")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/doubleEleven2021Statis')")
    public HttpResponseResult doubleEleven2021Statis(){
        return succeed(vipGroupActivityService.doubleEleven2021Statis());
    }

    @ApiOperation(value = "新增vip课活动方案")
    @PostMapping("/addVipGroupActivity")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/addVipGroupActivity')")
    public Object addVipGroupActivity(@RequestBody VipGroupActivityAddDto vipGroupActivityAddDto){
        return succeed(vipGroupActivityService.addVipGroupActivity(vipGroupActivityAddDto));
    }

    @ApiOperation(value = "获取活动方案")
    @GetMapping("/get")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/get')")
    public Object get(Integer id){
        return succeed(vipGroupActivityService.get(id));
    }

    @ApiOperation(value = "分页查询活动方案")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/queryPage')")
    public Object queryPage(VipGroupActivityQueryInfo queryInfo){
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        Integer applyToStudentType = queryInfo.getApplyToStudentType();
        if(applyToStudentType != null){
            if(applyToStudentType == 0 || applyToStudentType == 1){
                queryInfo.setNewStudent(applyToStudentType);
            }
            if(applyToStudentType == 2 || applyToStudentType == 3){
                queryInfo.setMemberFlag(applyToStudentType==2?1:0);
            }
        }
        return succeed(vipGroupActivityService.queryPage(queryInfo));
    }

    @ApiOperation(value = "修改活动方案")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/update')")
    public Object update(@RequestBody VipGroupActivityAddDto vipGroupActivityAddDto){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("用户信息获取失败");
        }
        return succeed(vipGroupActivityService.updateVipGroupActivity(vipGroupActivityAddDto, sysUser.getId()));
    }

    @ApiOperation(value = "删除活动方案")
    @PostMapping("/delete")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/delete')")
    public Object delete(Integer id){
        if(Objects.isNull(id)){
            throw new BizException("请指定活动编号!");
        }
        vipGroupActivityService.delete(id);
        return succeed();
    }

    @ApiOperation(value = "根据课程类型获取对应课程活动方案")
    @GetMapping("/findByVipGroupCategory")
    @PreAuthorize("@pcs.hasPermissions('vipGroupActivity/findByVipGroupCategory')")
    public Object findByVipGroupCategory(ActivityQueryDto activityQueryDto){
        List<Integer> userIds = Arrays.stream(activityQueryDto.getStudentIds().split(",")).map(id -> Integer.valueOf(id)).collect(Collectors.toList());
        List<Student> students = studentDao.findByStudentIds(userIds);
        if(CollectionUtils.isEmpty(students)||students.size()!=userIds.size()){
            return failed("学员信息不存在");
        }
        long newStudentNumCount = students.stream().filter(s -> s.getIsNewUser()).count();
        long memberCount = students.stream().filter(e -> e.getMemberRankSettingId() != null).count();

        if(newStudentNumCount == 0){
            activityQueryDto.setNewStudentNum(1);
        }else if(newStudentNumCount == userIds.size()){
            activityQueryDto.setNewStudentNum(0);
        }
        if(memberCount == 0){
            activityQueryDto.setMemberNum(1);
        }else if(memberCount == userIds.size()){
            activityQueryDto.setMemberNum(0);
        }

        SysUser student = teacherDao.getUser(userIds.get(0));
        if(Objects.isNull(student)){
            return failed("学员信息不存在");
        }
        activityQueryDto.setOrganId(student.getOrganId().toString());
        activityQueryDto.setEnable(true);
        List<VipGroupActivity> vipGroupActivities = vipGroupActivityService.findByVipGroupCategory(activityQueryDto);
        return succeed(vipGroupActivities);
    }

}
