package com.yc.ship.module.product.dal.mysql.pricearea;

import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.product.controller.admin.pricetemplate.vo.PriceCountryRespVO;
import com.yc.ship.module.product.dal.dataobject.pricearea.PriceAreaDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 区域加价设置 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface PriceAreaMapper extends BaseMapperX<PriceAreaDO> {


    default void deleteByObjectId(Long objectId) {
        delete(new LambdaQueryWrapperX<PriceAreaDO>().eq(PriceAreaDO::getObjectId, objectId));
    }

    default List<PriceAreaDO> selectListByObjectId(Long id) {
        return selectList(new LambdaQueryWrapperX<PriceAreaDO>().eq(PriceAreaDO::getObjectId, id));
    }

    default List<PriceCountryRespVO> selectCountryPriceByObjectId(Long id) {
        List<PriceCountryRespVO> list = new ArrayList<>();
        List<PriceAreaDO> priceAreaList = selectList(new LambdaQueryWrapperX<PriceAreaDO>().eq(PriceAreaDO::getObjectId, id).isNotNull(PriceAreaDO::getParentAreaId));
        priceAreaList.forEach(priceAreaDO -> {
            PriceCountryRespVO priceCountryRespVO = new PriceCountryRespVO();
            priceCountryRespVO.setPrice(priceAreaDO.getPrice());
            priceCountryRespVO.setCountryId(priceAreaDO.getAreaId().intValue());
            list.add(priceCountryRespVO);
        });
        return list;
    }
}