package com.zcloud.modules.mall.controller;

import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import com.zcloud.modules.mall.dto.SortDto;
import com.zcloud.modules.mall.entity.BusBannerEntity;
import com.zcloud.modules.mall.service.BusBannerService;
import com.zcloud.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author fangjiakai
 * @Desc 轮播图(BusBanner)表控制层
 * @Date 2024-06-26 10:00:19
 */
@Api(tags = "轮播图")
@RestController
@RequestMapping("busBanner")
public class BusBannerController extends AbstractController {

    @Autowired
    private BusBannerService busBannerService;

    @ApiOperation("轮播图-新增数据")
    @SysLog("轮播图-新增数据")
    @PostMapping("/save")
    public R insert(@RequestPart BusBannerEntity param,
                    @RequestPart(value="file", required = false) MultipartFile file) {
        param.setCreator(getUserId());
		param.setOperator(getUserId());
        busBannerService.save(param,file);
        // 重新排序
        busBannerService.resort();

        return R.ok();
    }

    @ApiOperation("轮播图-编辑数据")
    @SysLog("轮播图-编辑数据")
    @PostMapping("/update")
    public R update(@RequestBody BusBannerEntity param) {
        param.setOperator(getUserId());
        param.setSort(0);
        busBannerService.updateById(param);

        // 重新排序
        busBannerService.resort();
        return R.ok();
    }


    @ApiOperation("轮播图-删除数据")
    @SysLog("轮播图-删除数据")
    @PostMapping("/delete")
    public R delete(@RequestBody BusBannerEntity param) {
        busBannerService.delete(param.getBannerId(),getUserId());
        return R.ok();
    }

    @ApiOperation("轮播图-分页查询")
    @SysLog("轮播图-分页查询")
    @PostMapping("/listPage")
    public R listPage(@RequestBody Map<String, Object> params) {
        PageUtils page = busBannerService.listPage(params);

		return R.ok().put("page", page);
    }


    @ApiOperation("轮播图-根据ID查详情")
    @SysLog("轮播图-根据ID查详情")
    @PostMapping("/info")
    public R info(@RequestBody BusBannerEntity param) {
        BusBannerEntity busBanner = busBannerService.getById(param.getBannerId());
        return R.ok().put("busBanner", busBanner);
    }

    @ApiOperation("轮播图-获取所有")
    @SysLog("轮播图-获取所有")
    @PostMapping("/listAll")
    public R listAll() {
        List<BusBannerEntity> list = busBannerService.queryListByFlexibleParams("is_show",1)
                .stream().sorted(Comparator.comparing(BusBannerEntity::getSort)).collect(Collectors.toList());
        return R.ok().put("list", list);
    }
    @ApiOperation("轮播图-变更排序")
    @SysLog("轮播图-变更排序")
    @PostMapping("/sort")
    public R sort(@RequestBody SortDto dto) {
        busBannerService.sort(dto.getEntityList());
        return R.ok();
    }
}
