package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.cai.domain.Anchor;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserOnline;
import com.ruoyi.cai.dto.admin.vo.AnchorAdminVo;
import com.ruoyi.cai.dto.admin.vo.AnchorFullAdminVo;
import com.ruoyi.cai.notice.YunxinHttpService;
import com.ruoyi.cai.service.AnchorService;
import com.ruoyi.cai.service.UserInfoService;
import com.ruoyi.cai.service.UserOnlineService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
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.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 女神列表
 *
 * @author 77
 * @date 2023-12-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/anchor")
public class AnchorController extends BaseController {

    private final AnchorService anchorService;
    private final UserService userService;
    private final UserInfoService userInfoService;
    private final UserOnlineService userOnlineService;

    /**
     * 查询女神列表列表
     */
    @SaCheckPermission("cai:anchor:list")
    @GetMapping("/list")
    public TableDataInfo<AnchorAdminVo> list(AnchorAdminVo bo, PageQuery pageQuery) {
        IPage<AnchorAdminVo> page = anchorService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }


    /**
     * 获取女神列表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:anchor:query")
    @GetMapping("/{id}")
    public R<Anchor> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(anchorService.getById(id));
    }

    @GetMapping("/full")
    public R<AnchorFullAdminVo> getInfo(@NotNull(message = "主键不能为空")
                             Long userId) {
        AnchorFullAdminVo vo = new AnchorFullAdminVo();
        vo.setUser(userService.getById(userId));
        vo.setAnchor(anchorService.getByUserId(userId));
        vo.setUserOnline(userOnlineService.getByUserId(userId));
        vo.setUserInfo(userInfoService.getByUserId(userId));
        return R.ok(vo);
    }

    @SaCheckPermission("cai:anchor:add")
    @Log(title = "女神列表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@RequestBody AnchorAdminVo bo) {
        User user = userService.getByUserCode(bo.getUsercode());
        if(user == null){
            return R.fail("用户不存在");
        }
        anchorService.joinAnchor(user.getId());
        return R.ok();
    }

    @SaCheckPermission("cai:anchor:edit")
    @Log(title = "女神列表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Anchor bo) {
        return toAjax(anchorService.updateById(bo));
    }

    @Autowired
    private YunxinHttpService yunxinHttpService;

    @SaCheckPermission("cai:anchor:remove")
    @Log(title = "女神列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userId}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long userId) {
        boolean boo = anchorService.closeAnchor(userId);
        if(boo){
            yunxinHttpService.cancelAnchorMessage(userId);
        }
        return R.ok();
    }
}
