package com.kym.service.admin.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.github.pagehelper.PageHelper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kym.common.utils.CommUtil;
import com.kym.entity.BaseEntity;
import com.kym.entity.admin.AdminUserStation;
import com.kym.entity.admin.Station;
import com.kym.entity.admin.queryParams.CommonQueryParam;
import com.kym.entity.common.PageBean;
import com.kym.mapper.admin.AdminUserStationMapper;
import com.kym.service.admin.AdminUserStationService;
import com.kym.service.admin.StationService;
import com.kym.service.cache.KymCache;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 操作员站点关联表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2023-09-18
 */
@Service
@DS("db-admin")
public class AdminUserStationServiceImpl extends MPJBaseServiceImpl<AdminUserStationMapper, AdminUserStation> implements AdminUserStationService {

    @Resource
    private StationService stationService;

    @PostConstruct
    private void init() {
        DynamicDataSourceContextHolder.push("db-admin");
        var adminUser2Stations = list().stream().collect(Collectors.toMap(AdminUserStation::getAdminUserId,
                adminUserStation -> {
                    List<String> stationIds = new ArrayList<>();
                    stationIds.add(adminUserStation.getStationId());
                    return stationIds;
                },
                (v1, v2) -> {
                    v1.addAll(v2);
                    return v1;
                }
        ));

        KymCache.INSTANCE.putAdminUser2Stations(adminUser2Stations);
        DynamicDataSourceContextHolder.poll();
    }

    @Override
    public PageBean<AdminUserStation> listUserStation(CommonQueryParam commonQueryParam) {
        PageHelper.startPage(commonQueryParam.getPageNum(), commonQueryParam.getPageSize());
        List<AdminUserStation> list = lambdaQuery().eq(commonQueryParam.getUserId() > 0, AdminUserStation::getAdminUserId, commonQueryParam.getUserId()).list();
        return new PageBean<>(list);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveUserStation(List<AdminUserStation> userStationList) {
        List<Station> list = stationService.list();
        Map<String, String> stationMap = list.stream().collect(Collectors.toMap(k -> String.valueOf(k.getStationId()), Station::getStationName));
        userStationList.forEach(k -> {
                    CommUtil.asserts(k.getAdminUserId() > 0, "管理用户ID不能为空");
                    CommUtil.assertsNonNull(k.getStationId(), "站点信息不能为空");
                    String name = stationMap.get(k.getStationId());
                    CommUtil.assertsNonNull(name, "站点信息不存在");
                    k.setStationName(name);
                }
        );
        Map<String, Object> deleteMap = Map.of("admin_user_id", userStationList.get(0).getAdminUserId());
        removeByMap(deleteMap);
        saveBatch(userStationList);
        //刷新缓存
        Map<Long, List<String>> userStationMap = userStationList.stream().collect(Collectors.groupingBy(AdminUserStation::getAdminUserId, Collectors.mapping(AdminUserStation::getStationId, Collectors.toList())));
        KymCache.INSTANCE.putAdminUser2Stations(userStationMap);
    }
}
