package cn.momia.mapi.api.v1.user;

import cn.momia.api.user.ChildServiceApi;
import cn.momia.api.user.UserServiceApi;
import cn.momia.api.user.dto.Child;
import cn.momia.common.api.http.MomiaHttpResponse;
import cn.momia.common.api.util.CastUtil;
import cn.momia.common.util.SexUtil;
import cn.momia.mapi.api.v1.AbstractV1Api;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
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.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/child")
public class ChildV1Api extends AbstractV1Api {
    @Autowired private UserServiceApi userServiceApi;
    @Autowired private ChildServiceApi childServiceApi;

    @RequestMapping(method = RequestMethod.POST)
    public MomiaHttpResponse addChild(@RequestParam String utoken, @RequestParam String children) {
        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;
        if (StringUtils.isBlank(children)) return MomiaHttpResponse.BAD_REQUEST;

        return MomiaHttpResponse.SUCCESS(processUser(childServiceApi.add(utoken, children)));
    }

    @RequestMapping(method = RequestMethod.GET)
    public MomiaHttpResponse getChild(@RequestParam String utoken, @RequestParam(value = "cid") long childId) {
        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;
        if (childId <= 0) return MomiaHttpResponse.BAD_REQUEST;

        return MomiaHttpResponse.SUCCESS(processChild(childServiceApi.get(utoken, childId)));
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MomiaHttpResponse listChildren(@RequestParam String utoken) {
        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;
        return MomiaHttpResponse.SUCCESS(processChildren(childServiceApi.list(utoken)));
    }

    @RequestMapping(value = "/avatar", method = RequestMethod.POST)
    public MomiaHttpResponse updateChildAvatar(@RequestParam String utoken,
                                               @RequestParam(value = "cid") long childId,
                                               @RequestParam String avatar) {
        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;
        if (childId <= 0) return MomiaHttpResponse.BAD_REQUEST;
        if (StringUtils.isBlank(avatar)) return MomiaHttpResponse.FAILED("孩子头像不能为空");

        return MomiaHttpResponse.SUCCESS(processUser(childServiceApi.updateAvatar(utoken, childId, avatar)));
    }

    @RequestMapping(value = "/name", method = RequestMethod.POST)
    public MomiaHttpResponse updateChildName(@RequestParam String utoken,
                                             @RequestParam(value = "cid") long childId,
                                             @RequestParam String name) {
        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;
        if (childId <= 0) return MomiaHttpResponse.BAD_REQUEST;
        if (StringUtils.isBlank(name)) return MomiaHttpResponse.FAILED("孩子姓名不能为空");

        return MomiaHttpResponse.SUCCESS(processUser(childServiceApi.updateName(utoken, childId, name)));
    }

    @RequestMapping(value = "/sex", method = RequestMethod.POST)
    public MomiaHttpResponse updateChildSex(@RequestParam String utoken,
                                            @RequestParam(value = "cid") long childId,
                                            @RequestParam String sex) {
        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;
        if (childId <= 0) return MomiaHttpResponse.BAD_REQUEST;
        if (SexUtil.isInvalid(sex)) return MomiaHttpResponse.FAILED("无效的孩子性别");

        return MomiaHttpResponse.SUCCESS(processUser(childServiceApi.updateSex(utoken, childId, sex)));
    }

    @RequestMapping(value = "/birthday", method = RequestMethod.POST)
    public MomiaHttpResponse updateChildBirthday(@RequestParam String utoken,
                                                 @RequestParam(value = "cid") long childId,
                                                 @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;
        if (childId <= 0) return MomiaHttpResponse.BAD_REQUEST;
        if (birthday == null) return MomiaHttpResponse.FAILED("无效的孩子生日");

        return MomiaHttpResponse.SUCCESS(processUser(childServiceApi.updateBirthday(utoken, childId, birthday)));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public MomiaHttpResponse deleteChild(@RequestParam String utoken, @RequestParam(value = "cid") long childId) {
        if (StringUtils.isBlank(utoken)) return MomiaHttpResponse.TOKEN_EXPIRED;
        if (childId <= 0) return MomiaHttpResponse.BAD_REQUEST;

        return MomiaHttpResponse.SUCCESS(processUser(childServiceApi.delete(utoken, childId)));
    }
}

@@ -91,160 +91,41 @@
 mia.
-api.user.UserServiceApi;%0Aimport cn.momia.api.user.dto.Child;%0Aimport cn.momia.common.api.http.MomiaHttpResponse;%0Aimport cn.momia.common.api.util.CastUtil
+common.api.http.MomiaHttpResponse
 ;%0Aim
@@ -206,47 +206,8 @@
 pi;%0A
-import com.alibaba.fastjson.JSONArray;%0A
 impo
@@ -642,63 +642,12 @@
 til.
-ArrayList;%0Aimport java.util.Date;%0Aimport java.util.List
+Date
 ;%0A%0A@
@@ -747,62 +747,8 @@
 i %7B%0A
-    @Autowired private UserServiceApi userServiceApi;%0A
     

fix bug