package shop.alien.store.controller;

import com.alibaba.excel.util.CollectionUtils;
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.StoreImg;
import shop.alien.entity.store.vo.StoreImgInfoVo;
import shop.alien.entity.store.vo.StoreImgTypeVo;
import shop.alien.store.service.StoreImgService;
import shop.alien.store.service.StoreInfoService;

import java.util.List;

/**
 * 二期-门店图片Controller
 *
 * @author ssk
 * @since 2024-12-05
 */
@Slf4j
@Api(tags = {"二期-门店图片"})
@ApiSort(5)
@CrossOrigin
@RestController
@RequestMapping("/img")
@RequiredArgsConstructor
public class StoreImgController {
    private final StoreImgService storeImgService;
    private final StoreInfoService storeInfoService;

    @ApiOperation("获取图片")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "imgType", value = "图片类型, 0:其他, 1:入口图, 2:相册, 3:菜品, 4:环境, 5:价目表, 6:推荐菜, 7:菜单, 8:用户评论, 9:商家申诉, 10:商家头像, 11:店铺轮播图", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getByStoreId")
    public R<List<StoreImg>> getByStoreId(Integer storeId, Integer imgType) {
        log.info("StoreImgController.getByStoreId?storeId={}&imgType={}", storeId, imgType);
        return R.data(storeImgService.getStoreImg(storeId, imgType));
    }

    @ApiOperation("获取所有图片类型和数量")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getStoreImgTypeCount")
    public R<List<StoreImgTypeVo>> getStoreImgTypeCount(Integer storeId) {
        log.info("StoreImgController.getStoreImgTypeCount?storeId={}", storeId);
        return R.data(storeImgService.getStoreImgTypeCount(storeId));
    }

    @ApiOperation("新增或修改图片")
    @ApiOperationSupport(order = 3)
    @PostMapping("/saveOrUpdate")
    public R<String> saveOrUpdate(@RequestBody List<StoreImg> storeImgList) {
        log.info("StoreImgController.saveOrUpdate?storeImgList={}", storeImgList);
        Integer id = storeImgList.get(0).getId();
        if (storeImgService.saveOrUpdateBatch(storeImgList)) {
            if (null != id) {
                return R.success("修改成功");
            }
            return R.success("新增成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("新增/修改/删除图片")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdateImg")
    public R<String> saveOrUpdateImg(@RequestBody StoreImgInfoVo storeImgInfoVo) {
        log.info("StoreImgController.saveOrUpdateImg?storeImgInfoVo={}", storeImgInfoVo);
        List<StoreImg> storeImgList = storeImgInfoVo.getStoreImgList();
        if (storeImgInfoVo.getStoreId()==null || storeImgInfoVo.getImgType()==null) {
            return R.fail("失败");
        }
        // 清空storeid,imgType下图片
        int deleteCount = storeImgService.saveOrUpdateImg(storeImgInfoVo.getStoreId(),storeImgInfoVo.getImgType());
        log.info("StoreImgController.updateStoreImgModeInfo?deleteCount={}", deleteCount);
        int result = storeInfoService.updateStoreImgModeInfo(storeImgInfoVo);
        log.info("StoreImgController.updateStoreImgModeInfo?result={}", result);
        if(!CollectionUtils.isEmpty(storeImgList)){
            Integer id = storeImgList.get(0).getId();
            if (storeImgService.saveOrUpdateBatch(storeImgList)) {
                if (null != id) {
                    return R.success("修改成功");
                }
                return R.success("新增成功");
            }
        } else {
            return R.success("保存成功");
        }
        return R.fail("失败");
    }


    @ApiOperation(value = "删除图片")
    @ApiOperationSupport(order = 5)
    @PostMapping("/delete")
    public R<String> delete(@RequestBody List<Integer> ids) {
        log.info("StoreImgController.delete?ids={}", ids);
        if (storeImgService.removeByIds(ids)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("获取轮播图")
    @ApiOperationSupport(order = 6)
    @GetMapping("/getCarouselImage")
    public R<List<StoreImg>> getCarouselImage() {
        return R.data(storeImgService.getCarouselImage());
    }

    @ApiOperation("获取用户打卡广场小人图片")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getUserClockImg")
    public R<String> getUserClockImg(Integer userId) {
        log.info("StoreImgController.getUserClockImg?userId={}", userId);
        return storeImgService.getUserClockImg(userId);
    }

    @ApiOperation("通过businessId获取图片")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "imgType", value = "图片类型, 0:其他, 1:入口图, 2:相册, 3:菜品, 4:环境, 5:价目表, 6:推荐菜, 7:菜单, 8:用户评论, 9:商家申诉, 10:商家头像, 11:店铺轮播图", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "businessId", value = "业务ID", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getByBusinessId")
    public R<List<StoreImg>> getByBusinessId(Integer storeId, Integer imgType, Integer businessId) {
        log.info("StoreImgController.getByBusinessId?storeId={}&imgType={}&businessId={}", storeId, imgType, businessId);
        return R.data(storeImgService.getByBusinessId(storeId, imgType, businessId));
    }
}
