package com.hys.app.controller.erp.use;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.hys.app.framework.database.WebPage;
import com.hys.app.model.erp.dos.GoodsSnDO;
import com.hys.app.model.erp.dto.EnterpriseDTO;
import com.hys.app.model.erp.dto.EnterpriseQueryParams;
import com.hys.app.model.erp.vo.*;
import com.hys.app.service.erp.EnterpriseManager;
import com.hys.app.service.erp.ScanCodeManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.common.collect.HppcMaps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 企业 API
 *
 * @author 张崧
 * 2024-03-25 15:44:10
 */
@RestController
@RequestMapping("/admin/erp/scancode")
@Api(tags = "扫条形码")
@Validated
public class ScanCodeController {

    @Autowired
    private ScanCodeManager scanCodeManager;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "扫描订单")
    @GetMapping("/{orderCode}")
    public Map<String, Object> getListByScan(@PathVariable(value = "orderCode") String orderCode) {
        return scanCodeManager.getListByScan(orderCode);
    }

    @ApiOperation(value = "扫描sku")
    @GetMapping("getProduct/{warehouseId}/{productCode}")
    public ProductStockVO getProduct(@PathVariable(value = "productCode") String productCode, @PathVariable(value = "warehouseId") Long warehouseId) {
        return scanCodeManager.getProduct( productCode, warehouseId);
    }

    @ApiOperation(value = "扫描序列号")
    @GetMapping("getsnCode/{type}/{sku}/{snCode}")
    public GoodsSnVO getSnCode(@PathVariable(value = "sku") String sku, @PathVariable(value = "snCode") String snCode, @PathVariable(value = "type") String type) {
        return scanCodeManager.getSnCode( sku, snCode, type);
    }

    @ApiOperation(value = "序列号缓存")
    @PostMapping("skuRedis")
    public Object skuRedis(@RequestBody SkuRedisDO skuRedisDO) {
        if (skuRedisDO.getType() != null && skuRedisDO.getType().equals("enter")){
            return redisTemplate.opsForHash().entries(skuRedisDO.getSendOrderId());
        }
        Object o = redisTemplate.opsForHash().get(skuRedisDO.getSendOrderId(), skuRedisDO.getItemId());
        List<GoodsSnDO> val =(List<GoodsSnDO>) o;
        if (CollUtil.isEmpty(val)){
            List<Object> list = new ArrayList<>();
            list.add(skuRedisDO.getSku());
            redisTemplate.opsForHash().put(skuRedisDO.getSendOrderId(),skuRedisDO.getItemId(),list);
        }else {
            val.add(skuRedisDO.getSku());
            redisTemplate.opsForHash().put(skuRedisDO.getSendOrderId(),skuRedisDO.getItemId(),val);
        }
        Map entries = redisTemplate.opsForHash().entries(skuRedisDO.getSendOrderId());
        return entries;
    }

}

