package com.wansenai.api.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wansenai.entities.shop.BasShop;
import com.wansenai.service.shop.BasShopService;
import com.wansenai.utils.response.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * <p>商店档案</p>
 * @author clunt
 */
@Tag(name = "店铺管理接口")
@RestController
@RequestMapping("basShop")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopController {

    private final BasShopService shopService;

    @Operation(summary = "获取店铺列表")
    @GetMapping("/list")
    public Response<IPage<BasShop>> getShopList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "店铺名称") @RequestParam(required = false) String name,
            @Parameter(description = "店铺编码") @RequestParam(required = false) String code,
            @Parameter(description = "渠道") @RequestParam(required = false) String channel) {
        
        Page<BasShop> pageRequest = new Page<>(page, pageSize);
        LambdaQueryWrapper<BasShop> queryWrapper = new LambdaQueryWrapper<BasShop>()
                .like(name != null, BasShop::getName, name)
                .eq(code != null, BasShop::getCode, code)
                .eq(channel != null, BasShop::getChannel, channel);
        
        return Response.responseData(shopService.page(pageRequest, queryWrapper));
    }

    @Operation(summary = "根据ID获取店铺信息")
    @GetMapping("/{id}")
    public Response<BasShop> getShopById(@Parameter(description = "店铺ID") @PathVariable Long id) {
        return Response.responseData(shopService.getById(id));
    }

    @Operation(summary = "根据编码获取店铺信息")
    @GetMapping("/code/{code}")
    public Response<BasShop> getShopByCode(@Parameter(description = "店铺编码") @PathVariable String code) {
        return Response.responseData(shopService.getShopByCode(code));
    }

    @Operation(summary = "根据名称搜索店铺")
    @GetMapping("/search")
    public Response<List<BasShop>> searchShopByName(@Parameter(description = "店铺名称") @RequestParam String name) {
        return Response.responseData(shopService.getShopListByName(name));
    }

    @Operation(summary = "添加店铺")
    @PostMapping("/add")
    public Response<Boolean> addShop(@RequestBody BasShop shop) {
        return Response.responseData(shopService.addShop(shop));
    }

    @Operation(summary = "批量添加店铺")
    @PostMapping("/batch")
    public Response<Boolean> batchAddShop(@RequestBody List<BasShop> shopList) {
        return Response.responseData(shopService.batchAddShop(shopList));
    }

    @Operation(summary = "更新店铺信息")
    @PutMapping("/update")
    public Response<Boolean> updateShop(@RequestBody BasShop shop) {
        return Response.responseData(shopService.updateShop(shop));
    }

    @Operation(summary = "批量删除店铺")
    @DeleteMapping("/delete")
    public Response<Boolean> deleteShop(@Parameter(description = "店铺ID数组") @RequestParam("ids") List<Long> ids) {
        return Response.responseData(shopService.removeByIds(ids));
    }

    @Operation(summary = "根据渠道获取店铺列表")
    @GetMapping("/channel/{channel}")
    public Response<List<BasShop>> getShopListByChannel(@Parameter(description = "渠道") @PathVariable String channel) {
        return Response.responseData(shopService.getShopListByChannel(channel));
    }
}
