package com.slodon.b2b2c.controller.goods.seller;

import com.alibaba.fastjson.JSONObject;
import com.slodon.b2b2c.aop.VendorLogger;
import com.slodon.b2b2c.core.constant.*;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.enums.SeoTypeConstant;
import com.slodon.b2b2c.goods.dto.GoodsPublishFrontParamDTO;
import com.slodon.b2b2c.goods.dto.GoodsPublishInsertDTO;
import com.slodon.b2b2c.goods.example.*;
import com.slodon.b2b2c.goods.pojo.*;
import com.slodon.b2b2c.member.example.GoodsAnnexExample;
import com.slodon.b2b2c.model.goods.*;
import com.slodon.b2b2c.model.seller.*;
import com.slodon.b2b2c.seller.example.StoreInnerLabelExample;
import com.slodon.b2b2c.seller.example.StoreLabelBindGoodsExample;
import com.slodon.b2b2c.seller.example.StoreOpenAccountExample;
import com.slodon.b2b2c.seller.pojo.*;
import com.slodon.b2b2c.system.pojo.SysSeo;
import com.slodon.b2b2c.vo.goods.GoodsVO;
import com.slodon.b2b2c.vo.goods.ProductVO;
import com.slodon.b2b2c.vo.goods.RecommendGoodsVO;
import com.slodon.b2b2c.vo.goods.SellerGoodsDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.spatial3d.Geo3DDocValuesField;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author slodon
 */
@Slf4j
@Api(tags = "seller-商品管理")
@RestController
@RequestMapping("v3/goods/seller/goods")
public class GoodsSellerController extends BaseController {

    @Resource
    private GoodsModel goodsModel;
    @Resource
    private GoodsExtendModel goodsExtendModel;
    @Resource
    private GoodsSellerModel goodsSellerModel;
    @Resource
    private GoodsBindLabelModel goodsBindLabelModel;
    @Resource
    private ProductModel productModel;
    @Resource
    private GoodsPictureModel goodsPictureModel;
    @Resource
    private GoodsBindReserveNameModel goodsBindReserveNameModel;
    @Resource
    private StoreInnerLabelModel storeInnerLabelModel;
    @Resource
    private StoreLabelBindGoodsModel storeLabelBindGoodsModel;
    @Resource
    private StoreModel storeModel;
    @Resource
    private StoreGradeModel storeGradeModel;
    @Resource
    private RedisTemplate<String, Object> objectRedisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private StoreOpenAccountModel storeOpenAccountModel;
    @Resource
    private GoodsAnnexModel goodsAnnexModel;


    @ApiOperation("获取商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName", value = "商品名称", paramType = "query"),
            @ApiImplicitParam(name = "StoreCategoryId", value = "店铺分类Id", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态:2-待审核商品；3-在售商品；4-仓库中商品；6-违规下架商品", paramType = "query"),
            @ApiImplicitParam(name = "auditState", value = "审核状态：2-待审核；4-审核拒绝(待审核商品查询使用)", paramType = "query"),
            @ApiImplicitParam(name = "isVirtualGoods", value = "是否是虚拟商品：1-实物商品；2-虚拟商品", paramType = "query"),
            @ApiImplicitParam(name = "goodsCode", value = "商品货号", paramType = "query"),
            @ApiImplicitParam(name = "barCode", value = "商品条形码", paramType = "query"),
            @ApiImplicitParam(name = "webSite", value = "发布渠道", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<GoodsVO>> getList(HttpServletRequest request, String goodsName, Integer StoreCategoryId,
                                               Date startTime, Date endTime, Integer state, Integer auditState,
                                               Integer isVirtualGoods, String goodsCode, String barCode,@RequestParam(value = "webSite", required = false, defaultValue = "1") String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        GoodsExample example = new GoodsExample();
        example.setStoreId(vendor.getStoreId());
        example.setGoodsNameLike(goodsName);
        example.setWebSite(webSite);
        if (!StringUtil.isNullOrZero(StoreCategoryId)) {
            StoreInnerLabel storeInnerLabel = storeInnerLabelModel.getStoreInnerLabelByInnerLabelId(StoreCategoryId);
            AssertUtil.notNull(storeInnerLabel, "店铺内部分类不存在");
            StringBuilder goodsIds = new StringBuilder();

            StoreLabelBindGoodsExample storeLabelBindGoodsExample = new StoreLabelBindGoodsExample();
            //判断是一级还是二级
            if (storeInnerLabel.getParentInnerLabelId() == 0) {
                //当此id的父id为0时判断其下是否有子id
                StoreInnerLabelExample storeExample = new StoreInnerLabelExample();
                storeExample.setParentInnerLabelId(StoreCategoryId);
                List<StoreInnerLabel> storeInnerLabelList = storeInnerLabelModel.getStoreInnerLabelList(storeExample,null);
                if(CollectionUtils.isEmpty(storeInnerLabelList)){
                    storeLabelBindGoodsExample.setInnerLabelId(StoreCategoryId);
                }else{
                    storeLabelBindGoodsExample.setParentInnerLabelId(StoreCategoryId);
                }

            } else {
                storeLabelBindGoodsExample.setInnerLabelId(StoreCategoryId);
            }
            List<StoreLabelBindGoods> storeLabelBindGoodsList = storeLabelBindGoodsModel.getStoreLabelBindGoodsList(storeLabelBindGoodsExample, null);
            if (!CollectionUtils.isEmpty(storeLabelBindGoodsList)) {
                for (StoreLabelBindGoods storeLabelBindGoods : storeLabelBindGoodsList) {
                    goodsIds.append(",").append(storeLabelBindGoods.getGoodsId());
                }
                example.setGoodsIdIn(goodsIds.substring(1));
            } else {
                //任意一个不存在的值
                example.setGoodsIdIn("-1");
            }
        }
        example.setCreateTimeAfter(startTime);
        example.setCreateTimeBefore(endTime);
        example.setIsVirtualGoods(isVirtualGoods);
        example.setProductCodeLike(goodsCode);
        example.setBarCodeLike(barCode);
        if (!StringUtil.isNullOrZero(state)) {
            if (state == GoodsConst.GOODS_STATE_2) {
                if (!StringUtil.isNullOrZero(auditState)) {
                    if (auditState == GoodsConst.GOODS_STATE_2) {
                        example.setStateIn(GoodsConst.GOODS_STATE_SELL_NOW_TO_AUDIT + ", " + GoodsConst.GOODS_STATE_WAREHOUSE_TO_AUDIT);
                    } else {
                        example.setState(GoodsConst.GOODS_STATE_REJECT);
                    }
                } else {
                    example.setStateIn(GoodsConst.GOODS_STATE_SELL_NOW_TO_AUDIT + ", " + GoodsConst.GOODS_STATE_WAREHOUSE_TO_AUDIT + ", " + GoodsConst.GOODS_STATE_REJECT);
                }
            } else if (state == GoodsConst.GOODS_STATE_4) {
                example.setStateIn(GoodsConst.GOODS_STATE_WAREHOUSE_NO_AUDIT + ", " + GoodsConst.GOODS_STATE_WAREHOUSE_AUDIT_PASS + ", " + GoodsConst.GOODS_STATE_LOWER_BY_STORE);
            } else {
                example.setState(state);
            }
        } else {
            //默认显示在售商品
            example.setState(GoodsConst.GOODS_STATE_UPPER);
        }
        example.setIsDelete(GoodsConst.GOODS_IS_DELETE_NO);
        List<Goods> goodsList = goodsModel.getGoodsList(example, pager);
        //查找goods对应的goodsExtend 构造VO
        List<GoodsVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(goodsList)) {
            goodsList.forEach(goods -> {
                //是否达到预警值，true==是
                boolean warning = false;
                GoodsExtend goodsExtend = goodsExtendModel.getGoodsExtendByGoodsId(goods.getGoodsId());
                GoodsVO vo = new GoodsVO(goods, goodsExtend);
                //查询货品列表
                ProductExample productExample = new ProductExample();
                productExample.setGoodsId(goods.getGoodsId());
                productExample.setStateIn(GoodsConst.PRODUCT_STATE_1 + "," + GoodsConst.PRODUCT_STATE_3);
                List<Product> productList = productModel.getProductList(productExample, null);
                AssertUtil.notEmpty(productList, "商品不存在");
                List<ProductVO> productVOS = new ArrayList<>();
                for (Product product : productList) {
                    productVOS.add(new ProductVO(product));
                    if (product.getProductStock().compareTo(product.getProductStockWarning()) <= 0) {
                        warning = true;
                    }
                }
                vo.setProductList(productVOS);
                vo.setWarning(warning);

                //查询店铺内部分类
                StoreLabelBindGoodsExample labelBindGoodsExample = new StoreLabelBindGoodsExample();
                labelBindGoodsExample.setGoodsId(goods.getGoodsId());
                List<StoreLabelBindGoods> labelBindGoodsList = storeLabelBindGoodsModel.getStoreLabelBindGoodsList(labelBindGoodsExample, null);
                List<String> innerLabelList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(labelBindGoodsList)) {
                    labelBindGoodsList.forEach(storeLabelBindGoods -> {
                        innerLabelList.add(storeLabelBindGoods.getInnerLabelPath());
                    });
                }
                vo.setInnerLabelList(innerLabelList);
                vos.add(vo);
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("获取商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品Id", paramType = "query", required = true),
    })
    @GetMapping("getGoodsInfo")
    public JsonResult<SellerGoodsDetailVO> getGoodsInfo(HttpServletRequest request,
                                                        @RequestParam(value = "goodsId") Long goodsId) {
        Goods goods = goodsModel.getGoodsByGoodsId(goodsId);

        SysSeo seoInfo = storeModel.getRecordByTargetIdAndType(goodsId,SeoTypeConstant.SEO_TYPE_GOODS);
        if(seoInfo !=null){
            goods.setSeoTitle(seoInfo.getTitle());
            goods.setSeoDesc(seoInfo.getRemark());
            goods.setSeoKeyWords(seoInfo.getKeywords());
        }

        if (goods == null) {
            throw new MallException("商品不存在，请重试！");
        }
        GoodsExtend goodsExtend = goodsExtendModel.getGoodsExtendByGoodsId(goodsId);
        if (goodsExtend == null) {
            throw new MallException("商品不存在，请重试！");
        }
        //商品绑定标签
        GoodsBindLabelExample goodsBindLabelExample = new GoodsBindLabelExample();
        goodsBindLabelExample.setGoodsId(goodsId);
        goodsBindLabelExample.setOrderBy("bind_id asc");
        List<GoodsBindLabel> goodsBindLabelList = goodsBindLabelModel.getGoodsBindLabelList(goodsBindLabelExample, null);

        //商品绑定店铺内部分类
        StoreLabelBindGoodsExample storeLabelBindGoodsExample = new StoreLabelBindGoodsExample();
        storeLabelBindGoodsExample.setGoodsId(goodsId);
        storeLabelBindGoodsExample.setOrderBy("bind_id asc");
        List<StoreLabelBindGoods> storeLabelBindGoodsList = storeLabelBindGoodsModel.getStoreLabelBindGoodsList(storeLabelBindGoodsExample, null);

        //不是删除状态下的货品
        ProductExample productExample = new ProductExample();
        productExample.setGoodsId(goodsId);
        productExample.setStateNotEquals(GoodsConst.PRODUCT_STATE_0);
        productExample.setOrderBy("product_id asc");
        List<Product> productList = productModel.getProductList(productExample, null);

        //查询商品图片
        GoodsPictureExample goodsPictureExample = new GoodsPictureExample();
        goodsPictureExample.setGoodsId(goodsId);
        goodsPictureExample.setOrderBy("picture_id asc");
        List<GoodsPicture> goodsPictureList = goodsPictureModel.getGoodsPictureList(goodsPictureExample, null);

        //查询商品说明(上传的文件)
        GoodsAnnexExample goodsAnnexExample = new GoodsAnnexExample();
        goodsAnnexExample.createCriteria().andGoodsIdEqualTo(goodsId).andStatusEqualTo(GoodsAnnexStatusConst.IS_STATUS);
        //只查状态为1的数据
//        goodsAnnexExample.createCriteria().andStatusEqualTo(GoodsAnnexStatusConst.IS_STATUS);
        List<GoodsAnnex> goodsAnnexList = goodsAnnexModel.goodsAnnexList(goodsAnnexExample);


        SellerGoodsDetailVO sellerGoodsDetailVO = new SellerGoodsDetailVO(goods, goodsExtend, goodsBindLabelList,
                storeLabelBindGoodsList, goodsPictureList, productList,goodsAnnexList);
        //查询虚拟商品用户预留信息
        GoodsBindReserveNameExample example = new GoodsBindReserveNameExample();
        example.setGoodsId(goodsId);
        example.setOrderBy("reserve_name_id asc");
        List<GoodsBindReserveName> reserveNameList = goodsBindReserveNameModel.getGoodsBindReserveNameList(example, null);
        sellerGoodsDetailVO.setReserveList(reserveNameList);
        return SldResponse.success(sellerGoodsDetailVO);
    }

    @ApiOperation("商品上架")
    @VendorLogger(option = "商品上架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品Id集合，用逗号隔开", required = true, paramType = "query")
    })
    @PostMapping("upperShelf")
    public JsonResult<Integer> upperShelfGoods(HttpServletRequest request, String goodsIds) {
        // 验证参数是否为空
        String logMsg = "上架商品Id" + goodsIds;
        AssertUtil.notEmpty(goodsIds, "上架商品ID不能为空,请重试");
        AssertUtil.notFormatFrontIds(goodsIds, "goodsIds格式错误,请重试");
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        int number = goodsSellerModel.upperShelfGoods(vendor.getStoreId(), goodsIds);
        return SldResponse.success(number + "件商品上架成功", logMsg);
    }

    @ApiOperation("违规下架商品 重新上架")
    @VendorLogger(option = "违规下架商品 重新上架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品Id集合，用逗号隔开", required = true, paramType = "query")
    })
    @PostMapping("illegalUpperShelf")
    public JsonResult<Integer> illegalUpperShelf(HttpServletRequest request, String goodsIds) {
        // 验证参数是否为空
        String logMsg = "上架商品Id" + goodsIds;
        AssertUtil.notEmpty(goodsIds, "上架商品ID不能为空,请重试");
        AssertUtil.notFormatFrontIds(goodsIds, "goodsIds格式错误,请重试");
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        int number = goodsSellerModel.illegalUpperShelf(vendor.getStoreId(), goodsIds);
        return SldResponse.success(number + "件商品上架成功", logMsg);
    }

    @ApiOperation("商品下架")
    @VendorLogger(option = "商品下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品Id集合，用逗号隔开", required = true, paramType = "query")
    })
    @PostMapping("lockup")
    public JsonResult<Integer> lockupGoods(HttpServletRequest request, String goodsIds) {
        // 验证参数是否为空
        AssertUtil.notEmpty(goodsIds, "下架商品ID不能为空,请重试");
        AssertUtil.notFormatFrontIds(goodsIds, "goodsIds格式错误,请重试");
        //校验商品锁定状态
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setGoodsIdIn(goodsIds);
        List<Goods> goodsList = goodsModel.getGoodsList(goodsExample, null);
        goodsList.forEach(goods -> {
            AssertUtil.isTrue(goods.getIsLock() == GoodsConst.IS_LOCK_YES,
                    "商品名称为：" + goods.getGoodsName() + "的商品已锁定，不能下架");
        });

        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        int number = goodsSellerModel.lockupGoods(vendor.getStoreId(), goodsIds);

        //删除商品缓存信息
        for (String goodsId : goodsIds.split(",")) {
            objectRedisTemplate.opsForHash().delete(RedisConst.GOODS_DETAIL, String.valueOf(goodsId));
        }
        return SldResponse.success(number + "件商品下架成功", "下架店铺id" + vendor.getStoreId());
    }

    @ApiOperation("商品删除")
    @VendorLogger(option = "商品删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品Id集合，用逗号隔开", required = true, paramType = "query")
    })
    @PostMapping("deleteGoods")
    public JsonResult<Integer> deleteGoods(HttpServletRequest request, String goodsIds) {
        // 验证参数是否为空
        String logMsg = "删除商品Id" + goodsIds;
        AssertUtil.notEmpty(goodsIds, "删除商品ID不能为空,请重试");
        AssertUtil.notFormatFrontIds(goodsIds, "goodsIds格式错误,请重试");

        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        //校验商品锁定状态
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setGoodsIdIn(goodsIds);
        List<Goods> goodsList = goodsModel.getGoodsList(goodsExample, null);
        goodsList.forEach(goods -> {
            AssertUtil.isTrue(!goods.getStoreId().equals(vendor.getStoreId()), "非法操作");
            AssertUtil.isTrue(goods.getIsLock() == GoodsConst.IS_LOCK_YES,
                    "商品名称为：" + goods.getGoodsName() + "的商品已锁定，不能下架");
        });

        int number = goodsSellerModel.deleteGoods(vendor.getStoreId(), goodsIds);
        return SldResponse.success(number + "件商品删除成功", logMsg);
    }

    @ApiOperation("是否推荐")
    @VendorLogger(option = "是否推荐")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品Id集合，用逗号隔开", required = true, paramType = "query"),
            @ApiImplicitParam(name = "isRecommend", value = "是否推荐:1-设置推荐；0-取消推荐", required = true, paramType = "query")
    })
    @PostMapping("isRecommend")
    public JsonResult<Integer> isRecommend(HttpServletRequest request, String goodsIds, Integer isRecommend) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        AssertUtil.notEmpty(goodsIds, "商品ID不能为空,请重试");
        AssertUtil.notFormatFrontIds(goodsIds, "goodsIds格式错误,请重试");
        Store storeDb = storeModel.getStoreByStoreId(vendor.getStoreId());
        AssertUtil.notNull(storeDb, "店铺信息为空");
        //自营店铺不用判断
        if (storeDb.getIsOwnStore().equals(StoreConst.NO_OWN_STORE)) {
            //如果是推荐商品，必须先判断是否还能继续推荐
            if (isRecommend == GoodsConst.IS_RECOMMEND_YES) {
                //查询传入的商品是未推荐的商品数
                GoodsExample goodsExample = new GoodsExample();
                goodsExample.setGoodsIdIn(goodsIds);
                goodsExample.setIsRecommend(GoodsConst.IS_RECOMMEND_NO);
                List<Goods> goodsList = goodsModel.getGoodsList(goodsExample, null);
                //查询该店铺已推荐的商品数
                GoodsExample goodsRecommendExample = new GoodsExample();
                goodsRecommendExample.setStoreId(vendor.getStoreId());
                goodsRecommendExample.setStateNotEquals(GoodsConst.GOODS_STATE_DELETE);
                goodsRecommendExample.setStoreIsRecommend(GoodsConst.IS_RECOMMEND_YES);
                List<Goods> goodsRecommends = goodsModel.getGoodsList(goodsRecommendExample, null);
                StoreGrade storeGradeDb = storeGradeModel.getStoreGradeByGradeId(storeDb.getStoreGradeId());
                AssertUtil.isTrue((goodsList.size() + goodsRecommends.size()) > storeGradeDb.getRecommendLimit(), "超出店铺等级限制推荐数");
            }
        }
        //修改商品店铺是否推荐
        GoodsExample example = new GoodsExample();
        example.setGoodsIdIn(goodsIds);
        Goods goods = new Goods();
        goods.setStoreIsRecommend(isRecommend);
        goods.setUpdateTime(new Date());
        goodsModel.updateGoodsByExample(goods, example);
        return SldResponse.success("设置成功");
    }

    @ApiOperation("设置关联版式")
    @VendorLogger(option = "设置关联版式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品Id集合，用逗号隔开", required = true, paramType = "query"),
            @ApiImplicitParam(name = "topTemplateId", value = "顶部关联模版ID", paramType = "query"),
            @ApiImplicitParam(name = "bottomTemplateId", value = "底部关联模版ID", paramType = "query")
    })
    @PostMapping("setRelatedTemplate")
    public JsonResult<Integer> setRelatedTemplate(HttpServletRequest request, String goodsIds, Integer topTemplateId, Integer bottomTemplateId) {
        AssertUtil.notEmpty(goodsIds, "商品ID不能为空,请重试");
        AssertUtil.notFormatFrontIds(goodsIds, "goodsIds格式错误,请重试");
        GoodsExtendExample example = new GoodsExtendExample();
        example.setGoodsIdIn(goodsIds);
        GoodsExtend goodsExtend = new GoodsExtend();
        goodsExtend.setRelatedTemplateIdTop(topTemplateId);
        goodsExtend.setRelatedTemplateIdBottom(bottomTemplateId);
        goodsExtendModel.updateGoodsExtendByExample(goodsExtend, example);

        //删除商品缓存信息
        for (String goodsId : goodsIds.split(",")) {
            objectRedisTemplate.opsForHash().delete(RedisConst.GOODS_DETAIL, String.valueOf(goodsId));
        }
        return SldResponse.success("设置成功");
    }

    @ApiOperation("发布商品")
    @VendorLogger(option = "发布商品")
    @PostMapping("add")
    public JsonResult<Integer> addGoods(HttpServletRequest request, @RequestBody GoodsPublishFrontParamDTO paramDTO) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        //校验是否可以发布商品
        if (!checkIsCanPublish(vendor.getStoreId(), paramDTO.getWebSite())) {
            return SldResponse.fail("发布商品数量不允许超过店铺等级限制的数量");
        }

        String logMsg = "发布商品名称" + paramDTO.getGoodsName();
        //参数校验
        this.publishCheck(paramDTO);
        //构造入库dto
        GoodsPublishInsertDTO insertDTO = new GoodsPublishInsertDTO(paramDTO);

        if(insertDTO.getGoodsName().contains("&amp;")){
            insertDTO.setGoodsName(insertDTO.getGoodsName().replace("&amp;","&"));
        }
        Goods goods = goodsSellerModel.saveGoods(vendor, insertDTO);
        productModel.upsertProduct(goods, vendor);
        return SldResponse.success("发布商品成功", logMsg);
    }

    @ApiOperation("编辑商品")
    @VendorLogger(option = "编辑商品")
    @PostMapping("edit")
    public JsonResult<Integer> editGoods(HttpServletRequest request, @RequestBody GoodsPublishFrontParamDTO paramDTO) {
        AssertUtil.notNullOrZero(paramDTO.getGoodsId(), "请选择要编辑的商品");
//        String objStr = JSONObject.toJSONString(paramDTO);
//        System.out.println(objStr);
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        //查询数据库中的商品
        Goods goodsDb = goodsModel.getGoodsByGoodsId(paramDTO.getGoodsId());

        AssertUtil.notNull(goodsDb, "商品不存在");
        AssertUtil.isTrue(!goodsDb.getStoreId().equals(vendor.getStoreId()), "非法操作");
        AssertUtil.isTrue(goodsDb.getIsLock() == GoodsConst.IS_LOCK_YES, "商品已锁定，不能编辑");

        this.publishCheck(paramDTO);

        //构造入库dto
        GoodsPublishInsertDTO insertDTO = new GoodsPublishInsertDTO(paramDTO);
        if(insertDTO.getGoodsName().contains("&amp;")){
            insertDTO.setGoodsName(insertDTO.getGoodsName().replace("&amp;","&"));
        }
        Goods goods = goodsSellerModel.editGoods(vendor, insertDTO, goodsDb);
        productModel.upsertProduct(goods, vendor);
        return SldResponse.success("编辑商品成功", "商品名称" + paramDTO.getGoodsName());
    }

    /**
     * 发布编辑商品参数校验
     *
     * @param paramDTO 前端参数
     */
    private void publishCheck(GoodsPublishFrontParamDTO paramDTO) {
        paramDTO.setFreightFee(BigDecimal.ZERO);
        AssertUtil.notEmpty(paramDTO.getGoodsName(), "请填写商品名称");
        AssertUtil.notNullOrZero(paramDTO.getCategoryId3(), "请选择商品分类");
        if (paramDTO.getIsVirtualGoods() == GoodsConst.IS_VIRTUAL_GOODS_1) {
            AssertUtil.isTrue(paramDTO.getFreightFee() == null
                    && StringUtil.isNullOrZero(paramDTO.getFreightId()), "请设置统一运费或选择运费模板");
        } else {
            AssertUtil.isTrue(StringUtils.isEmpty(paramDTO.getAfterSaleService()), "请选择售后服务");
        }

        //多规格校验
        if (!CollectionUtils.isEmpty(paramDTO.getSpecInfoList())) {
            paramDTO.getSpecInfoList().forEach(specInfo -> {
                AssertUtil.notEmpty(specInfo.getSpecValueList(), "规格：" + specInfo.getSpecName() + "未添加规格值");
                if (specInfo.getIsMainSpec() == GoodsConst.IS_MAIN_SPEC_YES) {
                    //主规格，规格值图片必传
                    specInfo.getSpecValueList().forEach(specValueInfo -> {
                        AssertUtil.notEmpty(specValueInfo.getImageList(), "请上传主规格图片");
                    });
                }
            });
        }
    }

    @ApiOperation("获取聊天店铺商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("storeGoods")
    public JsonResult<PageVO<RecommendGoodsVO>> sellerGoods(HttpServletRequest request) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        // 店铺推荐商品
        GoodsExample example = new GoodsExample();
        example.setStoreId(vendor.getStoreId());
        example.setStoreIsRecommend(GoodsConst.IS_RECOMMEND_YES);
        example.setState(GoodsConst.GOODS_STATE_UPPER);
        example.setStoreState(GoodsConst.STORE_STATE_1);
        List<Goods> list = goodsModel.getGoodsList(example, pager);
        List<RecommendGoodsVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(goods -> {
                vos.add(new RecommendGoodsVO(goods));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("判断入驻店铺发布商品数是否超过等级限制的发布数")
    @GetMapping("enablePublish")
    public JsonResult<Integer> enablePublish(HttpServletRequest request,String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        if ("1".equals(stringRedisTemplate.opsForValue().get("zjpay_is_enable"))) {
            //启用中金
            if (vendor.getStore().getIsOwnStore().equals(StoreConst.NO_OWN_STORE)) {
                //入驻店铺，检测是否中金开户
                StoreOpenAccountExample storeOpenAccountExample = new StoreOpenAccountExample();
                storeOpenAccountExample.setStoreId(vendor.getStoreId());
                List<StoreOpenAccount> storeOpenAccountList = storeOpenAccountModel.getStoreOpenAccountList(storeOpenAccountExample, null);
                if (CollectionUtils.isEmpty(storeOpenAccountList)) {
                    //未开户
                    return SldResponse.fail(270, "未开户不可发布商品");
                }
                if (!storeOpenAccountList.get(0).getState().equals(StoreOpenAccountConst.STATE_30)) {
                    //开户不成功
                    return SldResponse.fail(270, "未开户签约成功不可发布商品");
                }
            }
        }
        if (checkIsCanPublish(vendor.getStoreId(),webSite)) {
            return SldResponse.success();
        } else {
            return SldResponse.fail("发布商品数量不允许超过店铺等级限制的数量");
        }
    }

    /**
     * 检测是否可以发布商品
     *
     * @param storeId 店铺id
     * @return 是否可发布商品
     */
    public boolean checkIsCanPublish(Long storeId,String webSite) {
        boolean isCanPublish = true;
        //获取店铺信息
        Store storeDb = storeModel.getStoreByStoreId(storeId);
        AssertUtil.notNull(storeDb, "未获取到店铺信息");
        //非自营店铺限制
        if (storeDb.getIsOwnStore().equals(StoreConst.NO_OWN_STORE)) {
            //获取店铺等级限制的发布商品数
            StoreGrade storeGradeDb = storeGradeModel.getStoreGradeByGradeId(storeDb.getStoreGradeId());
            //获取该店铺已发布的商品数
            GoodsExample example = new GoodsExample();
            example.setStoreId(storeId);
            example.setStateNotEquals(GoodsConst.GOODS_STATE_DELETE);
            example.setWebSite(webSite);
            List<Goods> goodsList = goodsModel.getGoodsList(example, null);
            if (goodsList.size() >= storeGradeDb.getGoodsLimit()) {
                isCanPublish = false;
            }
        }
        return isCanPublish;
    }

}
