package com.zanxiang.game.data.serve.service.impl;

import com.zanxiang.game.data.serve.pojo.dto.ChoiceListDTO;
import com.zanxiang.game.data.serve.pojo.vo.AccountListVO;
import com.zanxiang.game.data.serve.service.IAccountListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tianhua
 * @version 1.0
 * @description: 查询所有广告账号的方法逻辑层
 * @date 2023/11/3 15:10
 */
@Service
@Slf4j
public class AccountListServiceImpl implements IAccountListService {

    @Autowired
    private Dao dao;

    /**
     * 获得所有广告账号
     * @param dto ChoiceListDTO
     * @return list
     */
    @Override
    public List<AccountListVO> getAccountList(ChoiceListDTO dto) {

        if (StringUtils.isBlank(dto.getSourceSystem())) {
            //默认查询“ZX_ONE”数据
            dto.setSourceSystem("ZX_ONE");
        }

        Sql sql = Sqls.create("""
                SELECT
                	source_system ,
                	account_id ,
                	MAX(account_type) as account_type
                from dm_game_order.t_pitcher_agent
                where ((account_type in('MP','GDT') and LENGTH(account_id)<= 10) or
                (account_type = 'BYTE' and LENGTH(account_id)<= 17))
                AND source_system = @source_system AND agent_name is not NULL
                GROUP BY account_id ,source_system 
                """);
        sql.setParam("source_system", dto.getSourceSystem());
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(AccountListVO.class));
        dao.execute(sql);

        return sql.getList(AccountListVO.class);
    }

}
