# SalesKpiWeeklyInternalController.java

    package cloud.wconnect.workperformance.controller.saleskpi.weekly;

    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.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.workperformance.pojo.dto.saleskpi.weekly.SalesKpiSalesmanItemDTO;
    import cloud.wconnect.workperformance.service.saleskpi.ISalesKpiFirstDayLogService;
    import cloud.wconnect.workperformance.service.saleskpi.ISalesKpiWeeklyAttachmentService;
    import cloud.wconnect.workperformance.service.saleskpi.ISalesKpiWeeklyService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.ImmutableMap;
    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.util.Collections;
    import java.util.Map;
    import java.util.Objects;

    /**
     * Controller：Sales KPI Weekly Internal
     *
     * @author zc
     * @date 2023/09/11
     */
    @Slf4j
    @RestController
    @RequestMapping("/internal/sales-kpi/weekly")
    public class SalesKpiWeeklyInternalController extends BaseInternalController {

        @Resource
        private ISalesKpiWeeklyService salesKpiWeeklyService;
        @Resource
        private ISalesKpiWeeklyAttachmentService salesKpiWeeklyAttachmentService;
        @Resource
        private ISalesKpiFirstDayLogService salesKpiFirstDayLogService;
        @Resource
        private PropertyInternalService propertyInternalService;
        @Resource
        private ObjectMapper objectMapper;

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

        /**
         * 获取周起始日
         *
         * @param tenant   租户标识
         * @param property 资产标识
         * @param sign     签名
         * @return 周起始日
         * @author zc
         * @date 2023/9/14
         */
        @GetMapping("/first-day")
        public Map<String, Integer> getFirstDayOfWeek(String tenant, String property, @RequestHeader String sign) {
            // 校验签名
            checkIntranetParams(authKey, sign, tenant, property);
            int propertyId = propertyInternalService.getOne(property, tenant).getId();
            int firstDay = salesKpiFirstDayLogService.getFirstDay(propertyId);
            return ImmutableMap.of("firstDay", firstDay);
        }

        /**
         * 保存业务员指标数据
         *
         * @param weekly          周（yyyyMMdd）
         * @param tenant          租户标识
         * @param property        资产标识
         * @param salesmanItemDTO 指标数据
         * @param sign            签名
         * @return empty map
         * @author zc
         * @date 2023/9/11
         */
        @PostMapping("/{weekly:\\d+}/salesman-data")
        public Map<Object, Object> saveSalesmanData(@PathVariable long weekly, String tenant, String property,
                                                    @RequestBody @Valid SalesKpiSalesmanItemDTO salesmanItemDTO,
                                                    @RequestHeader String sign) {
            log.info("Sales KPI Weekly internal - Save salesman data start, tenant:{}, property:{}, dataJson:{}",
                    tenant, property, JsonUtils.convertObjToString(objectMapper, salesmanItemDTO));

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

            PropertyDTO propertyDTO = propertyInternalService.getOne(property, tenant);
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(propertyDTO
                    .getInternationalization().getNumber().getCalculationRule());
            salesmanItemDTO.getData().forEach(item -> {
                if (Objects.nonNull(item.getValue())) {
                    item.setValue(item.getValue().setScale(BaseConstant.DATA_PRECISION, roundingMode));
                }
            });

            // 转换为 Weekly 所在周的周起始日
            long effectiveWeekly = salesKpiFirstDayLogService.conversionToFirstDayOfWeek(propertyDTO.getId(),
                    propertyDTO.getTimezone(), weekly);

            // 保存业务员指标数据
            salesKpiWeeklyService.saveSalesmanData(propertyDTO.getId(), effectiveWeekly, salesmanItemDTO.getData());

            // 保存采集报告 url
            salesKpiWeeklyAttachmentService.saveAttachmentByCrawl(effectiveWeekly, propertyDTO.getId(),
                    salesmanItemDTO.getReportUrl());

            log.info("Sales KPI Weekly internal - Save salesman data end, tenant:{}, property:{}", tenant, property);

            return Collections.emptyMap();
        }
    }