package com.neusoft.smart.pos.ota.persistent.repository;

import com.neusoft.smart.pos.ota.persistent.domain.ApkStrategy;
import com.neusoft.smart.pos.ota.persistent.domain.ApkStrategyExample;
import com.neusoft.smart.pos.ota.persistent.domain.ApkStrategyGroup;
import com.neusoft.smart.pos.ota.persistent.domain.ApkStrategyGroupExample;
import com.neusoft.smart.pos.ota.persistent.domain.ApkVersion;
import com.neusoft.smart.pos.ota.persistent.domain.ApkVersionExample;
import com.neusoft.smart.pos.ota.persistent.domain.AppInfo;
import com.neusoft.smart.pos.ota.persistent.domain.AppInfoExample;
import com.neusoft.smart.pos.ota.persistent.ext.domain.ApkStrategyQuery;
import com.neusoft.smart.pos.ota.persistent.ext.domain.AppQuery;
import com.neusoft.smart.pos.ota.persistent.mapper.ApkStrategyGroupMapper;
import com.neusoft.smart.pos.ota.persistent.mapper.ApkStrategyMapper;
import com.neusoft.smart.pos.ota.persistent.mapper.ApkVersionMapper;
import com.neusoft.smart.pos.ota.persistent.mapper.AppInfoMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class AppRepository {
    
    @Autowired
    private AppInfoMapper appInfoMapper;
    
    @Autowired
    private ApkVersionMapper apkVersionMapper;
    
    @Autowired
    private ApkStrategyGroupMapper apkStrategyGroupMapper;
    
    @Autowired
    private ApkStrategyMapper apkStrategyMapper;
    
    public List<AppInfo> getAppList(AppQuery appQuery) {
        AppInfoExample appInfoExample = this.appExampleProvider(appQuery);
        if (appQuery.getIsPage()) {
            appInfoExample.setLimit(appQuery.getLimit());
            appInfoExample.setOffset(appQuery.getOffset());
        }
        appInfoExample.setOrderByClause("create_time desc");
        return appInfoMapper.selectByExample(appInfoExample);
    }
    
    public Integer getAppCount(AppQuery appQuery) {
        AppInfoExample appInfoExample = this.appExampleProvider(appQuery);
        return appInfoMapper.countByExample(appInfoExample);
    }
    
    public AppInfo getAppById(Integer id) {
        return appInfoMapper.selectByPrimaryKey(id);
    }
    
    public void createApp(AppInfo appInfo) {
        appInfoMapper.insertSelective(appInfo);
    }
    
    public void updateApp(AppInfo appInfo) {
        appInfoMapper.updateByPrimaryKeySelective(appInfo);
    }
    
    public void createApk(ApkVersion apkVersion) {
        apkVersionMapper.insertSelective(apkVersion);
    }
    
    public Integer queryApkCount(Integer appId, String apkVersion) {
        ApkVersionExample apkVersionExample = new ApkVersionExample();
        apkVersionExample.createCriteria().andAppIdEqualTo(appId).andApkVersionEqualTo(apkVersion);
        return apkVersionMapper.countByExample(apkVersionExample);
    }
    
    public List<ApkVersion> getApkVersionList(Integer appId) {
        ApkVersionExample apkVersionExample = new ApkVersionExample();
        apkVersionExample.createCriteria().andAppIdEqualTo(appId);
        apkVersionExample.setOrderByClause("create_time desc");
        return apkVersionMapper.selectByExample(apkVersionExample);
    }
    
    public List<ApkVersion> getApkVersionListByIds(List<Integer> apkIds) {
        ApkVersionExample apkVersionExample = new ApkVersionExample();
        apkVersionExample.createCriteria().andIdIn(apkIds);
        return apkVersionMapper.selectByExample(apkVersionExample);
    }
    
    private AppInfoExample appExampleProvider(@NotNull AppQuery appQuery) {
        AppInfoExample appInfoExample = new AppInfoExample();
        AppInfoExample.Criteria criteria = appInfoExample.createCriteria();
        if (!StringUtils.isEmpty(appQuery.getAppName())) {
            criteria.andAppNameLike("%" + appQuery.getAppName() + "%");
        }
        if (!StringUtils.isEmpty(appQuery.getAppPackage())) {
            criteria.andAppPackageLike("%" + appQuery.getAppPackage() + "%");
        }
        if (!StringUtils.isEmpty(appQuery.getAppName2())) {
            criteria.andAppNameEqualTo(appQuery.getAppName2());
        }
        if (!StringUtils.isEmpty(appQuery.getAppPackage2())) {
            criteria.andAppPackageEqualTo(appQuery.getAppPackage2());
        }
        if (null != appQuery.getStatus()) {
            criteria.andStatusEqualTo(appQuery.getStatus());
        }
        if (null != appQuery.getId()) {
            criteria.andIdNotEqualTo(appQuery.getId());
        }
        if (null != appQuery.getIds()) {
            criteria.andIdIn(appQuery.getIds());
        }
        if (!"sa".equals(RequestHeaderUtil.getRole())) {
            criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
        }
        return appInfoExample;
    }
    
    
    public List<ApkStrategyGroup> getApkStrategyGroupList(ApkStrategyQuery apkStrategyQuery) {
        ApkStrategyGroupExample apkStrategyGroupExample = this.apkExampleProvider(apkStrategyQuery);
        apkStrategyGroupExample.setOrderByClause("create_time desc");
        apkStrategyGroupExample.setLimit(apkStrategyQuery.getLimit());
        apkStrategyGroupExample.setOffset(apkStrategyQuery.getOffset());
        return apkStrategyGroupMapper.selectByExample(apkStrategyGroupExample);
    }
    
    public Integer getApkStrategyGroupCount(ApkStrategyQuery apkStrategyQuery) {
        ApkStrategyGroupExample apkStrategyGroupExample = this.apkExampleProvider(apkStrategyQuery);
        return apkStrategyGroupMapper.countByExample(apkStrategyGroupExample);
    }
    
    private ApkStrategyGroupExample apkExampleProvider(@NotNull ApkStrategyQuery apkStrategyQuery) {
        ApkStrategyGroupExample apkStrategyGroupExample = new ApkStrategyGroupExample();
        ApkStrategyGroupExample.Criteria criteria = apkStrategyGroupExample.createCriteria();
        if (!StringUtils.isEmpty(apkStrategyQuery.getStrategyName())) {
            criteria.andNameLike("%" + apkStrategyQuery.getStrategyName() + "%");
        }
        if (!"sa".equals(RequestHeaderUtil.getRole())) {
            criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
        }
        return apkStrategyGroupExample;
    }
    
    public List<AppInfo> getAppsByGroupId(Integer groupId) {
        ApkStrategyExample example = new ApkStrategyExample();
        example.createCriteria().andGroupIdEqualTo(groupId);
        List<ApkStrategy> apkStrategyList = apkStrategyMapper.selectByExample(example);
        
        List<Integer> appIds = apkStrategyList.stream().map(ApkStrategy::getAppId).collect(Collectors.toList());
        AppInfoExample appInfoExample = new AppInfoExample();
        appInfoExample.createCriteria().andIdIn(appIds);
        return appInfoMapper.selectByExample(appInfoExample);
    }
    
    public Integer getStrategyCountByName(String strategyName) {
        ApkStrategyGroupExample apkStrategyGroupExample = new ApkStrategyGroupExample();
        apkStrategyGroupExample.createCriteria().andNameEqualTo(strategyName);
        return apkStrategyGroupMapper.countByExample(apkStrategyGroupExample);
    }
    
    public void createStrategyGroup(ApkStrategyGroup apkStrategyGroup) {
        apkStrategyGroupMapper.insertSelective(apkStrategyGroup);
    }
    
    public void createStrategy(ApkStrategy apkStrategy) {
        apkStrategyMapper.insertSelective(apkStrategy);
    }
    
    
    public List<ApkStrategy> getApkStrategies(Integer groupId) {
        ApkStrategyExample example = new ApkStrategyExample();
        example.createCriteria().andGroupIdEqualTo(groupId);
        return apkStrategyMapper.selectByExample(example);
    }
}
