# TaInternalController.java

    package cloud.wconnect.finance.controller.ta.internal;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.controller.BaseInternalController;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.finance.cons.ta.TaReportTypeCons;
    import cloud.wconnect.finance.entity.ta.TaImportLog;
    import cloud.wconnect.finance.pojo.bo.ta.portal.TaPortalBO;
    import cloud.wconnect.finance.pojo.bo.ta.portal.TaPortalListBO;
    import cloud.wconnect.finance.pojo.dto.ta.TaImportResultDTO;
    import cloud.wconnect.finance.pojo.dto.ta.portal.TaPortalDTO;
    import cloud.wconnect.finance.service.ta.ITaComparePortalService;
    import cloud.wconnect.finance.service.ta.ITaImportLogService;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.*;

    import java.math.RoundingMode;
    import java.time.LocalDate;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;

    /**
     * <p>
     * TA 内部接口 前端控制器
     * </p>
     *
     * @author zc
     * @since 2023-07-07
     */
    @Slf4j
    @RestController
    @RequestMapping("/internal/ta")
    public class TaInternalController extends BaseInternalController {
        @Resource
        private ITaImportLogService importLogService;
        @Resource
        private ITaComparePortalService taComparePortalService;
        @Resource
        private PropertyInternalService propertyInternalService;
        @Resource
        private BeanMapper beanMapper;

        @Value("${internal.service.auth-key.basic}")
        private String authKey;

        /**
         * 保存 Portal 采集数据
         *
         * @param tenant   租户标识
         * @param property 资产标识
         * @param taPortal portal 数据
         * @param sign     签名
         * @return empty map
         * @author zc
         * @date 2023/9/5
         */
        @PostMapping("/portal-report")
        public Map<Object, Object> savePortal(String tenant, String property,
                                              @RequestBody @Valid TaPortalDTO taPortal,
                                              @RequestHeader String sign) {
            log.info("TA internal - Save portal start, tenant:{}, property:{}", tenant, property);
            // 校验签名
            checkIntranetParams(authKey, sign, tenant, property);

            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(propertyDTO
                    .getInternationalization().getNumber().getCalculationRule());
            int scale = BaseConstant.DATA_PRECISION;

            taPortal.getList().forEach(item -> {
                item.setRate(item.getRate().setScale(scale, roundingMode));
                item.setTotalCharged(item.getTotalCharged().setScale(scale, roundingMode));
            });
            TaPortalBO taPortalBO = new TaPortalBO();
            LocalDate dataLocalDate = DateTimeUtils.strToLocalDate(taPortal.getDataDate().toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
            taPortalBO.setDataDate(DateTimeUtils.localDateToStr(dataLocalDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD));
            List<TaPortalListBO> taPortalListBOS = beanMapper.mapList(taPortal.getList(), TaPortalListBO.class, (dto, bo) -> {
                LocalDate arrivalLocalDate = DateTimeUtils.strToLocalDate(dto.getArrivalDate().toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
                bo.setArrivalDay(DateTimeUtils.localDateToStr(arrivalLocalDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD));
                LocalDate departureLocalDate = DateTimeUtils.strToLocalDate(dto.getDepartureDate().toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
                bo.setDepartureDay(DateTimeUtils.localDateToStr(departureLocalDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD));
                bo.setRate(dto.getRate().setScale(scale, roundingMode));
                bo.setTotalCharged(dto.getTotalCharged().setScale(scale, roundingMode));
            });
            taPortalBO.setList(taPortalListBOS);

            // 处理对比逻辑 for Portal
            taComparePortalService.handlePortalReport(propertyDTO, taPortalBO);

            log.info("TA internal - Save portal end, tenant:{}, property:{}", tenant, property);

            return Collections.emptyMap();
        }

        /**
         * 记录 Portal 数据导入日志
         *
         * @param tenant            租户标识
         * @param property          资产标识
         * @param taImportResultDTO 导入结果信息
         * @param sign              签名
         * @return empty map
         * @author zc
         * @date 2023/7/10
         */
        @PostMapping("/portal-report/log")
        public Map<Object, Object> savePortalLog(String tenant, String property,
                                                 @RequestBody @Valid TaImportResultDTO taImportResultDTO,
                                                 @RequestHeader String sign) {
            log.info("TA internal - Save portal import log start, tenant:{}, property:{}", tenant, property);

            // 校验签名
            checkIntranetParams(authKey, sign, tenant, property);

            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);

            TaImportLog importLog = new TaImportLog();
            importLog.setTenantId(propertyDTO.getTenantId());
            importLog.setPropertyId(propertyDTO.getId());
            importLog.setReportType(TaReportTypeCons.PORTAL);
            importLog.setTheDate(DateTimeUtils.dateNowTimestamp(propertyDTO.getTimezone()));
            importLog.setDataDate(DateTimeUtils.localDateToLong(DateTimeUtils.strToLocalDate(taImportResultDTO.getTheDate().toString(),
                    BaseConstant.DATE_FORMAT_YYYYMMDD), propertyDTO.getTimezone()));
            importLog.setImportResult(taImportResultDTO.getImportResult());
            importLog.setUrl("");
            importLog.setCreatedBy(BaseConstant.ZERO);
            importLog.setCreatedTime(DateTimeUtils.nowTimestamp());
            importLog.setResolvedBy(BaseConstant.ZERO);
            importLog.setResolvedTime(BaseConstant.LONG_ZERO);

            importLogService.save(importLog);

            log.info("TA internal - Save portal import log end, tenant:{}, property:{}", tenant, property);

            return Collections.emptyMap();
        }
    }