package com.liquidnet.service.goblin.controller.manage;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.liquidnet.common.exception.constant.ErrorCode;
import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.dto.manage.GoblinStoreMgtGoodsSqbAddParam;
import com.liquidnet.service.goblin.dto.manage.GoblinStoreMgtGoodsSqbAddSkuParam;
import com.liquidnet.service.goblin.dto.manage.GoblinStoreMgtGoodsSqbEditSkuParam;
import com.liquidnet.service.goblin.dto.vo.*;
import com.liquidnet.service.goblin.param.shouqianba.response.data.MallProductsQueryData;
import com.liquidnet.service.goblin.service.manage.IGoblinStoreMgtExtraService;
import com.liquidnet.service.goblin.service.manage.IGoblinStoreMgtSqbGoodsService;
import com.liquidnet.service.goblin.util.GoblinMongoUtils;
import com.liquidnet.service.goblin.util.GoblinRedisUtils;
import com.liquidnet.service.goblin.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 收钱吧商品管理
 */
@Api(tags = "收钱吧商品管理")
@Slf4j
@Validated
@RestController
@RequestMapping("store/mgt/sqb/goods")
public class GoblinStoreMgtSqbGoodsController {

    @Autowired
    private IGoblinStoreMgtSqbGoodsService goblinstoreMgtSqbGoodsService;
    @Autowired
    private IGoblinStoreMgtExtraService goblinStoreMgtExtraService;
    @Autowired
    private GoblinRedisUtils goblinRedisUtils;
    @Autowired
    private GoblinMongoUtils goblinMongoUtils;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "SPU添加-收钱吧商品")
    @PostMapping("add")
    public ResponseDto<Object> add(@RequestParam("storeId") String storeId,
                                   @RequestBody @Validated List<GoblinSqbPerfGoodsVo> items) {
        String currentUid = CurrentUtil.getCurrentUid();
        if (!goblinRedisUtils.hasStoreId(currentUid, storeId)) return ResponseDto.failure(ErrorMapping.get("149002"));
        if (log.isDebugEnabled()) {
            log.debug("收钱吧商品管理:添加:[items={}]", JsonUtils.toJson(items));
        }
        for (GoblinSqbPerfGoodsVo sqbGoods : items) {
            GoblinStoreMgtGoodsSqbAddParam mgtGoodsSqbAddParamNew = new GoblinStoreMgtGoodsSqbAddParam();
            GoblinGoodsInfoVo goodsInfoVo = mgtGoodsSqbAddParamNew.initGoodsInfoVo(storeId, sqbGoods, currentUid, LocalDateTime.now());

            ArrayList<GoblinGoodsSpecVo> goodsSpecVoList = ObjectUtil.getGoblinGoodsSpecVoArrayList();
            List<GoblinGoodsSkuInfoVo> goodsSkuInfoVoList = ObjectUtil.getGoblinGoodsSkuInfoVoArrayList();
            mgtGoodsSqbAddParamNew.initGoodsSkuInfoVo(goodsInfoVo, goodsSkuInfoVoList, goodsSpecVoList, sqbGoods);

            goodsInfoVo.setTagVoList(new ArrayList<>());
            goodsInfoVo.setExtagVoList(new ArrayList<>());
            goodsInfoVo.setArtagVoList(new ArrayList<>());
            goodsInfoVo.setServiceSupportVoList(new ArrayList<>());
            goblinstoreMgtSqbGoodsService.sqbGoodsAdd(goodsInfoVo, goodsSkuInfoVoList);
        }
        return ResponseDto.success();
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "商品编辑:SPU编辑-收钱吧商品", notes = "只修改商品信息，不包含价格库存相关信息")
    @PutMapping("edit_spu")
    public ResponseDto<Object> editSpu(@RequestParam("storeId") String storeId,
                                       @RequestBody @Validated List<GoblinSqbPerfGoodsVo> items) {
        String currentUid = CurrentUtil.getCurrentUid();
        if (!goblinRedisUtils.hasStoreId(currentUid, storeId)) return ResponseDto.failure(ErrorMapping.get("149002"));

        if (log.isDebugEnabled()) {
            log.debug("收钱吧商品管理:商品编辑:SPU编辑:[GoblinStoreMgtGoodsSqbAddParam={}]", JsonUtils.toJson(items));
        }

        for (GoblinSqbPerfGoodsVo item : items) {
            GoblinGoodsInfoVo mgtGoodsInfoVo = goblinRedisUtils.getGoodsInfoVo(item.getSpuId());
            goblinstoreMgtSqbGoodsService.sqbGoodsEditSpu(currentUid, item, mgtGoodsInfoVo);
        }

        return ResponseDto.success();
    }

}
