# RateShopNotifyRuleController.java

    package cloud.wconnect.rateshop.controller.rateshop;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.rateshop.cons.rateshop.RateShopNotifyConfigTypeEnum;
    import cloud.wconnect.rateshop.entity.rateshop.RateShopNotifyRule;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.NotifyRuleDTO;
    import cloud.wconnect.rateshop.service.rateshop.IRateShopNotifyRuleService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import jakarta.validation.Valid;
    import java.util.Collections;
    import java.util.Map;
    import java.util.Objects;

    /**
     * <p>
     * rate shop通知规则表 前端控制器
     * </p>
     *
     * @author lichen
     * @since 2024-06-18
     */
    @RestController
    @RequestMapping("/rate-shop/notify-rule")
    public class RateShopNotifyRuleController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private IRateShopNotifyRuleService rateShopNotifyRuleService;
        @Autowired
        private UpdateLogUtil updateLogUtil;

        /**
         * 查询rateShop的通知规则
         *
         * @param property 资产标识
         * @return cloud.wconnect.rateshop.entity.rateshop.RateShopNotifyRule
         * @author lichen
         * @date 2024/6/18
         */
        @ApiId(1198)
        @GetMapping
        public RateShopNotifyRule get(String property) {
            //资产ID
            int propertyId = sessionContext.getPropertyId(property);
            //获取资产的通知规则
            return rateShopNotifyRuleService.getByPropertyIdAndTypeOrDefault(propertyId, RateShopNotifyConfigTypeEnum.OCC_SNAPSHOT_FOR_THE_NEXT_N_DAYS.getType());
        }

        /**
         * 保存rateShop的通知规则
         *
         * @param property      资产标识
         * @param notifyRuleDTO 通知DTO
         * @return java.util.Map
         * @author lichen
         * @date 2024/6/18
         */
        @ApiId(1199)
        @PostMapping
        public Map save(String property, @RequestBody @Valid NotifyRuleDTO notifyRuleDTO) {
            //资产ID
            int propertyId = sessionContext.getPropertyId(property);

            RateShopNotifyRule rateShopNotifyRule = rateShopNotifyRuleService.getByPropertyIdAndType(propertyId,
                    RateShopNotifyConfigTypeEnum.OCC_SNAPSHOT_FOR_THE_NEXT_N_DAYS.getType());

            if(Objects.isNull(rateShopNotifyRule)){
                rateShopNotifyRule = new RateShopNotifyRule().setPropertyId(propertyId)
                        .setRule(notifyRuleDTO.getRule()).setType(RateShopNotifyConfigTypeEnum.OCC_SNAPSHOT_FOR_THE_NEXT_N_DAYS.getType())
                        .setUpdateLog(updateLogUtil.get(sessionContext.getLoginUserId()));
                rateShopNotifyRuleService.save(rateShopNotifyRule);
            }else{
                String oriUpdateLog = rateShopNotifyRule.getUpdateLog();
                rateShopNotifyRule.setRule(notifyRuleDTO.getRule())
                        .setUpdateLog(updateLogUtil.append(oriUpdateLog, sessionContext.getLoginUserId()));
                rateShopNotifyRuleService.updateById(rateShopNotifyRule);
            }
            return Collections.emptyMap();
        }

    }