# SalesWidgetServiceImpl.java

    package cloud.wconnect.workperformance.service.mor.impl;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.YearMonthUtils;
    import cloud.wconnect.workperformance.entity.mor.Sales;
    import cloud.wconnect.workperformance.pojo.dto.mor.sales.widget.SalesAmountDTO;
    import cloud.wconnect.workperformance.pojo.dto.mor.sales.widget.SalesAmountsDTO;
    import cloud.wconnect.workperformance.pojo.query.mor.sales.SalesAmountQuery;
    import cloud.wconnect.workperformance.service.mor.ISalesService;
    import cloud.wconnect.workperformance.service.mor.ISalesWidgetService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.math.BigDecimal;
    import java.time.YearMonth;
    import java.time.format.DateTimeFormatter;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.function.Function;
    import java.util.stream.Collectors;

    /**
     * Service: widget
     *
     * @author zc
     * @date 2023/12/09
     */
    @Service
    @DS("work_performance")
    public class SalesWidgetServiceImpl implements ISalesWidgetService {

        @Autowired
        private ISalesService salesService;

        /**
         * 获取销售金额
         *
         * @param propertyList 资产
         * @param query        查询条件
         * @return SalesAmountsDTO 集合
         * @author zc
         * @date 2023/12/9
         */
        @Override
        public List<SalesAmountsDTO> listAmounts(List<PropertyDTO> propertyList, SalesAmountQuery query) {
            if (CollectionUtils.isEmpty(propertyList)) {
                return Collections.emptyList();
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(BaseConstant.DATE_FORMAT_YYYY_MM);

            return propertyList.stream().map(property -> {
                Map<Integer, Sales> month2Sales = salesService.list(new QueryWrapper<Sales>().lambda()
                                .eq(Sales::getPropertyId, property.getId())
                                .between(Sales::getTheMonth, query.getStartDate(), query.getEndDate())).stream()
                        .collect(Collectors.toMap(Sales::getTheMonth, Function.identity()));

                YearMonth startYearMonth = YearMonthUtils.getYearMonth(query.getStartDate().toString());
                YearMonth endYearMonth = YearMonthUtils.getYearMonth(query.getEndDate().toString());

                List<SalesAmountDTO> values = Lists.newArrayList();
                for (YearMonth currentYearMonth = startYearMonth; !currentYearMonth.isAfter(endYearMonth);
                     currentYearMonth = currentYearMonth.plusMonths(1)) {
                    Sales sales = month2Sales.get((int) YearMonthUtils.parseYearMonthToLong(currentYearMonth));
                    BigDecimal value = null;
                    if (Objects.nonNull(sales)) {
                        value = Objects.nonNull(sales.getManualSalesTotal()) ? sales.getManualSalesTotal() : sales.getReportSalesTotal();
                    }
                    values.add(SalesAmountDTO.builder()
                            .date(currentYearMonth.format(formatter))
                            .value(value)
                            .build());
                }

                return SalesAmountsDTO.builder()
                        .propertyId(property.getId())
                        .propertyName(property.getAbbr())
                        .values(values)
                        .build();
            }).toList();
        }
    }