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

import com.starry.admin.common.oss.service.IOssFileService;
import com.starry.admin.modules.clerk.module.entity.PlayAvatarFrameInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayAvatarFrameInfoAddVo;
import com.starry.admin.modules.clerk.module.vo.PlayAvatarFrameInfoQueryVo;
import com.starry.admin.modules.clerk.service.IPlayAvatarFrameInfoService;
import com.starry.admin.utils.SecurityUtils;
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.validation.annotation.Validated;
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-06-06
 */
@RestController
@RequestMapping("/clerk/avatarFrame")
public class PlayAvatarFrameInfoController {
    @Resource
    private IPlayAvatarFrameInfoService playAvatarFrameInfoService;


    @Resource
    private IOssFileService ossFileService;


    @PostMapping("/listByPage")
    public R listByPage(@Validated @RequestBody PlayAvatarFrameInfoQueryVo vo) {
        return R.ok(playAvatarFrameInfoService.selectByPage(vo));
    }

    /**
     * 查询店员头像框列表
     */
    @GetMapping("/listAll")
    public R listAll() {
        return R.ok(playAvatarFrameInfoService.selectAll());
    }


    @PostMapping("/uploadFile")
    public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        String fileAddress = ossFileService.upload(file.getInputStream(), SecurityUtils.getTenantId(), file.getOriginalFilename());
        return R.ok(fileAddress);
    }


    /**
     * 新增店员头像框
     */
    @Log(title = "店员头像框", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@Validated @RequestBody PlayAvatarFrameInfoAddVo vo) {
        boolean success = playAvatarFrameInfoService.create(ConvertUtil.entityToVo(vo, PlayAvatarFrameInfoEntity.class));
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改店员头像框
     */
    @Log(title = "店员头像框", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable String id, @RequestBody PlayAvatarFrameInfoEntity playAvatarFrameInfo) {
        playAvatarFrameInfo.setId(id);
        boolean success = playAvatarFrameInfoService.update(playAvatarFrameInfo);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除店员头像框
     */
    @Log(title = "店员头像框", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.ok(playAvatarFrameInfoService.deletePlayAvatarFrameInfoByIds(ids));
    }
}
