package com.gxwebsoft.shop.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.GoodsSku;
import com.gxwebsoft.shop.entity.GoodsSkuVO;
import com.gxwebsoft.shop.param.GoodsSkuParam;
import com.gxwebsoft.shop.service.GoodsSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 商品sku列表控制器
 *
 * @author 科技小王子
 * @since 2024-07-26 10:49:02
 */
@Api(tags = "商品sku列表管理")
@RestController
@RequestMapping("/api/shop/goods-sku")
public class GoodsSkuController extends BaseController {
    @Resource
    private GoodsSkuService goodsSkuService;

    @ApiOperation("根据商品spec生成SKU列表")
    @PostMapping("/generateGoodsSku")
    public ApiResult<List<GoodsSku>> generateGoodsSku(@RequestBody List<GoodsSkuVO> voList) {
      List<List<String>> resultList = new ArrayList<>();
      voList.forEach(d -> {
        resultList.add(d.getDetail());
      });
      System.out.println("resultList = " + resultList);
      List<String> combine = generateSku(resultList,0,"",new ArrayList<>());

      List<GoodsSku> goodsSkuList = new ArrayList<>();
      for (String sku : combine) {
        System.out.println(sku);
        GoodsSku goodsSku = new GoodsSku();
        goodsSku.setSku(sku);
        goodsSku.setUuid(UUID.randomUUID().toString());
        goodsSku.setImages(new ArrayList<>());
        goodsSkuList.add(goodsSku);
      }

      return success(goodsSkuList);
    }

    /**
     * 根据属性值生成sku
     *
     * @param list    属性值列表
     * @param index   下标
     * @param str     初始化数据
     * @param newObjs 新obj
     */
    public static List<String> generateSku(List<List<String>> list, int index, String str, List<String> newObjs) {
      if (index == list.size()) {
        newObjs.add(str);
        return newObjs;
      }
      for (String dataStr : list.get(index)) {
        if (StringUtils.isBlank(str)) {
          generateSku(list, index + 1, str + dataStr, newObjs);
        } else {
          generateSku(list, index + 1, str + "," + dataStr, newObjs);
        }
      }
      return newObjs;
    }

    @ApiOperation("分页查询商品sku列表")
    @GetMapping("/page")
    public ApiResult<PageResult<GoodsSku>> page(GoodsSkuParam param) {
        // 使用关联查询
        return success(goodsSkuService.pageRel(param));
    }

    @ApiOperation("查询全部商品sku列表")
    @GetMapping()
    public ApiResult<List<GoodsSku>> list(GoodsSkuParam param) {
        // 使用关联查询
        return success(goodsSkuService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:goodsSku:list')")
    @ApiOperation("根据id查询商品sku列表")
    @GetMapping("/{id}")
    public ApiResult<GoodsSku> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(goodsSkuService.getByIdRel(id));
    }

    @ApiOperation("添加商品sku列表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody GoodsSku goodsSku) {
        if (goodsSkuService.save(goodsSku)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改商品sku列表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody GoodsSku goodsSku) {
        if (goodsSkuService.updateById(goodsSku)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除商品sku列表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (goodsSkuService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加商品sku列表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<GoodsSku> list) {
        if (goodsSkuService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改商品sku列表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<GoodsSku> batchParam) {
        if (batchParam.update(goodsSkuService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除商品sku列表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (goodsSkuService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
