package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.LoginUser;
import com.platform.entity.FeedbackVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiFeedbackService;
import com.platform.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 * 描述: ApiFeedbackController <br>
 */
@Api(tags = "反馈")
@RestController
@RequestMapping("/api/feedback")
public class ApiFeedbackController extends ApiBaseAction {
    @Autowired
    private ApiFeedbackService feedbackService;

    /**
     * 添加反馈
     */
    @ApiOperation(value = "添加反馈")
    @PostMapping("save")
    public Object save(@RequestParam(value="mobile") String mobile,
                       @RequestParam(value="index") Integer index, @RequestParam(value="content") String content) {
//        JSONObject feedbackJson = super.getJsonRequest();
//        if (null != feedbackJson) {
//            FeedbackVo feedbackVo = new FeedbackVo();
//            feedbackVo.setUserId(loginUser.getUserId().intValue());
//            feedbackVo.setUserName(loginUser.getUsername());
//            feedbackVo.setMobile(feedbackJson.getString("mobile"));
//            feedbackVo.setFeedType(feedbackJson.getInteger("index"));
//            feedbackVo.setStatus(1);
//            feedbackVo.setContent(feedbackJson.getString("content"));
//            feedbackVo.setAddTime(new Date());
//            feedbackService.save(feedbackVo);
//            return super.toResponsSuccess("感谢你的反馈");
//        }

        if (!StringUtils.isEmpty(mobile) && index != null && !StringUtils.isEmpty(content)) {
            String regex = "^1[3-9][0-9]\\d{8}$";
            if(!mobile.matches(regex)){
                return toResponsFail("手机号不符合格式");
            }
            FeedbackVo feedbackVo = new FeedbackVo();
            feedbackVo.setUserId(getUserId().intValue());
            feedbackVo.setUserName(getUserName());
            feedbackVo.setMobile(mobile);
            feedbackVo.setFeedType(index);
            feedbackVo.setStatus(1);
            feedbackVo.setContent(content);
            feedbackVo.setAddTime(new Date());
            feedbackService.save(feedbackVo);
            return super.toResponsSuccess("感谢你的反馈");
        }
        return super.toResponsFail("反馈失败");
    }
}
