package org.jeecg.modules.bytedance.advertise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.bytedance.advertise.entity.AiBytedanceAdDplinkInfo;
import org.jeecg.modules.bytedance.advertise.mapper.AiBytedanceAdDplinkInfoMapper;
import org.jeecg.modules.bytedance.advertise.service.IAiBytedanceAdDplinkInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 头条计划dplink标识创建记录
 * @author jeecg-boot
 * 2021-04-26
 * @version V1.0
 */
@Service
public class AiBytedanceAdDplinkInfoServiceImpl extends ServiceImpl<AiBytedanceAdDplinkInfoMapper, AiBytedanceAdDplinkInfo> implements IAiBytedanceAdDplinkInfoService {

    @Resource
    private AiBytedanceAdDplinkInfoMapper aiBytedanceAdDplinkInfoMapper;

    @Override
    public AiBytedanceAdDplinkInfo getlatestDpLinkInfo(Long strategyId, Long accountId, String imageMode,Integer status) {
        QueryWrapper<AiBytedanceAdDplinkInfo> queryWrapper = new QueryWrapper<>();
        if(null!=strategyId&&strategyId!=0){
            queryWrapper.eq("strategy_id",strategyId);
        }
        if(null!=accountId&&accountId!=0){
            queryWrapper.eq("account_id",accountId);
        }
        if(null!=status){
            queryWrapper.eq("status",status);
        }
        if(null!=imageMode){
            queryWrapper.eq("image_mode",imageMode);
        }
        queryWrapper.orderByDesc("dp_link_index").last("limit 1");
        return this.getOne(queryWrapper);
    }

    /**
     *
     * @description: 查询账户当天已有条数
     *
     * @param accountId
     * @return: int
     * @author: zianY
     */
    @Override
    public int getDpLinkInfoCount(Long accountId,String imageMode) {
        QueryWrapper<AiBytedanceAdDplinkInfo> queryWrapper = new QueryWrapper<>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        queryWrapper.eq("account_id",accountId);
        queryWrapper.ge("create_time",format);
        queryWrapper.eq("image_mode",imageMode);
        return aiBytedanceAdDplinkInfoMapper.selectCount(queryWrapper);

    }


}
