package com.fzm.mall.server.admin.goods_center.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.goods_center.entity.dto.PackageDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.GoodsPackageVO;
import com.fzm.mall.server.admin.goods_center.entity.vo.SkuPropertyVO;
import com.fzm.mall.server.admin.goods_center.mapper.GoodsPackageMapper;
import com.fzm.mall.server.admin.goods_center.model.GoodsNft;
import com.fzm.mall.server.admin.goods_center.model.GoodsPackage;
import com.fzm.mall.server.admin.goods_center.model.Sku;
import com.fzm.mall.server.admin.goods_center.service.IGoodsPackageService;
import com.fzm.mall.server.admin.goods_center.service.ISkuPropService;
import com.fzm.mall.server.admin.goods_center.service.ISkuService;
import com.fzm.mall.server.admin.goods_center.service.ISpuService;
import com.fzm.mall.server.admin.util.ObjectUtil;
import org.apache.http.impl.cookie.IgnoreSpec;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-08-30
 */
@Service
@Transactional
public class GoodsPackageServiceImpl extends ServiceImpl<GoodsPackageMapper, GoodsPackage> implements IGoodsPackageService {

    @Autowired
    private DozerBeanMapper beanMapper;
    @Autowired
    private ISkuService skuService;
    @Autowired
    private ISpuService spuService;
    @Autowired
    private ISkuPropService skuPropService;

    @Override
    public void add(PackageDTO packageDTO, String goodsId) {
        ObjectUtil.checkObjFieldsIsNull(packageDTO);
        GoodsPackage goodsPackage = beanMapper.map(packageDTO, GoodsPackage.class);
        goodsPackage.setGoodsId(goodsId);
        save(goodsPackage);

        JSONArray jsonArray = JSONArray.parseArray(packageDTO.getResource());
        jsonArray.forEach(v -> {
            Map map = JSONObject.parseObject(v.toString(), Map.class);
            String skuId = map.get("skuId").toString();
            long number = Long.parseLong(map.get("number").toString());
            Sku sku = skuService.getBySkuId(skuId);
            if (sku.getStock() - number * packageDTO.getStock() < 0) {
                throw new MyException(MallResponseError.PACKAGE_GOODS_STOCK_INSUFFICIENT);
            } else {
                sku.setStock(sku.getStock() - number * packageDTO.getStock());
                skuService.updateById(sku);
            }
        });

    }

    @Override
    public GoodsPackageVO getByGoodsId(String goodsId) {
        LambdaQueryWrapper<GoodsPackage> qw = new LambdaQueryWrapper<>();
        qw.eq(GoodsPackage::getGoodsId, goodsId);
        GoodsPackage goodsPackage = getOne(qw);

        JSONArray arrayQuery = JSONArray.parseArray(goodsPackage.getResource());
        JSONArray arrayReturn = new JSONArray();

        arrayQuery.forEach(v -> {
            Map map = JSONObject.parseObject(v.toString(), Map.class);
            String skuId = map.get("skuId").toString();
            long number = Long.parseLong(map.get("number").toString());

            StringBuilder name = new StringBuilder();
            List<SkuPropertyVO> skuPropList = skuPropService.getBySkuId(skuId);
            for (SkuPropertyVO vo : skuPropList) {
                name.append(vo.getPropKey())
                        .append(":")
                        .append(vo.getPropVal())
                        .append(",");
            }
            String subname = name.substring(0, name.length() - 1);

            Sku bySkuId = skuService.getBySkuId(skuId);
            JSONObject jsonSku = new JSONObject();
            jsonSku.put("skuId", skuId);
            jsonSku.put("name", subname);
            jsonSku.put("number", bySkuId.getStock());

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("goodsName", spuService.getByGoodsId(bySkuId.getGoodsId()).getName());
            jsonObject.put("sku", jsonSku);
            jsonObject.put("number", number);

            arrayReturn.add(jsonObject);
        });

        GoodsPackageVO vo = new GoodsPackageVO();
        vo.setPrice(goodsPackage.getPrice());
        vo.setResource(arrayReturn);
        vo.setStock(goodsPackage.getStock());
        return vo;
    }

    @Override
    public GoodsPackage getByGoodsIdInner(String goodsId) {
        LambdaQueryWrapper<GoodsPackage> qw = new LambdaQueryWrapper<>();
        qw.eq(GoodsPackage::getGoodsId, goodsId);
        GoodsPackage goodsPackage = getOne(qw);
        return goodsPackage;
    }

    @Override
    public void edit(PackageDTO packageDTO, String goodsId) {
        ObjectUtil.checkObjFieldsIsNull(packageDTO);
        LambdaQueryWrapper<GoodsPackage> qw = new LambdaQueryWrapper<>();
        qw.eq(GoodsPackage::getGoodsId, goodsId);
        GoodsPackage aPackage = getOne(qw);

        JSONArray jsonArray = JSONArray.parseArray(aPackage.getResource());
        jsonArray.forEach(v -> {
            Map map = JSONObject.parseObject(v.toString(), Map.class);
            String skuId = map.get("skuId").toString();
            long number = Long.parseLong(map.get("number").toString());
            Sku sku = skuService.getBySkuId(skuId);
            sku.setStock(sku.getStock() + number);
            skuService.updateById(sku);
        });

        LambdaUpdateWrapper<GoodsPackage> uw = new LambdaUpdateWrapper<>();
        uw.eq(GoodsPackage::getGoodsId, goodsId);
        GoodsPackage goodsPackage = beanMapper.map(packageDTO, GoodsPackage.class);
        update(goodsPackage, uw);

        JSONArray jsonArray1 = JSONArray.parseArray(packageDTO.getResource());
        jsonArray1.forEach(v -> {
            Map map = JSONObject.parseObject(v.toString(), Map.class);
            String skuId = map.get("skuId").toString();
            long number = Long.parseLong(map.get("number").toString());
            Sku sku = skuService.getBySkuId(skuId);
            sku.setStock(sku.getStock() + number);
            skuService.updateById(sku);
        });
    }
}

