package com.cdm.module.product.controller.admin.sync;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.cdm.framework.common.pojo.CommonResult;
import com.cdm.module.product.dal.dataobject.sku.ProductSkuDO;
import com.cdm.module.product.dal.dataobject.spu.ProductSpuDO;
import com.cdm.module.product.dal.mysql.sku.ProductSkuMapper;
import com.cdm.module.product.dal.mysql.spu.ProductSpuMapper;
import com.cdm.module.product.controller.admin.spu.vo.GoodsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

import static com.cdm.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cdm.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 商品 SPU")
@RestController
@RequestMapping("/product/sync")
@Validated
public class SyncController {

    @Resource
    private ProductSpuMapper productSpuMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;

    @GetMapping("/createSKU")
    @Operation(summary = "创建商品 SKU")
    public CommonResult<Integer> createSKU() {
        List<ProductSpuDO> productSpuDOS = productSpuMapper.selectList();
        List<ProductSkuDO.Property> propertyList=new ArrayList<>();
        ProductSkuDO.Property property = new ProductSkuDO.Property();
        property.setPropertyId(0L);
        property.setPropertyName("默认");
        property.setValueId(0L);
        property.setValueName("默认");
        propertyList.add(property);
        List<ProductSkuDO> productSkuDOList=new ArrayList<>();
        for (ProductSpuDO productSpuDO : productSpuDOS) {
            ProductSkuDO productSkuDO = new ProductSkuDO();
            productSkuDO.setSpuId(productSpuDO.getId());
            productSkuDO.setProperties(propertyList);
            productSkuDO.setPrice(productSpuDO.getPrice());
            productSkuDO.setMarketPrice(productSpuDO.getMarketPrice());
            productSkuDO.setCostPrice(productSpuDO.getCostPrice());
            productSkuDO.setPicUrl(productSpuDO.getPicUrl());
            productSkuDO.setStock(productSpuDO.getStock());
            productSkuDOList.add(productSkuDO);
        }
        productSkuMapper.insertBatch(productSkuDOList);
        return success(productSkuDOList.size());
    }


    @GetMapping("/createSKU1")
    @Operation(summary = "创建商品 SKU")
    public void createSKU1() {
        List<GoodsVO> hashMapList = productSpuMapper.selectGoods();
        for (GoodsVO hashMap : hashMapList) {
            Integer id = hashMap.getId();
            ProductSpuDO productSpuDO = productSpuMapper.selectById(id);
            String gallery = hashMap.getGallery();

            JSONArray objects = JSONUtil.parseArray(gallery);
            List<Test1> userList = JSONUtil.toList(objects, Test1.class);
            List<String> stringList=new ArrayList<>();
            for (Test1 object : userList) {
                stringList.add(object.getUrl());
            }
            productSpuDO.setSliderPicUrls(stringList);
            productSpuMapper.updateById(productSpuDO);
        }
    }
    @Data
    class Test1{
        private String url;
    }

}
