package com.fdkankan.ucenter.controller;

import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.IntercomMessage;
import com.fdkankan.ucenter.mapper.IIntercomMessageMapper;
import com.fdkankan.ucenter.service.IIntercomMessageService;
import com.fdkankan.ucenter.vo.request.IntercomMessageParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/ucenter/intercomMessage")
public class InterComMessageController {

    @Autowired
    private IIntercomMessageService intercomMessageService;

    /**
     * 保存留言
     */
    @RequestMapping(value = "/leave", method = RequestMethod.POST)
    public Result leave(@RequestBody IntercomMessageParam message) throws Exception {
        if (StringUtils.isEmpty(message.getContact()) || StringUtils.isEmpty(message.getContent())){
            return Result.failure(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }

        IntercomMessage interComMessage = new IntercomMessage();
        interComMessage.setContent(message.getContent());
        interComMessage.setContact(message.getContact());
        interComMessage.setCreateTime(new Date());
        interComMessage.setUpdateTime(new Date());
        interComMessage.setRecStatus("A");
        interComMessage.setNoteType(1);
        interComMessage.setState(1);
        intercomMessageService.save(interComMessage);
        return Result.success();
    }
}
