# RevenueMarketCodeServiceImpl.java

    package cloud.wconnect.rateshop.service.revenue.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.rateshop.entity.revenue.RevenueMarketCode;
    import cloud.wconnect.rateshop.mapper.revenue.RevenueMarketCodeMapper;
    import cloud.wconnect.rateshop.pojo.dto.revenue.MarketCodeDTO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueMarketCodeService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2024-03-23
     */
    @Service
    @DS("rate_shop")
    public class RevenueMarketCodeServiceImpl extends ServiceImpl<RevenueMarketCodeMapper, RevenueMarketCode> implements IRevenueMarketCodeService {

        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private UpdateLogUtil updateLogUtil;

        /**
         * 获取某资产下有效的 market code
         *
         * @param propertyId 资产 id
         * @return 有效的 market code
         * @author yyt
         * @date 2024/3/23
         **/
        @Override
        public List<RevenueMarketCode> listEffiective(Integer propertyId) {
            return list(new QueryWrapper<RevenueMarketCode>().lambda().eq(RevenueMarketCode::getPropertyId, propertyId)
                    .eq(RevenueMarketCode::getStatus, true));
        }

        /**
         * 检查重复
         *
         * @param propertyId    资产 id
         * @param marketCodeDTO marketCodeDTO
         * @author yyt
         * @date 2024/3/23
         **/
        @Override
        public void checkRepeat(Integer propertyId, MarketCodeDTO marketCodeDTO) {
            long count = count(new QueryWrapper<RevenueMarketCode>().lambda().eq(RevenueMarketCode::getPropertyId, propertyId)
                    .eq(RevenueMarketCode::getType, marketCodeDTO.getType()).eq(RevenueMarketCode::getMarketCode, marketCodeDTO.getMarketCode())
                    .eq(RevenueMarketCode::getStatus, true)
                    .ne(Objects.nonNull(marketCodeDTO.getId()), RevenueMarketCode::getId, marketCodeDTO.getId()));
            if (count > BaseConstant.ZERO) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_EXISTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "Failed to save existing data");
            }
        }

        /**
         * 检查存在
         *
         * @param id market code id
         * @return RevenueMarketCode
         * @author yyt
         * @date 2024/3/23
         **/
        @Override
        public RevenueMarketCode checkExist(Integer id) {
            RevenueMarketCode marketCode = getById(id);
            if (Objects.isNull(marketCode)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_FOUND,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "the marketCode is not found;id:" + id);
            }
            return marketCode;
        }

        /**
         * 保存 market code
         *
         * @param propertyId    资产 id
         * @param marketCodeDTO market code
         * @param userId        操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void save(int propertyId, MarketCodeDTO marketCodeDTO, int userId) {
            RevenueMarketCode revenueMarketCode = beanMapper.map(marketCodeDTO, RevenueMarketCode.class);
            revenueMarketCode.setPropertyId(propertyId);
            revenueMarketCode.setStatus(true);
            revenueMarketCode.setUpdateLog(updateLogUtil.get(userId));
            save(revenueMarketCode);
        }

        /**
         * 更新 market code
         *
         * @param revenueMarketCode 更新前的 market code
         * @param marketCodeDTO     market code
         * @param loginUserId       操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void update(RevenueMarketCode revenueMarketCode, MarketCodeDTO marketCodeDTO, int loginUserId) {
            RevenueMarketCode update = new RevenueMarketCode();
            update.setId(revenueMarketCode.getId())
                    .setMarketCode(marketCodeDTO.getMarketCode())
                    .setCalculationRule(marketCodeDTO.getCalculationRule())
                    .setUpdateLog(updateLogUtil.append(revenueMarketCode.getUpdateLog(), loginUserId));
            updateById(update);
        }

        /**
         * 删除 market code
         *
         * @param marketCode 更新前的 market code
         * @param userId     操作人
         * @author yyt
         * @date 2026/1/19
         **/
        @Override
        public void delete(RevenueMarketCode marketCode, int userId) {
            RevenueMarketCode update = new RevenueMarketCode();
            update.setId(marketCode.getId())
                    .setStatus(false)
                    .setUpdateLog(updateLogUtil.append(marketCode.getUpdateLog(), userId));
            updateById(update);
        }

    }