package com.xingxi.web.controller.master.merchant.service.impl;

import com.xingxi.common.core.text.Convert;
import com.xingxi.common.enums.EDelFlag;
import com.xingxi.common.exception.BusinessException;
import com.xingxi.common.utils.DateUtils;
import com.xingxi.common.utils.ShiroUtils;
import com.xingxi.master.merchant.domain.MerchantProd;
import com.xingxi.master.merchant.service.impl.MerchantProdServiceImpl;
import com.xingxi.web.controller.master.merchant.domain.MerchantProdVo;
import com.xingxi.web.controller.master.merchant.mapper.MerchantProdVoMapper;
import com.xingxi.web.controller.master.merchant.service.IMerchantProdVoService;
import com.xingxi.web.controller.master.product.domain.ProdVo;
import com.xingxi.web.controller.master.product.mapper.ProdVoMapper;
import org.springframework.stereotype.Service;

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

/**
 * 店铺商品Service业务层处理
 *
 * @author xingxi
 * @date 2025-03-06
 */
@Service
public class MerchantProdVoServiceImpl extends MerchantProdServiceImpl implements IMerchantProdVoService {
    @Resource
    private MerchantProdVoMapper merchantProdVoMapper;

    /**
     * 查询店铺商品列表
     *
     * @param mercId 店铺ID
     * @return 商品
     */
    @Override
    public List<MerchantProdVo> selectMerchantProdVoByMercId(Long mercId) {
        return merchantProdVoMapper.selectMerchantProdVoByMercId(mercId);
    }

    /**
     * 查询店铺商品列表
     *
     * @param merchantProdVo 店铺商品
     * @return 商品
     */
    @Override
    public List<MerchantProdVo> selectMerchantProdVoList(MerchantProdVo merchantProdVo) {
        merchantProdVo.setDelFlag(EDelFlag.NO.getVal());
        return merchantProdVoMapper.selectMerchantProdVoList(merchantProdVo);
    }

    /**
     * 修改店铺商品
     *
     * @param merchantProdVo 店铺商品Vo
     * @return 结果
     */
    @Override
    public int updateMerchantProdVo(MerchantProdVo merchantProdVo) {
        String[] prodAttrIds = Convert.toStrArray(merchantProdVo.getProdAttrIds());
        MerchantProd cond = new MerchantProd();
        cond.setMercId(merchantProdVo.getMercId());
        List<MerchantProdVo> checkResult = merchantProdVoMapper.selectMerchantProdVoByMercId(merchantProdVo.getMercId());
        if (checkResult.size() > 0) {
            int effectRows = merchantProdVoMapper.logicDeleteMerchantProdByCondition(cond);
            if (effectRows == 0) {
                throw new BusinessException("删除失败，请重试！");
            }
        }
        int effectRows = 0;
        List<MerchantProd> batchInsertList = new ArrayList<>();
        for (String prodAttrId : prodAttrIds) {
            MerchantProd merchantProd = new MerchantProd();
            merchantProd.setMercId(merchantProdVo.getMercId());
            merchantProd.setProdAttrId(Long.parseLong(prodAttrId));
            merchantProd.setDelFlag(EDelFlag.NO.getVal());
            merchantProd.setCreateUser(ShiroUtils.getLoginName());
            merchantProd.setCreateTime(DateUtils.getNowDate());
            batchInsertList.add(merchantProd);
        }
        effectRows += merchantProdVoMapper.batchInsertMerchantProd(batchInsertList);
        return effectRows;
    }
}
