package cn.iocoder.yudao.module.basic.service.dealergoods;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.basic.controller.admin.dealergoods.vo.DealerGoodsCreateReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.dealergoods.vo.DealerGoodsListReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.dealergoods.vo.DealerGoodsPageReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.dealergoods.vo.DealerGoodsUpdateReqVO;
import cn.iocoder.yudao.module.basic.convert.dealergoods.DealerGoodsConvert;
import cn.iocoder.yudao.module.basic.dal.dataobject.dealergoods.DealerGoodsDO;
import cn.iocoder.yudao.module.basic.dal.mysql.dealergoods.DealerGoodsMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants.DEALER_GOODS_NOT_EXISTS;

/**
 * 经销商经销产品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DealerGoodsServiceImpl implements DealerGoodsService {

    @Resource
    private DealerGoodsMapper dealerGoodsMapper;

    @Override
    public Long createDealerGoods(DealerGoodsCreateReqVO createReqVO) {

        // 插入
        DealerGoodsDO dealerGoods = DealerGoodsConvert.INSTANCE.convert(createReqVO);
        dealerGoodsMapper.insert(dealerGoods);
        // 返回
        return dealerGoods.getId();
    }

    @Override
    public void updateDealerGoods(DealerGoodsUpdateReqVO updateReqVO) {
        // 校验存在
        validateDealerGoodsExists(updateReqVO.getId());
        // 更新
        DealerGoodsDO updateObj = DealerGoodsConvert.INSTANCE.convert(updateReqVO);
        dealerGoodsMapper.updateById(updateObj);
    }

    @Override
    public void deleteDealerGoods(Long id) {
        // 校验存在
        validateDealerGoodsExists(id);
        // 删除
        dealerGoodsMapper.deleteById(id);
    }

    private void validateDealerGoodsExists(Long id) {
        if (dealerGoodsMapper.selectById(id) == null) {
            throw exception(DEALER_GOODS_NOT_EXISTS);
        }
    }

    @Override
    public DealerGoodsDO getDealerGoods(Long id) {
        return dealerGoodsMapper.selectById(id);
    }

    /**
     * 获得经销商经销产品列表
     *
     * @param dealerGoodsListReqVO 根据信息内容动态查询
     * @return 经销商经销产品列表
     */
    @Override
    public List<DealerGoodsDO> getDealerGoodsListQuery(DealerGoodsListReqVO dealerGoodsListReqVO) {
        return dealerGoodsMapper.selectListQuery(dealerGoodsListReqVO);
    }

    @Override
    public PageResult<DealerGoodsDO> getDealerGoodsPage(DealerGoodsPageReqVO pageReqVO) {
        return dealerGoodsMapper.selectPage(pageReqVO);
    }

    /**
     * 根据商品ID和经销商ID获取所有对应的销售区域
     *
     * @param dealerId
     * @return
     */
    @Override
    public List<DealerGoodsDO> getSaleAreasByGoodsAndDealer(String goodsId, String dealerId) {
        List<DealerGoodsDO> dealerGoodsList =
                dealerGoodsMapper.selectList(new LambdaQueryWrapperX<DealerGoodsDO>()
                        .eq(DealerGoodsDO::getGoodsId, goodsId)
                        .eq(DealerGoodsDO::getDealerId, dealerId));
        List<String> saleAreas = new ArrayList<>();
        // 遍历经销商商品信息列表，找到匹配的商品ID和经销商ID的销售区域
        for (DealerGoodsDO dealerGoods : dealerGoodsList) {
            if (dealerGoods.getGoodsId().equals(goodsId) && dealerGoods.getDealerId().equals(dealerId)) {
                saleAreas.add(dealerGoods.getSaleArea());
            }
        }
        return dealerGoodsList;
    }

}
