package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.dictionary.*;
import com.lightinit.hsdataportal.entity.ResourceAuth;
import com.lightinit.hsdataportal.entity.ResourceAuthExample;
import com.lightinit.hsdataportal.entity.ResourceFollow;
import com.lightinit.hsdataportal.mapper.ResourceAuthMapper;
import com.lightinit.hsdataportal.mapper.ResourceFollowMapper;
import com.lightinit.hsdataportal.model.FollowDataListModel;
import com.lightinit.hsdataportal.model.MyDataShareListModel;
import com.lightinit.hsdataportal.model.ResourceAuthMapperDemo;
import com.lightinit.hsdataportal.service.IMyDataService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/6/15 13:25
 * @Description:
 **/
@Service
public class MyDataServiceImpl implements IMyDataService {

    @Autowired
    private ResourceAuthMapper resourceAuthMapper;
    @Autowired
    private ResourceFollowMapper resourceFollowMapper;

    @Override
    public List<MyDataShareListModel> getIndex(Long resourceAuthId,Long applierId, String platformType) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResourceAuthMapperDemo resourceAuthMapperDemo = new ResourceAuthMapperDemo();
        resourceAuthMapperDemo.setApplierId(applierId);                 //id
        resourceAuthMapperDemo.setResourceType(DicResourceType.DATA);   //数据
        resourceAuthMapperDemo.setResourceAuthId(resourceAuthId);
        if(DicPlatformType.OPEN.equals(platformType)){
            resourceAuthMapperDemo.setPlatformType(DicPlatformType.OPEN);   //开放平台
            resourceAuthMapperDemo.setShareOrOpenType(DicResourceOpenType.NOT_OPEN);    //'不开放'
            resourceAuthMapperDemo.setOpenType(DicPlatformType.OPEN);
            resourceAuthMapperDemo.setShareType(null);
            List<MyDataShareListModel> list=resourceAuthMapper.selectMyDataOpenListModel(resourceAuthMapperDemo);
            return list;
        }else{
            resourceAuthMapperDemo.setPlatformType(DicPlatformType.SHARE);   //共享平台
            resourceAuthMapperDemo.setShareOrOpenType(DicResourceShareType.NOT_SHARE);    //'不共享'
            resourceAuthMapperDemo.setShareType(DicPlatformType.SHARE);
            resourceAuthMapperDemo.setOpenType(null);
            List<MyDataShareListModel> list=resourceAuthMapper.selectMyDataOpenListModel(resourceAuthMapperDemo);
            return list;
        }
    }

    @Override
    public MyDataShareListModel selectResourceAuth(Long applierId,Long id,String platformType) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResourceAuthMapperDemo resourceAuthMapperDemo = new ResourceAuthMapperDemo();
        resourceAuthMapperDemo.setApplierId(applierId);                 //id
        resourceAuthMapperDemo.setResourceType(DicResourceType.DATA);   //数据
        resourceAuthMapperDemo.setResourceAuthId(id);
        List<MyDataShareListModel> list=null;
        if(DicPlatformType.OPEN.equals(platformType)){
            resourceAuthMapperDemo.setPlatformType(DicPlatformType.OPEN);   //开放平台
            resourceAuthMapperDemo.setShareOrOpenType(DicResourceOpenType.NOT_OPEN);    //'不开放'
            resourceAuthMapperDemo.setOpenType(DicPlatformType.OPEN);
            resourceAuthMapperDemo.setShareType(null);
            list=resourceAuthMapper.selectMyDataOpenListModel(resourceAuthMapperDemo);
        }else{
            resourceAuthMapperDemo.setPlatformType(DicPlatformType.SHARE);   //共享平台
            resourceAuthMapperDemo.setShareOrOpenType(DicResourceShareType.NOT_SHARE);    //'不予共享'
            resourceAuthMapperDemo.setShareType(DicPlatformType.SHARE);
            resourceAuthMapperDemo.setOpenType(null);
           list=resourceAuthMapper.selectMyDataOpenListModel(resourceAuthMapperDemo);
        }
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<FollowDataListModel> getFollowDateList(Long applierId, String platformType) {
        ResourceAuthMapperDemo resourceAuthMapperDemo = new ResourceAuthMapperDemo();//条件相同，复用此model
        List<FollowDataListModel> list=null;
        resourceAuthMapperDemo.setResourceType(DicResourceType.DATA);   //数据
        resourceAuthMapperDemo.setApplierId(applierId);//用户id
        if(DicPlatformType.OPEN.equals(platformType)){
            resourceAuthMapperDemo.setPlatformType(DicPlatformType.OPEN);   //开放平台
            resourceAuthMapperDemo.setShareOrOpenType(DicResourceOpenType.NOT_OPEN);    //'不开放'
            resourceAuthMapperDemo.setOpenType(DicPlatformType.OPEN);
            resourceAuthMapperDemo.setShareType(null);
            list=resourceFollowMapper.getFollowDateList(resourceAuthMapperDemo);
        }else{
            resourceAuthMapperDemo.setPlatformType(DicPlatformType.SHARE);   //共享平台
            resourceAuthMapperDemo.setShareOrOpenType(DicResourceShareType.NOT_SHARE);    //'不予共享'
            resourceAuthMapperDemo.setShareType(DicPlatformType.SHARE);
            resourceAuthMapperDemo.setOpenType(null);
            list=resourceFollowMapper.getFollowDateList(resourceAuthMapperDemo);
        }
       return  list;
    }

    @Override
    public Long getResourceAuthId(Long id, Long userId, String platformType) {
        ResourceAuthExample resourceAuthExample = new ResourceAuthExample();
        resourceAuthExample.createCriteria().andPublishSettingIdEqualTo(id).andPlatformTypeEqualTo(platformType).andApplierEqualTo(userId);
        List<ResourceAuth> resourceAuths = resourceAuthMapper.selectByExample(resourceAuthExample);
        if (resourceAuths != null && resourceAuths.size() > 0) {
            return resourceAuths.get(0).getId();
        }
        return null;
    }
}
