package cn.momia.mapi.api.admin;

import cn.momia.api.course.CourseServiceApi;
import cn.momia.api.course.OrderServiceApi;
import cn.momia.api.im.ImServiceApi;
import cn.momia.common.core.http.MomiaHttpResponse;
import cn.momia.mapi.api.AbstractApi;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * TODO Admin 相关的内容从MAPI中分离出去
 */
@RestController
@RequestMapping("/admin")
public class AdminApi extends AbstractApi {
    @Autowired private ImServiceApi imServiceApi;
    @Autowired private CourseServiceApi courseServiceApi;
    @Autowired private OrderServiceApi orderServiceApi;

    @RequestMapping(value = "/im/group", method = RequestMethod.POST)
    public MomiaHttpResponse createGroup(@RequestParam(value = "coid") long courseId,
                                         @RequestParam(value = "sid") long courseSkuId,
                                         @RequestParam(value = "tids") String teachers,
                                         @RequestParam(value = "name") String groupName) {
        if (courseId <= 0 || courseSkuId <= 0 || StringUtils.isBlank(teachers) || StringUtils.isBlank(groupName)) return MomiaHttpResponse.BAD_REQUEST;

        Set<Long> teacherUserIds = new HashSet<Long>();
        for (String teacher : Splitter.on(",").trimResults().omitEmptyStrings().split(teachers)) {
            teacherUserIds.add(Long.valueOf(teacher));
        }
        if (teacherUserIds.isEmpty()) return MomiaHttpResponse.FAILED("创建群组失败，至少要有一个群成员");

        return MomiaHttpResponse.SUCCESS(imServiceApi.createGroup(courseId, courseSkuId, teacherUserIds, groupName));
    }

    @RequestMapping(value = "/course/booking/batch", method = RequestMethod.POST)
    public MomiaHttpResponse batchBooking(@RequestParam String uids,
                                          @RequestParam(value = "coid") long courseId,
                                          @RequestParam(value = "sid") long skuId) {
        Set<Long> userIds = new HashSet<Long>();
        for (String userId : Splitter.on(",").omitEmptyStrings().trimResults().split(uids)) {
            userIds.add(Long.valueOf(userId));
        }

        List<Long> failedUserIds = courseServiceApi.batchBooking(userIds, courseId, skuId);
        for (long userId : userIds) {
            if (!failedUserIds.contains(userId)) imServiceApi.joinGroup(userId, courseId, skuId);
        }

        return MomiaHttpResponse.SUCCESS(failedUserIds);
    }

    @RequestMapping(value = "/course/cancel/batch", method = RequestMethod.POST)
    public MomiaHttpResponse batchCancel(@RequestParam String uids,
                                         @RequestParam(value = "coid") long courseId,
                                         @RequestParam(value = "sid") long skuId) {
        Set<Long> userIds = new HashSet<Long>();
        for (String userId : Splitter.on(",").omitEmptyStrings().trimResults().split(uids)) {
            userIds.add(Long.valueOf(userId));
        }

        boolean successful = courseServiceApi.batchCancel(userIds, courseId, skuId);
        if (successful) {
            for (long userId : userIds) {
                imServiceApi.leaveGroup(userId, courseId, skuId);
            }

            orderServiceApi.batchExtendPackageTimes(userIds, courseId, skuId, 1);
        }

        return MomiaHttpResponse.SUCCESS(successful);
    }

    @RequestMapping(value = "/package/time/extend", method = RequestMethod.POST)
    public MomiaHttpResponse extendPackageTime(@RequestParam(value = "pid") long packageId, @RequestParam int time) {
        if (packageId <= 0 || time <= 0) return MomiaHttpResponse.BAD_REQUEST;
        return MomiaHttpResponse.SUCCESS(orderServiceApi.extendPackageTime(packageId, time));
    }
}

@@ -641,16 +641,44 @@
 oller;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -3402,15 +3402,18 @@
     
-boolean
+List%3CLong%3E
  suc
@@ -3419,16 +3419,23 @@
 ccessful
+UserIds
  = cours
@@ -3497,16 +3497,17 @@
     if (
+!
 successf
@@ -3508,16 +3508,33 @@
 ccessful
+UserIds.isEmpty()
 ) %7B%0A    
@@ -3552,33 +3552,43 @@
  (long userId : 
-u
+successfulU
 serIds) %7B%0A      
@@ -3714,17 +3714,27 @@
 geTimes(
-u
+successfulU
 serIds, 
@@ -3757,32 +3757,219 @@
  1);%0A        %7D%0A%0A
+        List%3CLong%3E failedUserIds = new ArrayList%3CLong%3E();%0A        for (long userId : userIds) %7B%0A            if (!successfulUserIds.contains(userId)) failedUserIds.add(userId);%0A        %7D%0A%0A
         return M
@@ -3993,26 +3993,29 @@
 SUCCESS(
-successful
+failedUserIds
 );%0A    %7D

fix bug