package com.slodon.b2b2c.model.goods;

import com.slodon.b2b2c.core.constant.GoodsAnnexStatusConst;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.dao.read.goods.GoodsAnnexReadMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsAnnexWriterMapper;
import com.slodon.b2b2c.goods.dto.GoodsPublishFrontParamDTO;
import com.slodon.b2b2c.goods.example.GoodsPictureExample;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.goods.pojo.GoodsAnnex;
import com.slodon.b2b2c.goods.pojo.GoodsPicture;
import com.slodon.b2b2c.member.example.GoodsAnnexExample;
import com.slodon.b2b2c.seller.pojo.Vendor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class GoodsAnnexModel {

    @Resource
    private GoodsAnnexReadMapper goodsAnnexReadMapper;

    @Resource
    private GoodsAnnexWriterMapper goodsAnnexWriterMapper;

    @Resource
    private GoodsAnnexModel goodsAnnexModel;

    @Resource
    private GoodsSellerModel goodsSellerModel;

    /**
     * 根据条件查询goods_annex表记录
     * @param example
     * @return
     */
    public List<GoodsAnnex> goodsAnnexList(GoodsAnnexExample example) {
        List<GoodsAnnex> goodsAnnexList;
        goodsAnnexList = goodsAnnexReadMapper.selectByExample(example);
        return goodsAnnexList;
    }

    /**
     * 根据goodsId新增商品附件记录
     * @param goods
     */
    public void insertGoodsAnnex(List<GoodsPublishFrontParamDTO.AnnexInfo> annexInfoList,Goods goods, Vendor vendor){
        if(!CollectionUtils.isEmpty(annexInfoList)){
            annexInfoList.forEach(annexInfo ->
                    insertOrUpdateGoodsAnnex(annexInfo,goods.getGoodsId(),vendor));
        }
    }

    /**
     * 根据goodsId删除（逻辑删除，status置为0）之后更新商品附件记录
     * @param goods
     */
    public void deleteAndUpdateGoodsAnnex(List<GoodsPublishFrontParamDTO.AnnexInfo> annexInfoList,Goods goods, Vendor vendor){
        if(StringUtils.isEmpty(goods.getGoodsId())){
            throw new MallException("请选择要删除的数据");
        }
        GoodsAnnex annex = new GoodsAnnex();
        annex.setStatus(GoodsAnnexStatusConst.NOT_STATUS);
        GoodsAnnexExample goodsAnnexExample = new GoodsAnnexExample();
        goodsAnnexExample.createCriteria().andGoodsIdEqualTo(goods.getGoodsId());
        goodsAnnexWriterMapper.updateByExampleSelective(annex,goodsAnnexExample);

        if(!CollectionUtils.isEmpty(annexInfoList)){
            annexInfoList.forEach(annexInfo ->
                    insertOrUpdateGoodsAnnex(annexInfo,goods.getGoodsId(),vendor));
        }


    }


    public void insertOrUpdateGoodsAnnex(GoodsPublishFrontParamDTO.AnnexInfo annexInfo, Long goodsId, Vendor vendor){
        GoodsAnnex goodsAnnex = new GoodsAnnex();
        goodsAnnex.setGoodsId(goodsId);
        goodsAnnex.setAnnexName(annexInfo.getAnnexName());
        goodsAnnex.setAnnexPath(annexInfo.getAnnexPath());
        goodsAnnex.setSort(1);
        goodsAnnex.setCreateId(vendor.getVendorId());
        goodsAnnex.setCreateTime(new Date());
        goodsAnnex.setStoreId(vendor.getStoreId());
        goodsAnnex.setStatus(GoodsAnnexStatusConst.IS_STATUS);
        goodsAnnexModel.saveAnnex(goodsAnnex);
    }

    /**
     * 新增goods_annex表
     * @param goodsAnnex
     * @return
     */
    public Integer saveAnnex(GoodsAnnex goodsAnnex){
        int count = goodsAnnexWriterMapper.insert(goodsAnnex);
        if(count == 0){
            throw new MallException("添加商品对应附件表失败，请重试");
        }
        return count;
    }



}
