package com.sckw.system.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.FileUtils;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.UserCacheResDto;
import com.sckw.system.dao.SysVersionDao;
import com.sckw.system.model.SysVersion;
import com.sckw.system.model.vo.req.FindVersionListReqVo;
import com.sckw.system.model.vo.res.FindVersionListResVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 版本管理service接口
 * @author zk
 * @date 2023-05-31
 */
@Service
public class SysVersionService {

    @Autowired
    SysVersionDao sysVersionDao;

    @Autowired
    RemoteSystemService remoteSystemService;

    public PageResult findPage(FindVersionListReqVo reqVo) {
        PageHelper.startPage(reqVo.getPage(), reqVo.getPageSize());
        List<SysVersion> list = sysVersionDao.findList(reqVo);
        if(CollectionUtils.isEmpty(list)) {
            return PageHelperUtil.getPageResult(new PageInfo<>());
        }

        List<FindVersionListResVo> findVersionListResVos = BeanUtils.copyToList(list, FindVersionListResVo.class);
        List<Long> createByList = findVersionListResVos.stream().map(FindVersionListResVo::getCreateBy).collect(Collectors.toList());
        List<Long> updateByList = findVersionListResVos.stream().map(FindVersionListResVo::getUpdateBy).toList();
        createByList.addAll(updateByList);
        Map<Long, UserCacheResDto> longUserCacheResDtoMap = remoteSystemService.queryUserCacheMapByIds(createByList);
        findVersionListResVos.forEach(item -> {
            UserCacheResDto userCacheResDto = longUserCacheResDtoMap.get(item.getCreateBy());
            if (Objects.nonNull(userCacheResDto)) {
                item.setCreateByName(userCacheResDto.getName());
            }

            UserCacheResDto userCacheResDto1 = longUserCacheResDtoMap.get(item.getUpdateBy());
            if (Objects.nonNull(userCacheResDto1)) {
                item.setUpdateByName(userCacheResDto1.getName());
            }

            item.setFilePath(FileUtils.splice(item.getFilePath()));
        });

        return PageHelperUtil.getPageResult(new PageInfo<>(findVersionListResVos), list, reqVo.getPageSize());
    }


    /**
     * @param clientType 客户端类型
     * @return HttpResult
     * @desc: 查最新的版本信息
     * @author: czh
     * @date: 2023/10/10
     */
    public FindVersionListResVo findLatestVersion(Integer systemType, String clientType) {
        SysVersion sysVersion = sysVersionDao.findLatestVersion(systemType, clientType);
        if (Objects.isNull(sysVersion)) {
            return null;
        }

        FindVersionListResVo findVersionListResVo = new FindVersionListResVo();
        BeanUtils.copyProperties(sysVersion, findVersionListResVo);
        List<Long> userIdList = new ArrayList<>();
        userIdList.add(sysVersion.getCreateBy());
        userIdList.add(sysVersion.getUpdateBy());
        Map<Long, UserCacheResDto> longUserCacheResDtoMap = remoteSystemService.queryUserCacheMapByIds(userIdList);
        UserCacheResDto userCacheResDto = longUserCacheResDtoMap.get(sysVersion.getCreateBy());
        if (Objects.nonNull(userCacheResDto)) {
            findVersionListResVo.setCreateByName(userCacheResDto.getName());
        }

        UserCacheResDto userCacheResDto2 = longUserCacheResDtoMap.get(sysVersion.getUpdateBy());
        if (Objects.nonNull(userCacheResDto2)) {
            findVersionListResVo.setUpdateByName(userCacheResDto2.getName());
        }
        findVersionListResVo.setFilePath(FileUtils.splice(findVersionListResVo.getFilePath()));
        return findVersionListResVo;
    }

}
