# AdjNotifyRuleController.java

    package cloud.wconnect.frontoffice.controller.adjustment;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.commonlib.util.UpdateLogUtil;
    import cloud.wconnect.frontoffice.cons.adjustment.AdjNotifyConfigTypeEnum;
    import cloud.wconnect.frontoffice.entity.adjustment.AdjNotifyRule;
    import cloud.wconnect.frontoffice.pojo.dto.adjustment.AdjNotifyRuleDTO;
    import cloud.wconnect.frontoffice.service.adjustment.IAdjNotifyRuleService;
    import com.google.common.collect.Lists;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import jakarta.validation.Valid;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Map;
    import java.util.Objects;

    /**
     * 调账功能通知规则控制层
     *
     * @author lichen
     * @date 2024/9/20
     */
    @RestController
    @RequestMapping("/adjustment/notify-rule")
    public class AdjNotifyRuleController {

        @Autowired
        private IAdjNotifyRuleService notifyRuleService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private UpdateLogUtil updateLogUtil;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询资产在指定类型下配置的通知规则
         *
         * @param property 资产
         * @param type     类型
         * @return cloud.wconnect.frontoffice.entity.adjustment.AdjNotifyRule
         * @author lichen
         * @date 2024/9/20
         */
        @ApiId(1251)
        @GetMapping
        public AdjNotifyRule get(String property, @RequestParam Integer type) {
            ArrayList<Integer> typeList = Lists.newArrayList(AdjNotifyConfigTypeEnum.DRAFT_STATISTICS.getType(), AdjNotifyConfigTypeEnum.DRAFT_STATISTICS_EACH_TYPE.getType());
            if (!typeList.contains(type)) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_IN_LIST,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("type", typeList),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The type must between in [1:Notification of the number of drafts of the person responsible 4:Notification of the number of drafts of each type]");
            }

            int propertyId = sessionContext.getPropertyId(property);

            //查询通知规则
            AdjNotifyRule notifyRule = notifyRuleService.getByPropertyIdAndType(propertyId, type);

            return Objects.nonNull(notifyRule) ? notifyRule : new AdjNotifyRule();
        }

        /**
         * 保存通知规则
         *
         * @param property      资产
         * @param notifyRuleDTO 通知规则DTO
         * @return java.util.Map
         * @author lichen
         * @date 2024/9/20
         */
        @ApiId(1252)
        @PostMapping
        public Map save(String property, @RequestBody @Valid AdjNotifyRuleDTO notifyRuleDTO) {
            ArrayList<Integer> typeList = Lists.newArrayList(AdjNotifyConfigTypeEnum.DRAFT_STATISTICS.getType(), AdjNotifyConfigTypeEnum.DRAFT_STATISTICS_EACH_TYPE.getType());
            if (!typeList.contains(notifyRuleDTO.getType())) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_IN_LIST,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("type", typeList),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The type must between in [1:Notification of the number of drafts of the person responsible 4:Notification of the number of drafts of each type]");
            }

            int propertyId = sessionContext.getPropertyId(property);
            //查询通知规则
            AdjNotifyRule notifyRule = notifyRuleService.getByPropertyIdAndType(propertyId, notifyRuleDTO.getType());

            if (Objects.isNull(notifyRule)) {
                AdjNotifyRule insertEntity = new AdjNotifyRule();
                insertEntity.setPropertyId(propertyId);
                insertEntity.setRule(notifyRuleDTO.getRule());
                insertEntity.setType(notifyRuleDTO.getType());
                insertEntity.setUpdateLog(updateLogUtil.get(sessionContext.getLoginUserId()));
                notifyRuleService.save(insertEntity);
            } else {
                String oriUpdateLog = notifyRule.getUpdateLog();

                notifyRule.setRule(notifyRuleDTO.getRule());
                notifyRule.setUpdateLog(updateLogUtil.append(oriUpdateLog, sessionContext.getLoginUserId()));
                notifyRuleService.updateById(notifyRule);
            }
            return Collections.emptyMap();
        }

    }