package com.xxl.job.executor.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 com.xxl.job.executor.common.entity.UserAllocation;
import com.xxl.job.executor.common.mapper.UserAllocationMapper;
import com.xxl.job.executor.common.service.IUserAllocationService;
import com.xxl.job.executor.utils.CtopAdConstant;
import com.xxl.job.executor.utils.ResultMapUtils;
import com.xxl.job.executor.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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 {

    @Autowired
    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 Map<String, Object> getPangolinAccountListByParams(String userId, JSONObject data) {
        Map<String, Object> result = new HashMap<>();
        Long projectId = data.getLong("project_id");
        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_id", CtopAdConstant.PLATFORM_TYPE_PANGOLIN);
        if (null != userId && !"".equals(userId.trim())) {
            queryWrapper.eq("user_id", userId);
        }
        if (null != projectId && projectId != 0) {
            queryWrapper.eq("project_id", projectId);
        }
        List<UserAllocation> allocations = this.list(queryWrapper);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        result.put("data", allocations);
        return result;
    }

    @Override
    public Map<String, Object> getPangolinProjectListByParams(String userId) {
        Map<String, Object> result = new HashMap<>();
        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct project_id,project_name");
        queryWrapper.eq("media_id", CtopAdConstant.PLATFORM_TYPE_PANGOLIN);
        if (null != userId && !"".equals(userId.trim())) {
            queryWrapper.eq("user_id", userId);
        }
        List<UserAllocation> allocations = this.list(queryWrapper);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        result.put("data", allocations);
        return result;
    }

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

    @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<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);
    }
}
