# MarketSegmentServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.cons.monthlyreport.AreaEnum;
    import cloud.wconnect.workperformance.entity.monthlyreport.*;
    import cloud.wconnect.workperformance.mapper.monthlyreport.MarketSegmentMapper;
    import cloud.wconnect.workperformance.mapper.monthlyreport.ReportRemarkMapper;
    import cloud.wconnect.workperformance.pojo.dto.monthlyreport.marketsegment.MarketSegmentDTO;
    import cloud.wconnect.workperformance.pojo.dto.monthlyreport.managerscorecard.MarketSegmentDetailDTO;
    import cloud.wconnect.workperformance.service.monthlyreport.IHandlePropertySettingChange;
    import cloud.wconnect.workperformance.service.monthlyreport.IMarketSegmentService;
    import cloud.wconnect.workperformance.service.monthlyreport.IReportRemarkService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.List;

    /**
     * <p>
     * 月报Market Mix区域表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2023-09-01
     */
    @Service
    @DS("monthly_report")
    public class MarketSegmentServiceImpl extends ServiceImpl<MarketSegmentMapper, MarketSegment> implements IMarketSegmentService, IHandlePropertySettingChange {

        @Autowired
        private IReportRemarkService reportRemarkService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private ReportRemarkMapper reportRemarkMapper;

        /**
         * 查询Market Mix区域详情
         *
         * @param monthlyReport 月报
         * @param propertyDTO   资产DTO
         * @return cloud.wconnect.workperformance.pojo.dto.monthlyreport.managerscorecard.MarketSegmentDetailDTO
         * @author lichen
         * @date 2023/9/2
         */
        @Override
        public MarketSegmentDetailDTO getMarketMix(MonthlyReport monthlyReport, PropertyDTO propertyDTO) {
            //查询备注
            String remark = reportRemarkService.listByReportIdAndAreaId(monthlyReport.getId(), AreaEnum.MARKET_MIX.getAreaId());

            List<MarketSegmentDTO> marketSegmentDTOList = list(new LambdaQueryWrapper<MarketSegment>()
                    .eq(MarketSegment::getReportId, monthlyReport.getId())
                    .eq(MarketSegment::getPropertyId, propertyDTO.getId())
                    .orderByAsc(MarketSegment::getId))
                    .stream().map(item -> beanMapper.map(item, MarketSegmentDTO.class)).toList();

            return new MarketSegmentDetailDTO(marketSegmentDTOList, remark);
        }

        /**
         * 保存Market Mix区域数据
         *
         * @param monthlyReport          月报
         * @param propertyDTO            资产DTO
         * @param marketSegmentDetailDTO 区域信息
         * @param loginUserId            登录用户id
         * @return void
         * @author lichen
         * @date 2023/9/2
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(MonthlyReport monthlyReport, PropertyDTO propertyDTO, MarketSegmentDetailDTO marketSegmentDetailDTO, int loginUserId) {
            remove(new LambdaQueryWrapper<MarketSegment>()
                    .eq(MarketSegment::getReportId, monthlyReport.getId())
                    .eq(MarketSegment::getPropertyId, propertyDTO.getId()));

            //保存区域信息
            List<MarketSegment> marketSegments = beanMapper.mapList(marketSegmentDetailDTO.getList(), MarketSegment.class, (dto, entity) -> {
                entity.setTenantId(propertyDTO.getTenantId());
                entity.setPropertyId(propertyDTO.getId());
                entity.setReportId(monthlyReport.getId());
                entity.setYear(monthlyReport.getYear());
                entity.setMonth(monthlyReport.getMonth());
                entity.setLastYear(monthlyReport.getYear() - 1);
                entity.setUpdateId(loginUserId);
                entity.setUpdateTime(DateTimeUtils.nowTimestamp());
            });
            saveBatch(marketSegments);

            //保存备注
            ReportRemark reportRemark = ReportRemark.builder()
                    .tenantId(propertyDTO.getTenantId())
                    .propertyId(propertyDTO.getId())
                    .reportId(monthlyReport.getId())
                    .areaId(AreaEnum.MARKET_MIX.getAreaId())
                    .remark(marketSegmentDetailDTO.getRemark())
                    .build();
            reportRemarkMapper.saveOrUpdateByUnique(reportRemark);
        }

        /**
         * 删除区域数据
         *
         * @param propertyDTO    资产DTO
         * @param monthlyReports 月报集合
         * @return void
         * @author lichen
         * @date 2023/9/5
         */
        @Override
        public void delete(PropertyDTO propertyDTO, List<MonthlyReport> monthlyReports) {
            List<Long> reportIds = monthlyReports.stream().map(MonthlyReport::getId).toList();
            if (CollectionUtils.isEmpty(reportIds)) return;
            remove(new LambdaQueryWrapper<MarketSegment>()
                    .in(MarketSegment::getReportId, reportIds)
                    .eq(MarketSegment::getPropertyId, propertyDTO.getId()));
        }
    }