# TaRecordsStatusManualController.java

    package cloud.wconnect.finance.controller.ta;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.cons.CommonFieldI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.finance.cons.ta.TaRecordsStatusEnum;
    import cloud.wconnect.finance.entity.ta.TaRecordsStatus;
    import cloud.wconnect.finance.pojo.dto.ta.manualrecords.TaRecordsStatusManualDTO;
    import cloud.wconnect.finance.service.ta.ITaRecordsStatusManualService;
    import cloud.wconnect.finance.service.ta.ITaRecordsStatusService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.springframework.web.bind.annotation.*;

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

    /**
     * <p>
     * TA Records status manual 前端控制器
     * </p>
     *
     * @author zc
     * @since 2023-07-14
     */
    @RestController
    @RequestMapping("/ta/manual-records")
    public class TaRecordsStatusManualController {
        @Resource
        private ITaRecordsStatusService taRecordsStatusService;
        @Resource
        private ITaRecordsStatusManualService taRecordsStatusManualService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 获取手动创建的数据详情
         *
         * @param id       数据 id
         * @param property 资产标识
         * @return TaRecordsStatusManualDTO
         * @author zc
         * @date 2023/7/14
         */
        @ApiId(448)
        @GetMapping("/{id:\\d+}")
        public TaRecordsStatusManualDTO getDetails(@PathVariable long id, @RequestParam String property) {
            int propertyId = sessionContext.getPropertyId(property);

            TaRecordsStatus taRecordsStatus = checkManualData(propertyId, id);

            return taRecordsStatusManualService.getDetails(taRecordsStatus);
        }

        /**
         * 保存手动创建的数据
         *
         * @param property                 资产标识
         * @param taRecordsStatusManualDTO 数据内容
         * @return id
         * @author zc
         * @date 2023/7/14
         */
        @ApiId(449)
        @PostMapping
        public Map<String, Long> save(@RequestParam String property, @RequestBody @Valid TaRecordsStatusManualDTO taRecordsStatusManualDTO) {
            if (Objects.isNull(taRecordsStatusManualDTO.getPortal())
                    && Objects.isNull(taRecordsStatusManualDTO.getTba())) {
                throw new BadRequestException(CommonErrorI18nConstant.CANNOT_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("portal & tba"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The portal and tba cannot all be null");
            }

            int propertyId = sessionContext.getPropertyId(property);
            int loginUserId = sessionContext.getLoginUserId();

            // 新增数据
            if (Objects.isNull(taRecordsStatusManualDTO.getId())) {
                return ImmutableMap.of("id", taRecordsStatusManualService.add(propertyId,
                        loginUserId, taRecordsStatusManualDTO));
            }

            // 更新数据
            TaRecordsStatus originTaRecordsStatus = checkManualData(propertyId, taRecordsStatusManualDTO.getId());

            taRecordsStatusManualService.update(propertyId, loginUserId, originTaRecordsStatus, taRecordsStatusManualDTO);

            return ImmutableMap.of("id", taRecordsStatusManualDTO.getId());
        }

        /**
         * 删除手动创建的数据
         *
         * @param id       数据 id
         * @param property 资产标识
         * @return empty map
         * @author zc
         * @date 2023/7/14
         */
        @ApiId(450)
        @DeleteMapping("/{id:\\d+}")
        public Map<Object, Object> remove(@PathVariable long id, @RequestParam String property) {
            int propertyId = sessionContext.getPropertyId(property);

            TaRecordsStatus taRecordsStatus = checkManualData(propertyId, id);

            taRecordsStatusManualService.remove(taRecordsStatus);

            return Collections.emptyMap();
        }

        /**
         * 校验手动创建的数据
         *
         * @param propertyId 资产 id
         * @param id         数据 id
         * @return taRecordsStatus 数据
         * @author zc
         * @date 2023/01/03
         */
        private TaRecordsStatus checkManualData(int propertyId, long id) {
            // 校验数据是否存在
            TaRecordsStatus taRecordsStatus = taRecordsStatusService.checkExist(id);

            // 校验数据归属
            taRecordsStatusService.checkBelong(propertyId, taRecordsStatus);

            // 校验数据
            if (!Lists.newArrayList(TaRecordsStatusEnum.MATCHED.getCode(),
                    TaRecordsStatusEnum.TBA.getCode()).contains(taRecordsStatus.getStatus())
                    || taRecordsStatus.getCreateId().equals(BaseConstant.ZERO)) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_IN_LIST,
                        i18nMessageUtils.getErrorParamMapNeedTranslate(CommonFieldI18nConstant.STATUS,
                                List.of(TaRecordsStatusEnum.MATCHED.getI18nCode(), TaRecordsStatusEnum.TBA.getI18nCode())),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The taRecordsStatus must be in [1:matched; 5:TBA] and must be create by manual.");
            }
            return taRecordsStatus;
        }
    }