package ctop.bytedance.ad.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ctop.bytedance.ad.common.entity.UserAllocation;
import ctop.bytedance.ad.common.mapper.UserAllocationMapper;
import ctop.bytedance.ad.common.service.IUserAllocationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户分配
 * @Author: jeecg-boot
 * @Date: 2019-08-08
 * @Version: V1.0
 */
@Service
public class UserAllocationServiceImpl extends ServiceImpl<UserAllocationMapper, UserAllocation> implements IUserAllocationService {

    @Resource
    UserAllocationMapper userAllocationMapper;

    @Override
    public UserAllocation getByAccountId(Long accountId) {
        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", accountId + "").orderByDesc("id").last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public List<UserAllocation> getByParams(Long projectId, String systemType, Integer accountStatus) {
        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
        if (null != projectId && projectId != 0) {
            queryWrapper.eq("project_id", projectId);
        }
        if (null != systemType && !"".equals(systemType.trim())) {
            queryWrapper.eq("system_type", systemType);
        }
        if (null != accountStatus) {
            queryWrapper.eq("account_status", accountStatus);
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    @Override
    public List<UserAllocation> getByMediaId(String mediaId) {
        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
        if ("".equals(mediaId)) {
            queryWrapper.eq("media_id", mediaId);
        }
        return this.list(queryWrapper);
    }

    @Override
    public List<UserAllocation> listByMediaId(String mediaId, int accountStatus) {
        return userAllocationMapper.listByMediaId(mediaId, accountStatus);
    }

    @Override
    public String getAdvertiserIdByAccountId(Long accountId) {
        return userAllocationMapper.queryAdvertiserId(accountId);
    }

    @Override
    public List<JSONObject> getAccountListByProject(Long projectId) {
        return userAllocationMapper.getAccountListByProject(projectId);
    }

    /**
     * 获取项目下的所有userId
     *
     * @param projectId
     * @return
     */
    @Override
    public List<JSONObject> getUserIdListByProjectId(Long projectId) {
        return userAllocationMapper.getUserIdListByProjectId(projectId);
    }

    @Override
    public List<JSONObject> getAccountIdListByUserId(Long userId) {
        return userAllocationMapper.getAccountIdListByUserId(userId);
    }

    @Override
    public List<JSONObject> getAccountIdsByUserId(String userId) {
        List<JSONObject> result = new ArrayList<>();
        if ("e9ca23d68d884d4ebb19d07889727dae".equals(userId)) {
            result = userAllocationMapper.getAllAccountIdsByMediaId("2");
        } else {
            result = userAllocationMapper.getAccountIdsByUserId(userId);

        }
        return result;
    }

    @Override
    public List<UserAllocation> getUserAllocations(String mediaId, int switchType) {
        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_id", mediaId);
        queryWrapper.eq("account_status", 0);
        queryWrapper.eq("monitoring_link", 0);
        queryWrapper.orderByDesc("create_time");
        return this.list(queryWrapper);
    }

    @Override
    public JSONObject getSwitchStatusByAccount(Long accountId) {
        return userAllocationMapper.getSwitchStatusByAccount(accountId);
    }

    @Override
    public List<Long> getAccountsByDeleteComment() {
        return userAllocationMapper.queryAccountsByDeleteComment();
    }

    @Override
    public Map<Long, List<JSONObject>> getAccountListByProjectList(List<JSONObject> projectList) {
        Map<Long, List<JSONObject>> map = new HashMap<>();
        List list = new ArrayList();
        for (int i = 0; i < projectList.size(); i++) {
            JSONObject jsonObject = projectList.get(i);
            list.add(jsonObject.getLong("projectId"));
            map.put(jsonObject.getLong("projectId"), new ArrayList<>());
        }
        List<JSONObject> jsonList = userAllocationMapper.getAccountListByProjectList(list);
        for (int i = 0; i < jsonList.size(); i++) {
            JSONObject jsonObject = jsonList.get(i);
            Long projectId = jsonObject.getLong("projectId");
            List<JSONObject> jsonObjects = map.get(projectId);
            jsonObjects.add(jsonObject);
        }
        return map;
    }
}
