package cn.ysk.cashier.service.impl.productimpl;

import cn.ysk.cashier.dto.rabbit.StockRecordMsg;
import cn.ysk.cashier.mybatis.mapper.TbProducSkutMapper;
import cn.ysk.cashier.mybatis.mapper.TbProductMapper;
import cn.ysk.cashier.pojo.product.TbProduct;
import cn.ysk.cashier.pojo.shop.TbShopPurveyor;
import cn.ysk.cashier.pojo.shop.TbShopUnit;
import cn.ysk.cashier.repository.product.TbProductRepository;
import cn.ysk.cashier.repository.product.TbProductStockDetailRepository;
import cn.ysk.cashier.repository.shop.TbShopPurveyorRepository;
import cn.ysk.cashier.repository.shop.TbShopUnitRepository;
import cn.ysk.cashier.utils.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.pojo.product.TbProductSku;
import cn.ysk.cashier.repository.product.TbProductSkuRepository;
import cn.ysk.cashier.service.product.TbProductSkuService;
import cn.ysk.cashier.pojo.product.TbProductStockDetail;
import cn.ysk.cashier.pojo.product.TbProductStockOperate;
import cn.ysk.cashier.vo.ProductStockOperateVO;
import cn.ysk.cashier.dto.product.OutAndOnDto;
import cn.ysk.cashier.dto.product.ProductListDto;
import cn.ysk.cashier.pojo.shop.TbShopInfo;
import cn.ysk.cashier.repository.shop.TbShopInfoRepository;

import cn.ysk.cashier.pojo.shop.TbShopPurveyorTransact;
import cn.ysk.cashier.repository.shop.TbShopPurveyorTransactRepository;
import com.alipay.api.domain.Product;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.product.TbProductStockOperateRepository;
import cn.ysk.cashier.service.TbProductStockOperateService;
import cn.ysk.cashier.dto.product.TbProductStockOperateDto;
import cn.ysk.cashier.dto.product.TbProductStockOperateQueryCriteria;
import cn.ysk.cashier.mapper.product.TbProductStockOperateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.*;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lyf
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-01-25
 **/
@Service
@RequiredArgsConstructor
@Slf4j
public class TbProductStockOperateServiceImpl implements TbProductStockOperateService {

    private final TbProductStockOperateRepository tbProductStockOperateRepository;
    private final TbProductStockOperateMapper tbProductStockOperateMapper;
    private final TbShopPurveyorTransactRepository purveyorTransactRepository;
    private final TbShopPurveyorRepository purveyorRepository;
    private final TbShopInfoRepository shopInfoRepository;
    private final TbProductSkuRepository tbProductSkuRepository;
    private final TbProductSkuService productSkuService;
    private final EntityManager entityManager;

    private final RedisUtils redisUtils;
    private final TbProductRepository tbProductRepository;


    @Override
    public Map<String, Object> queryAllPage(TbProductStockOperateQueryCriteria criteria) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(criteria.getPage(), criteria.getSize(), sort);


        Page<TbProductStockOperate> page = tbProductStockOperateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        List<ProductStockOperateVO> stockOperateVOList = new ArrayList<>();
        for (TbProductStockOperate stockOperate : page.getContent()) {
            ProductStockOperateVO stockOperateVO = new ProductStockOperateVO();
            BeanUtils.copyProperties(stockOperate, stockOperateVO);
            stockOperateVO.setStockSnap(ListUtil.stringChangeList(stockOperate.getStockSnap()));
            stockOperateVO.setOperatorSnap(StringUtils.stringChangeMap(stockOperate.getOperatorSnap()));
            stockOperateVO.setType();
            stockOperateVOList.add(stockOperateVO);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("content", stockOperateVOList);
        map.put("totalElements", page.getTotalElements());
        return map;
    }

    @Override
    public List<TbProductStockOperate> queryAll(TbProductStockOperateQueryCriteria criteria) {
        return tbProductStockOperateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
    }

    @Override
    public void nullify(Integer id) {
//        TbProductStockOperate tbProductStockOperate = tbProductStockOperateRepository.findById(id).orElseGet(TbProductStockOperate::new);
//        tbProductStockOperate.setStatus("nullify");
//        //作废之后恢复数量
//        tbProductStockOperate.get
//
//
//        ValidationUtil.isNull( tbProductStockOperate.getId(),"TbProductStockOperate","id",id);
//        tbProductStockOperate.copy(resources);
//        tbProductStockOperateRepository.save(tbProductStockOperate);
    }

    @Override
    @Transactional
    public ProductStockOperateVO findById(Integer id) {
        TbProductStockOperate tbProductStockOperate = tbProductStockOperateRepository.findById(id).orElseGet(TbProductStockOperate::new);
        ProductStockOperateVO stockOperateVO = new ProductStockOperateVO();
        BeanUtils.copyProperties(tbProductStockOperate, stockOperateVO);
        stockOperateVO.setStockSnap(ListUtil.stringChangeList(tbProductStockOperate.getStockSnap()));
        stockOperateVO.setOperatorSnap(StringUtils.stringChangeMap(tbProductStockOperate.getOperatorSnap()));


        ValidationUtil.isNull(tbProductStockOperate.getId(), "TbProductStockOperate", "id", id);
        return stockOperateVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbProductStockOperateDto create(TbProductStockOperate resources) {
        return tbProductStockOperateMapper.toDto(tbProductStockOperateRepository.save(resources));
    }

    /**
     * 出库
     *
     * @param resources
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OutAndOnDto createOutAndONOperate(OutAndOnDto resources) {
        if (CollectionUtils.isEmpty(resources.getList())) {
            throw new BadRequestException("出入库数据不可为空");
        }
        //用户相关
        TbShopInfo byId = shopInfoRepository.findById(Integer.valueOf(resources.getShopId())).orElseGet(TbShopInfo::new);
        if (byId.getId() == null) {
            throw new BadRequestException("商户号有误");
        }
        //增加操作日志
        long times = Instant.now().toEpochMilli();
        TbProductStockOperate stockOperate = new TbProductStockOperate();
        stockOperate.setShopId(resources.getShopId());
        stockOperate.setStockSnap(resources.getList().toString());
        stockOperate.setType(resources.getType());
        //其它入库      //purveyor 供应商入库
        if ("purchase".equals(resources.getType()) || "purveyor".equals(resources.getType())) {
            stockOperate.setSubType(1);
        } else {
            stockOperate.setSubType(-1);
        }
        Map<String, String> operatorSnapMap = new HashMap<>();
        operatorSnapMap.put("name", byId.getShopName());
        operatorSnapMap.put("account", byId.getAccount());

        stockOperate.setOperatorSnap(JSON.toJSONString(operatorSnapMap));
        stockOperate.setBatchNumber(resources.getBatchNumber());
        stockOperate.setRemark(resources.getRemark());
        stockOperate.setStockTime(times);
        stockOperate.setCreatedAt(times);
        stockOperate.setUpdatedAt(times);
        stockOperate.setStatus("normal");
        stockOperate.setPurveyorId(resources.getPurveyorId());
        if (StringUtils.isNotBlank(resources.getPurveyorId())) {
            Optional<TbShopPurveyor> byId1 = purveyorRepository.findById(Integer.valueOf(resources.getPurveyorId()));
            if (byId1.isPresent()) {
                TbShopPurveyor tbShopPurveyor = byId1.get();
                resources.setPurveyorName(tbShopPurveyor.getPurveyorName());
            }
        }
        stockOperate.setPurveyorName(resources.getPurveyorName());
        if (!resources.getIsImport().equals("true")) {
            //供应商退货 reject
            //供应商入库 purveyor
            if ("reject".equals(resources.getType()) || "purveyor".equals(resources.getType())) {
                TbShopPurveyorTransact purveyorTransact = new TbShopPurveyorTransact();
                purveyorTransact.setShopId(resources.getShopId());
                purveyorTransact.setPurveyorName(resources.getPurveyorName());
                purveyorTransact.setPurveyorId(resources.getPurveyorId());
                purveyorTransact.setRemark(resources.getRemark());
                purveyorTransact.setCreatedAt(times);
                purveyorTransact.setUpdatedAt(times);
                if("reject".equals(resources.getType())){
                    purveyorTransact.setTotalAmount(resources.getTotalAmount().negate());
                    purveyorTransact.setPaidAmount(resources.getPaidAmount());
                    purveyorTransact.setWaitAmount((resources.getTotalAmount().subtract(resources.getPaidAmount())).negate());
                }else{
                    purveyorTransact.setTotalAmount(resources.getTotalAmount());
                    purveyorTransact.setPaidAmount(resources.getPaidAmount());
                    purveyorTransact.setWaitAmount((resources.getTotalAmount().subtract(resources.getPaidAmount())));
                }
                if (resources.getTotalAmount().compareTo(resources.getPaidAmount()) <= 0) {
                    purveyorTransact.setStatus(1);
                } else {
                    purveyorTransact.setStatus(0);
                }
                purveyorTransact.setPaidAt(resources.getPaidAt());
                purveyorTransact.setType(resources.getType());
                purveyorTransactRepository.save(purveyorTransact);
                if (resources.getPurveyorId() != null) {
                    purveyorRepository.upLastTransactAt(resources.getPurveyorId());
                }
            }
            TbProductStockOperate saveStockOperate = tbProductStockOperateRepository.save(stockOperate);
            Map<Integer, Double> idStockMap = new HashMap<>();
            for (Object date : resources.getList()) {
                //商品详情
                ProductListDto productListDto = JSONObject.parseObject(JSONObject.toJSONString(date), ProductListDto.class);
                if (productListDto.getNumber().equals(0)) {
                    continue;
                }
                TbProductSku tbProductSku = tbProductSkuRepository.findById(productListDto.getId()).orElseGet(TbProductSku::new);

                TbProduct product = tbProductRepository.findById(Integer.valueOf(productListDto.getProductId())).orElseGet(TbProduct::new);
                if (product.getStockNumber() == null) {
                    product.setStockNumber(0);
                }
                if (product.getId() == null) {
                    throw new BadRequestException("商品有误");
                }

                boolean isDistribute = product.getIsDistribute() == 1;

                // 保存库存详情
                TbProductStockDetail productStockDetail = new TbProductStockDetail();
                productStockDetail.setBatchNumber(resources.getBatchNumber());
                productStockDetail.setCreatedAt(times);
                productStockDetail.setUpdatedAt(times);
                productStockDetail.setIsStock(1);
                Integer round = (int) Math.floor(isDistribute ? product.getStockNumber() : tbProductSku.getStockNumber());
                productStockDetail.setLeftNumber(round);
                productStockDetail.setProductId(tbProductSku.getProductId());
                productStockDetail.setProductName(product.getName());
                productStockDetail.setRecordId(saveStockOperate.getId().toString());
                productStockDetail.setShopId(resources.getShopId());
                productStockDetail.setSkuId(productListDto.getId().toString());
                productStockDetail.setSourcePath("NORMAL");
                productStockDetail.setSpecSnap(productListDto.getSpecSnap());
                productListDto.setNumber(productListDto.getNumber() != null ? productListDto.getNumber() : 0);
                tbProductSku.setCostPrice(productListDto.getCostPrice());
                // 总成本价
                BigDecimal totalCostPrice;
                productStockDetail.setType(resources.getType());
                switch (resources.getType()) {
                    case "sale":
                        productStockDetail.setStockNumber((double) -productListDto.getNumber());
                        productStockDetail.setSubType(-1);
                        break;
                    //后台出库
                    case "refund":
                        productStockDetail.setStockNumber((double) -productListDto.getNumber());
                        productStockDetail.setSubType(1);
                        totalCostPrice = productListDto.getCostPrice().multiply(BigDecimal.valueOf(productListDto.getNumber()));
                        productStockDetail.setCostAmount(totalCostPrice);
                        productStockDetail.setType("供应商出库");

                        // 获取增加后的库存
                        double refundStockNum = getStockNum(product, tbProductSku, productListDto.getNumber(), false);
                        setProSpecInfo(product, tbProductSku, refundStockNum,
                                productListDto.getCostPrice(), isDistribute);

                        break;
                    case "reject":
                        productStockDetail.setStockNumber((double) -productListDto.getNumber());
                        productStockDetail.setSubType(-1);
                        totalCostPrice = productListDto.getCostPrice().multiply(BigDecimal.valueOf(productListDto.getNumber()));
                        productStockDetail.setCostAmount(totalCostPrice);
                        productStockDetail.setType("供应商出库");

                        // 获取增加后的库存
                        setProSpecInfo(product, tbProductSku, getStockNum(product, tbProductSku, productListDto.getNumber(), false),
                                productListDto.getCostPrice(), isDistribute);
                        break;
                    //后台入库
                    case "purveyor":
                        productStockDetail.setStockNumber((double) productListDto.getNumber());
                        productStockDetail.setSubType(1);
                        totalCostPrice = productListDto.getCostPrice().multiply(BigDecimal.valueOf(productListDto.getNumber()));
                        productStockDetail.setCostAmount(totalCostPrice);
                        productStockDetail.setType("供应商入库");

                        // 获取增加后的库存
                        double stockNum = getStockNum(product, tbProductSku, productListDto.getNumber(), true);
                        setProSpecInfo(product, tbProductSku, stockNum,
                                productListDto.getCostPrice(), isDistribute);

                        break;
                    case "purchase":
                        productStockDetail.setStockNumber((double) productListDto.getNumber());
                        productStockDetail.setSubType(1);

                        totalCostPrice = productListDto.getCostPrice().multiply(BigDecimal.valueOf(productListDto.getNumber()));
                        productStockDetail.setCostAmount(totalCostPrice);
                        productStockDetail.setType("其他入库");

                        // 获取增加后的库存
                        setProSpecInfo(product, tbProductSku, getStockNum(product, tbProductSku, productListDto.getNumber(), true),
                                productListDto.getCostPrice(), isDistribute);
                        break;
                    case "other-out":
                        productStockDetail.setStockNumber((double) -productListDto.getNumber());
                        productStockDetail.setSubType(-1);

                        totalCostPrice = productListDto.getCostPrice().multiply(BigDecimal.valueOf(productListDto.getNumber()));
                        productStockDetail.setCostAmount(totalCostPrice);
                        productStockDetail.setType("其他出库");

                        // 获取增加后的库存
                        setProSpecInfo(product, tbProductSku, getStockNum(product, tbProductSku, productListDto.getNumber(), false),
                                productListDto.getCostPrice(), isDistribute);
                        break;
                    default:
                        break;
                }
                productStockDetail.setUnitName(productListDto.getUnitName());
                entityManager.persist(productStockDetail);

                if (isDistribute) {
                    //修改成本价
                    tbProductSkuRepository.updateCostByProductId(product.getId().toString(), productListDto.getCostPrice());
                    tbProductRepository.save(product);
                } else {
                    //sku数量
                    tbProductSku.setCostPrice(productListDto.getCostPrice());
                    productSkuService.update(tbProductSku);
                }
                idStockMap.put(productListDto.getId(), productStockDetail.getStockNumber());
            }
            redisUtils.redisUp(2, resources.getShopId(), idStockMap);
        } else {
            tbProductStockOperateRepository.save(stockOperate);
        }
        return resources;
    }

    private double getStockNum(TbProduct product, TbProductSku productSku, double num, boolean isAdd) {
        if (product.getIsDistribute() == 1) {
            product.setStockNumber((int) (isAdd ? product.getStockNumber() + num : product.getStockNumber() - num));
            return product.getStockNumber();
        } else {
            productSku.setStockNumber(isAdd ? productSku.getStockNumber() + num : productSku.getStockNumber() - num);
            return productSku.getStockNumber();
        }
    }

    public static void main(String[] args) {
    }

    private void setProSpecInfo(TbProduct product, TbProductSku productSku, double num, BigDecimal cost, boolean isShareStock) {
        if (product.getSpecInfo() != null) {
            JSONArray specInfoArr = JSON.parseArray(product.getSpecInfo());
            for (Object item : specInfoArr) {
                JSONObject jsonObject = (JSONObject) item;
                if (isShareStock) {
                    jsonObject.put("stockNumber", num);
                    jsonObject.put("costPrice", cost);
                } else if (productSku != null &&
                        (
                                productSku.getId().equals(jsonObject.getInteger("id")) ||
                                (productSku.getSpecSnap() != null && productSku.getSpecSnap().equals(jsonObject.getString("specSnap")))
                        )) {
                    jsonObject.put("stockNumber", num);
                    jsonObject.put("costPrice", cost);
                    break;
                }
            }
            product.setSpecInfo(specInfoArr.toJSONString());
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbProductStockOperate resources) {
        TbProductStockOperate tbProductStockOperate = tbProductStockOperateRepository.findById(resources.getId()).orElseGet(TbProductStockOperate::new);
        ValidationUtil.isNull(tbProductStockOperate.getId(), "TbProductStockOperate", "id", resources.getId());
        tbProductStockOperate.copy(resources);
        tbProductStockOperateRepository.save(tbProductStockOperate);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tbProductStockOperateRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TbProductStockOperate> all, HttpServletResponse response) throws IOException {

        List<Map<String, Object>> list = new ArrayList<>();
        for (TbProductStockOperate stockOperate : all) {
            ProductStockOperateVO stockOperateVO = new ProductStockOperateVO();
            BeanUtils.copyProperties(stockOperate, stockOperateVO);
            stockOperateVO.setType();
            stockOperateVO.setStockSnap(ListUtil.stringChangeList(stockOperate.getStockSnap()));
            stockOperateVO.setOperatorSnap(StringUtils.stringChangeMap(stockOperate.getOperatorSnap()));
            Map<String, Object> map = new LinkedHashMap<>();
            for (int i = 0; i < stockOperateVO.getStockSnap().size(); i++) {
                if (i == 0) {
                    map.put("操作类型 ", stockOperateVO.getType());
                    map.put("供应商名称", stockOperateVO.getPurveyorName());
                    map.put("商品数量 ", stockOperateVO.getStockSnap().size());
                    map.put(" 备  注 ", stockOperateVO.getRemark());
                    map.put("操作人 ", stockOperateVO.getOperatorSnap().get("account"));
                    map.put("创建时间", DateUtil.getStrTime(new Date(stockOperateVO.getStockTime())));
                } else {
                    map.put("操作类型 ", "");
                    map.put("供应商名称", "");
                    map.put("商品数量 ", "");
                    map.put(" 备  注 ", "");
                    map.put("操 作 人", "");
                    map.put("创建时间", "");
                }
                JSONObject jsonObject = stockOperateVO.getStockSnap().getJSONObject(0);
                map.put("商品名称", jsonObject.getString("name"));
                map.put("商品规格", jsonObject.getString("specSnap"));
                map.put("变动数量", jsonObject.getString("number"));
                list.add(map);
            }
        }
        FileUtil.downloadExcel(list, response);
    }


    private final TbProductMapper productMapper;
    private final TbProducSkutMapper producSkutMapper;
    private final TbShopUnitRepository shopUnitRepository;
    private final TbProductStockOperateRepository productStockOperateRepository;
    private final TbProductStockDetailRepository productStockDetailRepository;
    @Override
    public void addStockRecord(StockRecordMsg stockRecordMsg) {
        TbShopInfo byId = shopInfoRepository.findById(stockRecordMsg.getShopId()).orElseGet(TbShopInfo::new);
        if (byId.getId() == null) {
            throw new BadRequestException("商户号有误");
        }

        TbProduct product = productMapper.selectOne(new LambdaUpdateWrapper<TbProduct>()
                .eq(TbProduct::getShopId, stockRecordMsg.getShopId())
                .eq(TbProduct::getId, stockRecordMsg.getProductId())
                .eq(TbProduct::getIsDel, 0));

        TbProductSku productSku = producSkutMapper.selectOne(new LambdaUpdateWrapper<TbProductSku>()
                .eq(TbProductSku::getShopId, stockRecordMsg.getShopId())
                .eq(TbProductSku::getId, stockRecordMsg.getSkuId())
                .eq(TbProductSku::getIsDel, 0));
        if (product == null || productSku == null) {
            log.info("商品或sku不存在");
            throw new BadRequestException("商品或sku不存在");
        }

        TbShopUnit tbShopUnit = shopUnitRepository.findById(product.getUnitId()).orElse(null);

        //增加操作日志
        long times = Instant.now().toEpochMilli();
        HashMap<String, Object> snapItem = new HashMap<>();
        snapItem.put("stockNumber", product.getIsDistribute().equals(1) ? product.getStockNumber() - stockRecordMsg.getNumber() :
                productSku.getStockNumber() - stockRecordMsg.getNumber());
        snapItem.put("number", stockRecordMsg.getSubType() == -1 ? -stockRecordMsg.getNumber() : stockRecordMsg.getNumber());
        snapItem.put("coverImg", product.getCoverImg());
        snapItem.put("productId", product.getId());
        snapItem.put("name", product.getName());
        snapItem.put("unitName", tbShopUnit == null ? "" : tbShopUnit.getName());

        ArrayList<HashMap<String, Object>> stockSnap = new ArrayList<>();
        stockSnap.add(snapItem);

        TbProductStockOperate stockOperate = new TbProductStockOperate();
        stockOperate.setShopId(String.valueOf(stockRecordMsg.getShopId()));
        stockOperate.setStockSnap(JSONObject.toJSONString(stockSnap));
        stockOperate.setType(stockRecordMsg.getType());

        Map<String, String> operatorSnapMap = new HashMap<>();
        operatorSnapMap.put("name", byId.getShopName());
        operatorSnapMap.put("account", byId.getAccount());

        stockOperate.setOperatorSnap(JSON.toJSONString(operatorSnapMap));
        stockOperate.setStockTime(times);
        stockOperate.setCreatedAt(times);
        stockOperate.setUpdatedAt(times);
        stockOperate.setStatus("normal");
        productStockOperateRepository.save(stockOperate);


        TbProductStockDetail productStockDetail = new TbProductStockDetail();
        productStockDetail.setCreatedAt(times);
        productStockDetail.setUpdatedAt(times);
        productStockDetail.setIsStock(1);
        productStockDetail.setProductId(String.valueOf(product.getId()));
        productStockDetail.setProductName(product.getName());
        productStockDetail.setUnitName(tbShopUnit == null ? null : tbShopUnit.getName());
        productStockDetail.setShopId(String.valueOf(stockRecordMsg.getShopId()));
        productStockDetail.setSkuId(String.valueOf(stockRecordMsg.getSkuId()));
        productStockDetail.setSubType(stockRecordMsg.getSubType());
        productStockDetail.setType(stockRecordMsg.getType());
        productStockDetail.setSpecSnap(productSku.getSpecSnap());
        productStockDetail.setStockNumber(Double.valueOf(stockRecordMsg.getNumber()));
        productStockDetail.setCostAmount(productSku.getSalePrice().multiply(BigDecimal.valueOf(Math.abs(stockRecordMsg.getNumber()))));
        productStockDetail.setLeftNumber(product.getIsDistribute().equals(1) ? product.getStockNumber() - stockRecordMsg.getNumber()
                : productSku.getStockNumber().intValue() - stockRecordMsg.getNumber());
        productStockDetail.setRecordId(stockOperate.getId().toString());
        productStockDetail.setStockSnap(JSONObject.toJSONString(snapItem));
        productStockDetail.setSourcePath("NORMAL");
        productStockDetailRepository.save(productStockDetail);

    }
}
