package com.jpsoft.shinestar.modules.mobile.controller;

import com.jpsoft.shinestar.modules.base.entity.MessageReport;
import com.jpsoft.shinestar.modules.base.service.MessageReportService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/mobile/messageReportApi")
@Api(description = "messageReport")
public class MessageReportApiController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MessageReportService messageReportService;

    @ApiOperation(value="添加信息")
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public MessageResult<MessageReport> add(String content,String phone,String photo,
                                            String token,
                                            @RequestAttribute String subject){
        MessageResult<MessageReport> msgResult = new MessageResult<>();

        try {
            MessageReport messageReport = new MessageReport();

            messageReport.setId(UUID.randomUUID().toString());
            messageReport.setContent(content);
            messageReport.setDelFlag(false);
            messageReport.setCreateBy(subject);
            messageReport.setCreateTime(new Date());
            messageReport.setPhone(phone);
            messageReport.setPhoto(photo);
            
            int affectCount = messageReportService.insert(messageReport);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(messageReport);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }
}
