package org.jeecg.modules.adweb.enquiry.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import jakarta.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.adweb.site.entity.AdwebSite;
import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: cyan
 * @Date: 2021-04-25
 * @Version: V1.0
 */
@Service
@Slf4j
public class AdwebOpenApiService {
    @Resource private IAdwebSiteService adwebSiteService;

    @Value("${server.servlet.context-path}")
    public String contextPath;

    /**
     * @param siteCode
     * @return
     */
    public List<AdwebSite> selectBySiteCode(String siteCode) {
        LambdaQueryWrapper<AdwebSite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdwebSite::getCode, siteCode);
        return adwebSiteService.list(queryWrapper);
    }

    /**
     * 获取4位编号
     *
     * @param siteCode
     * @return
     */
    public String loadNo(String siteCode) {
        int code = 0;
        List<AdwebSite> adwebSites = selectBySiteCode(siteCode);
        if (CollectionUtils.isNotEmpty(adwebSites)) {
            AdwebSite adwebSite = adwebSites.get(0);
            if (adwebSite.getDayEnquiryCount() != null) {
                code = adwebSite.getDayEnquiryCount() + 1;
            } else {
                code = 1;
            }
        } else {
            code = 1;
        }
        return String.format("%04d", code);
    }

    //    public String loadOutNoByUser(String uid) {
    //        if (StringUtils.isEmpty(uid)) {
    //            return "000";
    //        }
    //        Date todayZero = DateUtil.formatDate(new Date(), DateUtil.DATE_PATTERN);
    //        QueryWrapper<AdwebEnquiry> queryWrapper = new QueryWrapper<>();
    //        queryWrapper.eq("uid", uid).gt("ctime", todayZero);
    //        int count = (int) adwebEnquiryService.count(queryWrapper);
    //        return String.format("%03d", count + 1);
    //    }
}
