package com.starry.admin.modules.play.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starry.admin.common.oss.service.IOssFileService;
import com.starry.admin.modules.play.module.entity.PlayResourcesInfoEntity;
import com.starry.admin.modules.play.module.vo.PlayResourcesInfoAddVo;
import com.starry.admin.modules.play.module.vo.PlayResourcesInfoReviewVo;
import com.starry.admin.modules.play.service.IPlayResourcesInfoService;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 陪聊资源Controller
 *
 * @author admin
 * @since 2024-03-24
 */
@RestController
@RequestMapping("/play/resource/info")
public class PlayResourcesInfoController {
    @Resource
    private IPlayResourcesInfoService playResourcesInfoService;

    @Resource
    private IOssFileService ossFileService;


    /**
     * 查询陪聊资源列表
     */
    @GetMapping("/list")
    public R list(PlayResourcesInfoEntity playResourcesInfo) {
        IPage<PlayResourcesInfoEntity> list = playResourcesInfoService.selectPlayResourcesInfoByPage(playResourcesInfo);
        return R.ok(list);
    }


    /**
     * 获取陪聊资源详细信息
     */
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id) {
        return R.ok(playResourcesInfoService.selectPlayResourcesInfoById(id));
    }

    /**
     * 陪聊上传资源（图片/签名/视频/录音小样等）
     */
    @Log(title = "陪聊资源", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@RequestBody PlayResourcesInfoAddVo vo, @RequestParam("file") MultipartFile file) throws IOException {
        // 校验文件类型是否正常

        // 上传文件到OSS
        String fileAddress = ossFileService.upload(file.getInputStream(), "", file.getOriginalFilename());

        // 保持陪聊资源
        PlayResourcesInfoEntity entity = ConvertUtil.entityToVo(vo, PlayResourcesInfoEntity.class);
        entity.setAddress(fileAddress);
        boolean success = playResourcesInfoService.create(entity);
        // 发送审核通知给对应的管理员

        // 返回信息
        if (success) {
            return R.ok("成功");
        }
        return R.error("添加失败");
    }


    /**
     * 陪聊资料审核
     */
    @Log(title = "陪聊资源审核", businessType = BusinessType.UPDATE)
    @PostMapping("/examine")
    public R examinePlayResourcesInfo(@RequestBody PlayResourcesInfoReviewVo vo) {
        // 验证权限，判断当前接口调用人是否有权限操作该接口

        // 资料审核
        playResourcesInfoService.examinePlayResourcesInfo(vo.getId(), vo.isSuccess(), vo.getRemark());
        return R.ok("成功");
    }


    /**
     * 删除陪聊资源
     */
    @Log(title = "陪聊资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.ok(playResourcesInfoService.deletePlayResourcesInfoByIds(ids));
    }
}
