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

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

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

/**
 * 区域基准价设置 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface BasicPriceAreaMapper extends BaseMapperX<BasicPriceAreaDO> {

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

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

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

}