package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserCall;
import com.ruoyi.cai.dto.admin.vo.UserCallAdminVo;
import com.ruoyi.cai.service.UserCallService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.BeanConvertUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 通话记录
 *
 * @author 77
 * @date 2023-12-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/userCall")
public class UserCallController extends BaseController {

    private final UserCallService userCallService;
    private final UserService userService;

    /**
     * 查询通话记录列表
     */
    @SaCheckPermission("cai:userCall:list")
    @GetMapping("/list")
    public TableDataInfo<UserCallAdminVo> list(UserCallAdminVo bo, PageQuery pageQuery) {
        Page<UserCallAdminVo> page = userCallService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    /**
     * 获取通话记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:userCall:query")
    @GetMapping("/{id}")
    public R<UserCallAdminVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        UserCall userCall = userCallService.getById(id);
        UserCallAdminVo resp = BeanConvertUtil.convertTo(userCall, UserCallAdminVo::new);
        User fromUser = userService.getById(userCall.getFromUid());
        if(fromUser != null){
            resp.setFromMobile(fromUser.getMobile());
            resp.setFromAvatar(fromUser.getAvatar());
        }
        User toUser = userService.getById(userCall.getToUid());
        if(toUser != null){
            resp.setToMobile(toUser.getMobile());
            resp.setToAvatar(toUser.getAvatar());
        }
        return R.ok(resp);
    }

    @GetMapping("/trace")
    public R<UserCallAdminVo> getInfoTraceId(String traceId) {
        UserCall userCall = userCallService.getOne(Wrappers.lambdaQuery(UserCall.class).eq(UserCall::getTraceId,traceId).last("limit 1"));
        UserCallAdminVo resp = BeanConvertUtil.convertTo(userCall, UserCallAdminVo::new);
        User fromUser = userService.getById(userCall.getFromUid());
        if(fromUser != null){
            resp.setFromMobile(fromUser.getMobile());
            resp.setFromAvatar(fromUser.getAvatar());
        }
        User toUser = userService.getById(userCall.getToUid());
        if(toUser != null){
            resp.setToMobile(toUser.getMobile());
            resp.setToAvatar(toUser.getAvatar());
        }
        return R.ok(resp);
    }

    /**
     * 删除通话记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:userCall:remove")
    @Log(title = "通话记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userCallService.removeBatchByIds(Arrays.asList(ids)));
    }
}
