package org.tio.sitexxx.service.service;

import org.tio.jfinal.plugin.activerecord.Db;
import org.tio.jfinal.plugin.activerecord.Record;
import org.tio.sitexxx.service.model.main.*;
import org.tio.sitexxx.service.vo.Const;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 *    @author : vagrancy
 *    e-mail : 18050829067@163.com
 *    time   : 2026-02-25 16:26
 *    desc   : 任务服务
 * </pre>
 */
public class TaskService {
    public static final TaskService me = new TaskService();

    public UserTask getOne(Integer uid){
        return UserTask.dao.findFirst("select * from user_task where uid = "+uid+" limit 1");
    }

    public List<Task> getList() {
        return Task.dao.find("select * from task where status = 1");
    }

    /**
     * 当天数量
     * @param uid 用户序号
     * @param taskId 任务序号
     * @return 结果
     */
    public int getDayCount(Integer uid, Integer taskId) {
        Record record = Db.use(Const.Db.TIO_SITE_MAIN)
                .findFirst("select count(*) as count from task_record where uid = "+uid+" and taskId ="+taskId+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY)");
        return record.getInt("count");
    }

    /**
     * 当天数量
     * @param uid 用户序号
     * @param taskId 任务序号
     * @return 结果
     */
    public int getDayCount(Integer uid, Integer taskId, String deviceId, String ip) {
        Record record = Db.use(Const.Db.TIO_SITE_MAIN)
                .findFirst("select count(*) as count from task_record where ip = "+ip+" and deviceId = "+deviceId+" and uid = "+uid+" and taskId ="+taskId+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY)");
        return record.getInt("count");
    }


    public int getDayCountByIp(Integer taskId, String ip) {
        Record record = Db.use(Const.Db.TIO_SITE_MAIN)
                .findFirst("select count(*) as count from task_record where ip = '"+ip+"' and taskId ="+taskId+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY)");
        return record.getInt("count");
    }

    public int getDayCountByDeviceId(Integer taskId, String deviceId) {
        Record record = Db.use(Const.Db.TIO_SITE_MAIN)
                .findFirst("select count(*) as count from task_record where deviceId = '"+deviceId+"' and taskId ="+taskId+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY)");
        return record.getInt("count");
    }

    /**
     * 添加任务记录
     * @param uid 用户序号
     * @param taskId 任务序号
     * @param adType 广告类型
     * @param ecpm 价格
     * @param sdkName sdk名称
     * @param transId 地位id
     */
    public void addTaskRecord(Integer taskId, Integer uid, String pCode, Integer adType, String ecpm, String sdkName,
                              String transId, Integer mediationRit) {
        TaskRecord model = new TaskRecord();
        model.setStatus(1);
        model.setCoin(ecpm);
        model.setCtime(new Date());
        model.setAdType(adType);
        model.setTaskId(taskId);
        model.setPCode(pCode);
        model.setUtime(new Date());
        model.setUid(uid);
        model.setSdkName(sdkName);
        model.setTransId(transId);
        model.setMediationRit(mediationRit);
        model.setType(Const.TaskType.AD);
        model.save();
    }

    /**
     * 通过邀请添加任务记录
     * @param taskId 任务序号
     * @param uid 用户徐海
     * @param coin 硬币
     */
    public void addTaskRecordByInvite(Integer taskId, Integer uid, String coin, String deviceId, String ip) {
        TaskRecord model = new TaskRecord();
        model.setUid(uid);
        model.setTaskId(taskId);
        model.setStatus(1);
        model.setCoin(coin);
        model.setAdType(Const.AdType.NONE);
        model.setSdkName("");
        model.setTransId("");
        model.setMediationRit(0);
        model.setType(Const.TaskType.INVITE);
        model.setCtime(new Date());
        model.setUtime(new Date());
        model.setIp(ip);
        model.setDeviceId(deviceId);
        model.save();
    }

    /**
     * 获取任务
     * @param adId 广告序号
     * @return
     */
    public Task getTaskByAdId(String adId, Integer type) {
        return Task.dao.findFirst("select * from task where adId = "+adId+" and adType = "+type+" limit 1");
    }

    /**
     * 获取任务记录
     * @param uid 用户序号
     * @param transId 交易序号
     * @return
     */
    public TaskRecord getTaskRecord(int uid, String transId, int type) {
        return TaskRecord.dao.findFirst("select * from task_record where uid = '"+uid+"' and transId ='"+transId+"' and adType = "+type+" limit 1");
    }

    /**
     * 添加用户任务记录
     * @param uid 用户序号
     * @param beforeCoin 之前硬币
     * @param coinCount 硬币
     * @param afterCoin 之后硬币
     */
    public void addUserTaskRecord(int uid, BigDecimal beforeCoin, BigDecimal coinCount, BigDecimal afterCoin) {
        UserTaskRecord record = new UserTaskRecord();
        record.setAfterCoin(afterCoin);
        record.setBeforeCoin(beforeCoin);
        record.setCoin(coinCount);
        record.setUid(uid);
        record.setStatus(1);
        record.setCtime(new Date());
        record.setUtime(new Date());
        record.save();
    }


    public long getNew(Integer taskId, int uid) {
        Record record = Db.use(Const.Db.TIO_SITE_MAIN)
                .findFirst("select ctime from task_record where uid = "+uid+" and taskId ="+taskId+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY) order by ctime desc limit 1");
        if (record == null) {
            return 0;
        }
        return record.getDate("ctime").getTime();
    }

    /**
     * 获取任务
     * @param taskId 任务序号
     * @return 结果
     */
    public Task getTask(Integer taskId) {
        return Task.dao.findFirst("select * from task where id = "+taskId);
    }

    /**
     * 通过邀请获取任务记录
     * @param uid 用户序号
     * @param taskId 任务序号
     * @return 结果
     */
    public TaskRecord getTaskRecordByInvite(Integer uid, Integer taskId) {
        return TaskRecord.dao.findFirst("select * from task_record where uid = '"+uid+"' and taskId ='"+taskId+"' and type = "+Const.TaskType.INVITE+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY) limit 1");
    }

    /**
     * 通过邀请获取数量
     * @param pid 父id
     * @return
     */
    public int getDayCountByInvite(Integer pid, String pCode) {
        //获取uid
        List<TaskDoneInvite> list = TaskDoneInvite.dao.find("select * from task_done_invite where uid = "+pid);
        String sql;
        if (list.isEmpty()) {
            sql = "select count(*) as count from task_record where pCode = '"+pCode+"' and type = "+Const.TaskType.AD+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY)";
        } else {
            String str = "";
            if (list.size() == 1) {
                str = ""+list.get(0).getTwoUid();
            } else {
                for (int i = 0; i < list.size(); i++) {
                    str = list.get(i).getTwoUid()+",";
                    if (i != list.size() -1) {
                        str += ",";
                    }
                }
            }
            sql = "select count(*) as count from task_record where pCode = '"+pCode+"' and uid not in ("+str+") and type = "+Const.TaskType.AD+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY)";
        }
        //移除uid
        Record record = Db.use(Const.Db.TIO_SITE_MAIN).findFirst(sql);
        return record.getInt("count");
    }

    /**
     * 通过邀请获取数量
     * @param pid 父id
     * @return
     */
    public List<Record> getDayByInvite(Integer pid) {
        //获取uid
        List<TaskDoneInvite> list = TaskDoneInvite.dao.find("select * from task_done_invite where uid = "+pid);
        String sql;
        if (list.isEmpty()) {
            sql = "select * from task_record where type = "+Const.TaskType.AD+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY) group by uid";
        } else {
            String str = "";
            for (TaskDoneInvite invite : list) {
                str = invite.getTwoUid()+",";
            }
            sql = "select * from task_record where uid not in ("+str+") type = "+Const.TaskType.AD+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY) group by uid";
        }
        //移除uid
        return Db.use(Const.Db.TIO_SITE_MAIN).find(sql);
    }

    /**
     * 添加任务完整邀请
     * @param uid 用户序号
     * @param twoUid 对方序号
     */
    public void addTaskDoneInvite(Integer uid, Integer twoUid) {
        TaskDoneInvite model = new TaskDoneInvite();
        model.setCtime(new Date());
        model.setTwoUid(twoUid);
        model.setUid(uid);
        model.setStatus(1);
        model.setUtime(new Date());
        model.save();
    }

    /**
     * 获取天数是否完成
     * @param uid 用户序号
     * @param taskId 任务序号
     * @return 结果
     */
    public int getDayCountByDoneInvite(Integer uid, Integer taskId) {
        Record record =  Db.use(Const.Db.TIO_SITE_MAIN).findFirst("select count(*) as count from task_record where uid = '"+uid+"' and taskId ='"+taskId+"' and type = "+Const.TaskType.INVITE+" and ctime >= CURDATE() AND ctime < DATE_ADD(CURDATE(), INTERVAL 1 DAY) limit 1");
        return record.getInt("count") > 0 ? 1 : 0;
    }
}
