package com.hz.pm.api.dashboard.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.dashboard.constant.DashboardConstant;
import com.hz.pm.api.dashboard.handle.CockpitStatsHandler;
import com.hz.pm.api.dashboard.model.dto.CockpitApplicationDTO;
import com.hz.pm.api.dashboard.model.dto.CockpitStatsSaveDTO;
import com.hz.pm.api.dashboard.model.entity.CockpitApplication;
import com.hz.pm.api.dashboard.model.entity.CockpitStats;
import com.hz.pm.api.dashboard.model.vo.CockpitStatsVO;
import com.hz.pm.api.dashboard.service.ICockpitApplicationService;
import com.hz.pm.api.dashboard.service.ICockpitStatsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname CockpitStatsManage
 * @Description
 * @Date 2023/10/11 17:44
 * @Author PoffyZhang
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class CockpitStatsManage {

    private final ICockpitStatsService cockpitStatsService;

    private final CockpitStatsHandler convertCockpitStats;

    private final ICockpitApplicationService cockpitApplicationService;

    /**
     * 驾驶舱 统计数据
     * @param regionCode
     * @param year
     * @return
     */
    public CockpitStatsVO getData(String regionCode, Integer year) {
        CockpitStats cockpitStats = cockpitStatsService.getOne(Wrappers.lambdaQuery(CockpitStats.class)
                .eq(StringUtils.isNotBlank(regionCode), CockpitStats::getRegionCode, regionCode)
                .eq(StringUtils.isBlank(regionCode), CockpitStats::getRegionCode, DashboardConstant.CockpitStats.TOTAL)
                .eq(Objects.nonNull(year), CockpitStats::getYear, year)
                .eq(Objects.isNull(year), CockpitStats::getYear, DashboardConstant.CockpitStats.NONE_YEAR)
                .last(BizConst.LIMIT_1));

        if(Objects.isNull(cockpitStats)){
            return new CockpitStatsVO();
        }

        List<CockpitApplication> aps = cockpitApplicationService
                .list(Wrappers.lambdaQuery(CockpitApplication.class)
                .orderByAsc(CockpitApplication::getCreateOn));

        return  convertCockpitStats.convertCockpitStats(cockpitStats,regionCode,year,aps);
    }

    public void save(List<CockpitStatsSaveDTO> saves) {
        for(CockpitStatsSaveDTO save : saves){
            List<CockpitStats> cockpitStatsList = cockpitStatsService.list(Wrappers.lambdaUpdate(CockpitStats.class)
                    .eq(Objects.nonNull(save.getRegionCode()), CockpitStats::getRegionCode, save.getRegionCode())
                    .eq(Objects.nonNull(save.getYear()), CockpitStats::getYear, save.getYear()));
            if(CollUtil.isNotEmpty(cockpitStatsList)){
                for(CockpitStats cockpitStats  : cockpitStatsList){
                    BeanUtil.copyProperties(save,cockpitStats, CopyOptions.create().ignoreNullValue());
                    cockpitStatsService.updateById(cockpitStats);
                }
            }

            if(Objects.nonNull(save.getCockpitApplications())){
                cockpitApplicationService.remove(Wrappers.lambdaQuery(CockpitApplication.class));

                List<CockpitApplicationDTO> cockpitApplications = save.getCockpitApplications();
                if(CollUtil.isNotEmpty(cockpitApplications)){
                    List<CockpitApplication> applications = cockpitApplications.stream()
                            .map(a -> BeanUtil.copyProperties(a,CockpitApplication.class))
                            .collect(Collectors.toList());
                    cockpitApplicationService.saveBatch(applications);
                }
            }
        }
    }
}
