package org.jeecg.modules.fileupload.service.impl;

import cn.com.ctop.common.module.utils.Check;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.modules.fileupload.mapper.MaterialUsrServiceMapper;
import org.jeecg.modules.fileupload.service.IMaterialUsrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialUsrServiceImpl implements IMaterialUsrService {
    @Autowired
    private MaterialUsrServiceMapper materialUsrServiceMapper;


    @Override
    public JSONObject getMaterialUseInfo(String code) {
        JSONObject returnJson = new JSONObject();
        List<JSONObject> kuaishouInfo = this.getKuaiShouMaterialInfo(code);
        returnJson.put("kuaishou", kuaishouInfo);
        List<JSONObject> byteDanceInfo = this.getbyteDanceMaterialInfo(code);
        returnJson.put("byteDance", byteDanceInfo);
        return returnJson;


    }


    private List<JSONObject> getKuaiShouMaterialInfo(String code) {
        List<JSONObject> accountInfos = materialUsrServiceMapper.getAccountInfo(code);
        if (Check.isNull(accountInfos)) {
            return null;
        }
        for (int i = 0; i < accountInfos.size(); i++) {
            JSONObject accountJson = accountInfos.get(i);
            Long accountId = accountJson.getLong("accountId");
            String photoId = accountJson.getString("photoId");
            Integer relationCount = materialUsrServiceMapper.getKuiShouRelationCount(accountId, photoId);
            accountJson.put("relationCount", relationCount);
            JSONObject refuseInfo = materialUsrServiceMapper.getKuiShouRefuseRelationCount(accountId, photoId);
            accountJson.put("refuseInfo", refuseInfo);
        }


        return accountInfos;
    }


    private List<JSONObject> getbyteDanceMaterialInfo(String code) {
        List<JSONObject> accountInfos = materialUsrServiceMapper.getByteDanceAccountInfo(code);
        if (Check.isNull(accountInfos)) {
            return null;
        }
        for (int i = 0; i < accountInfos.size(); i++) {
            JSONObject accountJson = accountInfos.get(i);
            String accountId = accountJson.getString("accountId");
            String vid = accountJson.getString("vid");
            Integer relationCount = materialUsrServiceMapper.getByteDanceRelationCount(accountId, vid);
            accountJson.put("relationCount", relationCount);
        }


        return accountInfos;
    }


}
