package com.ssj.service.sys.appinfo.service.impl;

import com.ssj.bean.sys.appinfo.bean.AppInfo;
import com.ssj.dao.sys.appinfo.dao.AppInfoDao;
import com.ssj.dao.sys.appinfo.dao.AppInfoQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.sys.appinfo.service.AppInfoService;
import com.ssj.service.sys.appinfo.service.dto.LastVersionAppInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AppInfoServiceImpl extends BaseServiceImpl<AppInfo, String> implements AppInfoService {

    @Autowired
    private AppInfoDao appInfoDao;
    @Autowired
    private AppInfoQueryDao appInfoQueryDao;

    @Override
    public AppInfo latestVersion(int type) {
        List<AppInfo> appInfos = appInfoDao.findByVersionTypeAndTypeOrderByCreateTimeDescIssueNumDesc("RELEASE",type);
        if (appInfos != null && appInfos.size() > 0) {
            return appInfos.get(0);
        } else {
            return null;
        }
    }

    @Override
    public AppInfo getAppInfo(AppInfo appInfo) {
        // TODO Auto-generated method stub
        // appInfo = appInfoDAO.selectOne(appInfo);
        return null;
    }

    @Override
    public AppInfo getAppInfo(String version, String versionType, Long issueNum) {
        // TODO Auto-generated method stub
        AppInfo appInfo = new AppInfo();
        appInfo.setVersion(version);
        appInfo.setVersionType(versionType);
        appInfo.setIssueNum(issueNum);
        appInfo = this.getAppInfo(appInfo);
        return appInfo;
    }

    @Override
    public PagingAndSortingRepository<AppInfo, String> getDao() {
        return appInfoDao;
    }

    @Override
    public Page<AppInfo> findByPage(Map<String, Object> params, Pageable sp) {
        Map<String, SearchFilter> filters = SearchFilter.parse(params);
        Specification<AppInfo> specification = DynamicSpecifications.bySearchFilter(filters.values(), AppInfo.class);
        return  appInfoDao.findAll(specification,sp);
    }

	@Override
	public AppInfo findTopByVersionAndType(String version, int type) {
		return appInfoDao.findTopByVersionAndType(version,type);
	}

    @Override
    public List<LastVersionAppInfoDto> findLastPackageByTypes(List<Integer> types) {
        List<LastVersionAppInfoDto> list = appInfoQueryDao.findLastPackageByTypes(types, "RELEASE");
        System.out.println("12312312");
        return list;
//        return appInfoQueryDao.findLastPackageByTypes(types, "RELEASE");
    }

    @Override
    public ResponseEntity findNewLastNewPackageByType(int type) {
        ResponseEntity responseEntity=new ResponseEntity();
        AppInfo appInfo = appInfoDao.findLastPackageByVersionTypeAndType("RELEASE", type);
        Map<String,Object> data=new HashMap<>();
        data.put("fileKey",appInfo.getFileKey());
        data.put("version",appInfo.getVersion());
        responseEntity.success(data,"成功");
        return  responseEntity;
    }
}
