# RevenueDailySummaryTaFilterConfigController.java

    package cloud.wconnect.rateshop.controller.revenue;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.rateshop.entity.revenue.RevenueDailySummaryTaFilterConfig;
    import cloud.wconnect.rateshop.pojo.dto.revenue.RevenueDailySummaryTaFilterConfigDTO;
    import cloud.wconnect.rateshop.pojo.query.revenue.DailySummaryTaFilterQuery;
    import cloud.wconnect.rateshop.service.revenue.IRevenueDailySummaryTaFilterConfigService;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.google.common.collect.ImmutableMap;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import jakarta.validation.Valid;
    import java.text.MessageFormat;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;

    /**
     * Daily Summary Market Code配置
     *
     * @author lichen
     * @date 2024/3/27
     */
    @RestController
    @RequestMapping("/revenue/daily-summary/market-code-config")
    public class RevenueDailySummaryTaFilterConfigController {

        @Autowired
        private IRevenueDailySummaryTaFilterConfigService taFilterConfigService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private UpdateLogUtil updateLogUtil;

        /**
         * 查询Daily Summary Market Code配置
         *
         * @param property 资产
         * @param query    查询条件
         * @return java.util.List<cloud.wconnect.rateshop.entity.revenue.RevenueDailySummaryTaFilterConfig>
         * @author lichen
         * @date 2024/3/27
         */
        @ApiId(1112)
        @GetMapping
        public List<RevenueDailySummaryTaFilterConfig> list(String property, @Valid DailySummaryTaFilterQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            return taFilterConfigService.list(new LambdaQueryWrapper<RevenueDailySummaryTaFilterConfig>()
                    .eq(RevenueDailySummaryTaFilterConfig::getPropertyId, propertyId)
                    .like(StringUtils.isNotBlank(query.getMarketCode()), RevenueDailySummaryTaFilterConfig::getMarketCode, query.getMarketCode())
                    .orderByDesc(RevenueDailySummaryTaFilterConfig::getId));
        }

        /**
         * 保存Market Code
         *
         * @param property          资产
         * @param taFilterConfigDTO 配置Market Code DTO
         * @return java.util.Map
         * @author lichen
         * @date 2024/3/27
         */
        @ApiId(1113)
        @PostMapping("/{id:\\d+}")
        public Map save(String property, @PathVariable Integer id,
                        @RequestBody @Valid RevenueDailySummaryTaFilterConfigDTO taFilterConfigDTO) {
            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();
            if (id > 0) {
                //校验存在
                RevenueDailySummaryTaFilterConfig oriTaFilterConfig = taFilterConfigService.checkExist(id);
                //校验归属
                checkBelong(oriTaFilterConfig, propertyId);
                //校验market code唯一
                taFilterConfigService.checkSole(propertyId, taFilterConfigDTO.getMarketCode(), id);
                //更新
                oriTaFilterConfig.setMarketCode(taFilterConfigDTO.getMarketCode());
                oriTaFilterConfig.setUpdateLog(updateLogUtil.append(oriTaFilterConfig.getUpdateLog(), loginUserId));
                taFilterConfigService.updateById(oriTaFilterConfig);
                return ImmutableMap.of("id", id);
            } else {
                //校验market code唯一
                taFilterConfigService.checkSole(propertyId, taFilterConfigDTO.getMarketCode(), null);
                RevenueDailySummaryTaFilterConfig revenueDailySummaryTaFilterConfig = new RevenueDailySummaryTaFilterConfig()
                        .setPropertyId(propertyId)
                        .setMarketCode(taFilterConfigDTO.getMarketCode()).setUpdateLog(updateLogUtil.get(loginUserId));
                taFilterConfigService.save(revenueDailySummaryTaFilterConfig);
                return ImmutableMap.of("id", revenueDailySummaryTaFilterConfig.getId());
            }
        }

        /**
         * 删除Market Code配置
         *
         * @param property 资产ID
         * @param id       配置主键ID
         * @return java.util.Map
         * @author lichen
         * @date 2024/3/27
         */
        @ApiId(1114)
        @DeleteMapping("/{id:\\d+}")
        public Map delete(String property, @PathVariable Integer id) {
            int propertyId = sessionContext.getPropertyId(property);
            //校验存在
            RevenueDailySummaryTaFilterConfig oriTaFilterConfig = taFilterConfigService.checkExist(id);
            //校验归属
            checkBelong(oriTaFilterConfig, propertyId);
            //删除
            taFilterConfigService.removeById(id);
            return Collections.emptyMap();
        }

        /**
         * 校验是否归属于当前资产
         *
         * @param oriTaFilterConfig market code 配置
         * @param propertyId        资产ID
         * @return void
         * @author lichen
         * @date 2024/3/27
         */
        private void checkBelong(RevenueDailySummaryTaFilterConfig oriTaFilterConfig, Integer propertyId) {
            if (!oriTaFilterConfig.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(), BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The Daily Summary filter config not belong currentPropertyId, filterConfigId:{0}, belongPropertyId:{1}, currentPropertyId:{2}",
                                oriTaFilterConfig.getId(), oriTaFilterConfig.getPropertyId(), propertyId));
            }
        }

    }