package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreVideo;
import shop.alien.entity.store.dto.StoreVideoSaveDto;
import shop.alien.store.service.StoreVideoService;

import java.util.List;

/**
 * 门店视频Controller
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"门店视频"})
@ApiSort(6)
@CrossOrigin
@RestController
@RequestMapping("/video")
@RequiredArgsConstructor
public class StoreVideoController {
    private final StoreVideoService storeVideoService;

    @ApiOperation("保存视频（单个或批量）")
    @ApiOperationSupport(order = 1)
    @PostMapping("/saveOrSaveBatch")
    public R<String> saveOrSaveBatch(@RequestBody StoreVideoSaveDto dto) {
        log.info("StoreVideoController.saveOrSaveBatch?dto={}", dto);
        if (storeVideoService.saveOrSaveBatch(dto)) {
            return R.success("保存成功");
        }
        return R.fail("保存失败");
    }

    @ApiOperation("新增视频")
    @ApiOperationSupport(order = 1)
    @PostMapping("/save")
    public R<String> save(@RequestBody StoreVideo storeVideo) {
        log.info("StoreVideoController.save?storeVideo={}", storeVideo);
        if (storeVideoService.save(storeVideo)) {
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("批量新增视频")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveBatch")
    public R<String> saveBatch(@RequestBody List<StoreVideo> storeVideoList) {
        log.info("StoreVideoController.saveBatch?storeVideoList={}", storeVideoList);
        if (storeVideoService.saveBatch(storeVideoList)) {
            return R.success("批量新增成功");
        }
        return R.fail("批量新增失败");
    }

    @ApiOperation("修改视频")
    @ApiOperationSupport(order = 3)
    @PostMapping("/update")
    public R<String> update(@RequestBody StoreVideo storeVideo) {
        log.info("StoreVideoController.update?storeVideo={}", storeVideo);
        if (storeVideoService.updateById(storeVideo)) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("新增或修改视频")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdate")
    public R<String> saveOrUpdate(@RequestBody StoreVideo storeVideo) {
        log.info("StoreVideoController.saveOrUpdate?storeVideo={}", storeVideo);
        if (storeVideoService.saveOrUpdate(storeVideo)) {
            if (storeVideo.getId() != null) {
                return R.success("修改成功");
            }
            return R.success("新增成功");
        }
        return R.fail("操作失败");
    }

    @ApiOperation("批量新增或修改视频")
    @ApiOperationSupport(order = 5)
    @PostMapping("/saveOrUpdateBatch")
    public R<String> saveOrUpdateBatch(@RequestBody List<StoreVideo> storeVideoList) {
        log.info("StoreVideoController.saveOrUpdateBatch?storeVideoList={}", storeVideoList);
        if (storeVideoService.saveOrUpdateBatch(storeVideoList)) {
            return R.success("批量操作成功");
        }
        return R.fail("批量操作失败");
    }

    @ApiOperation("根据ID删除视频")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "视频ID", dataType = "Integer", paramType = "query", required = true)})
    @PostMapping("/deleteById")
    public R<String> deleteById(Integer id) {
        log.info("StoreVideoController.deleteById?id={}", id);
        if (storeVideoService.removeById(id)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("批量删除视频")
    @ApiOperationSupport(order = 7)
    @PostMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestBody List<Integer> ids) {
        log.info("StoreVideoController.deleteBatch?ids={}", ids);
        if (storeVideoService.removeByIds(ids)) {
            return R.success("批量删除成功");
        }
        return R.fail("批量删除失败");
    }

    @ApiOperation("根据ID查询视频")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "视频ID", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getById")
    public R<StoreVideo> getById(Integer id) {
        log.info("StoreVideoController.getById?id={}", id);
        StoreVideo storeVideo = storeVideoService.getById(id);
        if (storeVideo != null) {
            return R.data(storeVideo);
        }
        return R.fail("未找到数据");
    }

    @ApiOperation("根据门店ID查询视频列表")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getByStoreId")
    public R<List<StoreVideo>> getByStoreId(Integer storeId) {
        log.info("StoreVideoController.getByStoreId?storeId={}", storeId);
        return R.data(storeVideoService.getByStoreId(storeId));
    }

    @ApiOperation("根据门店ID和业务ID查询视频列表")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "businessId", value = "业务ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getByStoreIdAndBusinessId")
    public R<List<StoreVideo>> getByStoreIdAndBusinessId(Integer storeId, Integer businessId) {
        log.info("StoreVideoController.getByStoreIdAndBusinessId?storeId={}&businessId={}", storeId, businessId);
        return R.data(storeVideoService.getByStoreIdAndBusinessId(storeId, businessId));
    }

    @ApiOperation("分页查询视频列表")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "businessId", value = "业务ID", dataType = "Integer", paramType = "query", required = false)
    })
    @GetMapping("/page")
    public R<IPage<StoreVideo>> page(Integer current, Integer size, Integer storeId, Integer businessId) {
        log.info("StoreVideoController.page?current={}&size={}&storeId={}&businessId={}", current, size, storeId, businessId);
        Page<StoreVideo> page = new Page<>(current, size);
        LambdaQueryWrapper<StoreVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(storeId != null, StoreVideo::getStoreId, storeId)
                .eq(businessId != null, StoreVideo::getBusinessId, businessId)
                .orderByAsc(StoreVideo::getImgSort)
                .orderByDesc(StoreVideo::getCreatedTime);
        IPage<StoreVideo> result = storeVideoService.page(page, queryWrapper);
        return R.data(result);
    }

    @ApiOperation("查询所有视频列表")
    @ApiOperationSupport(order = 12)
    @GetMapping("/list")
    public R<List<StoreVideo>> list() {
        log.info("StoreVideoController.list");
        return R.data(storeVideoService.list());
    }
}

