package com.fzm.mall.server.admin.shop.model;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.admin.shop.service.INavigationGoodsService;
import com.fzm.mall.server.admin.util.UUIDUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 店铺中心-店铺导航管理
 * </p>
 *
 * @author wulixian
 * @since 2021-04-26
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("shop_navigation")
@ApiModel(value = "Navigation对象", description = "店铺中心-店铺导航管理")
public class Navigation implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 导航状态:0->已删除
     */
    public static final Integer SHOP_NAVIGATION_STATUS_DELETED = 0;

    /**
     * 导航状态:1->有效
     */
    public static final Integer SHOP_NAVIGATION_STATUS_EFFECTIVE = 1;

    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "导航编号")
    private String navigationId;

    @ApiModelProperty(value = "店铺编号")
    private String shopId;

    @ApiModelProperty(value = "导航名称")
    private String navigationName;

    @ApiModelProperty(value = "导航图标url")
    private String image;

    @ApiModelProperty(value = "导航状态(0->已删除;1->有效)")
    private Integer status;

    @ApiModelProperty(value = "显示顺序")
    private Integer displayOrder;

    @ApiModelProperty(value = "商户号")
    private String merchantId;

    @ApiModelProperty(value = "信息入库时间")
    @TableField(fill = FieldFill.INSERT)
    private Long createTime;

    @ApiModelProperty(value = "信息更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateTime;

    @TableField(exist = false)
    @ApiModelProperty(value = "产品列表")
    private List<NavigationGoods> goodsList;

    public void init() {
        setNavigationId(UUIDUtil.getUUID());
        setStatus(SHOP_NAVIGATION_STATUS_EFFECTIVE);
    }

    public void saveNavigationGoods(INavigationGoodsService navigationGoodsService) {
        if (CollectionUtils.isNotEmpty(this.goodsList)) {
            this.goodsList.forEach(navigationGoods -> {
                navigationGoods.setNavigationId(this.navigationId);
                navigationGoods.setMerchantId(this.merchantId);
            });
            navigationGoodsService.saveBatch(this.goodsList);
        }
    }

    public void updateNavigationGoods(INavigationGoodsService navigationGoodsService) {
        deleteNavigationGoods(navigationGoodsService);

        saveNavigationGoods(navigationGoodsService);
    }

    /**
     * 删掉导航-商品中间表数据
     * @param navigationGoodsService
     */
    public void deleteNavigationGoods(INavigationGoodsService navigationGoodsService) {
        QueryWrapper<NavigationGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("navigation_id", this.navigationId)
                .eq("merchant_id", this.merchantId);
        navigationGoodsService.remove(queryWrapper);
    }

    public void listGoods(INavigationGoodsService navigationGoodsService) {
        QueryWrapper<NavigationGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("navigation_id", this.navigationId)
                .eq("merchant_id", this.merchantId);
        setGoodsList(navigationGoodsService.list(queryWrapper));
    }
}
