package com.gree.mall.manager.logic.goods;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.GoodsTag;
import com.gree.mall.manager.plus.service.GoodsTagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class GoodsTagLogic {

    @Autowired
    private GoodsTagService goodsTagService;

    @Autowired
    CommonLogic commonLogic;


    /**
     * page查询商品标签
     */
    public IPage<GoodsTag> pageGoodsTag(HttpServletRequest request, Integer pageNum, Integer pageSize,Integer type) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getAdminCompanyIds();

        return goodsTagService.lambdaQuery()
                .eq(type != null, GoodsTag::getType, type)
                .page(new Page<>(pageNum, pageSize));
    }




    /**
     * 批量编辑标签
     */
    public void updateGoodsTag(HttpServletRequest request, String tags,Integer type) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);


        //清除旧标签
        goodsTagService.lambdaUpdate()
                .eq(GoodsTag::getType, type)
                .remove();

        //添加新标签
        if (StringUtils.isNotEmpty(tags)) {
            List<String> tagList = Arrays.asList(tags.trim().split(","));

            List<GoodsTag> saveTagList = new ArrayList<>();
            for (String tagStr : tagList
            ) {
                GoodsTag goodsTag = new GoodsTag();
                goodsTag.setGoodsTagName(tagStr);
                goodsTag.setCreateBy(adminUser.getAdminUserId());
                goodsTag.setType(type);
                saveTagList.add(goodsTag);
            }
            goodsTagService.saveBatch(saveTagList);
        }

    }



}
