package com.yonge.cooleshow.admin.controller.open;

import com.yonge.cooleshow.admin.io.request.im.IMNotifyMessageVO;
import com.yonge.cooleshow.admin.io.request.im.UserFriendInfoVO;
import com.yonge.cooleshow.biz.dal.service.ImUserFriendService;
import com.yonge.cooleshow.biz.dal.wrapper.im.CustomerService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户通讯录表(ImUserFriend)表控制层
 *
 * @author zx
 * @since 2022-03-22 10:45:59
 */
@Api(tags = "用户通讯录表")
@RestController
@RequestMapping("/open/im")
public class ImUserFriendController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private ImUserFriendService imUserFriendService;

    @ApiOperation("新用户添加客服")
    @PostMapping(value = "/customerService")
    public HttpResponseResult<Boolean> customerService(@RequestBody UserFriendInfoVO info) {

        if (info.invalidRequestParam()) {
            return failed("无效的请求参数");
        }

        // 新用户自动绑定系统客服
        int ret = imUserFriendService.registerUserBindCustomerService(info.getUserId(), info.getFriendIds());

        return succeed(ret > 0);
    }

    /**
     * 发送系统客服消息
     * @param info IMNotifyMessageVO
     * @return HttpResponseResult<Boolean>
     */
    @PostMapping(value = "/message")
    public HttpResponseResult<Boolean> sendSysCustomerServiceMessage(@RequestBody IMNotifyMessageVO info) {

        if (info.invalidRequestParam()) {
            return failed("无效的请求参数");
        }

        // 发送客服通知消息
        imUserFriendService.sendCustomerServiceNotifyMessage(info.getSender(),
                CustomerService.NotifyMessage.from(info.jsonString()));

        return succeed();
    }
}

