package com.slodon.b2b2c.model.member.advich;

import com.maxmind.db.CHMCache;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Location;
import com.maxmind.geoip2.record.Subdivision;
import com.slodon.b2b2c.core.constant.EnquiryConst;
import com.slodon.b2b2c.core.constant.NumConstant;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.DateUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.dao.read.member.MemberDownloadCenterReadMapper;
import com.slodon.b2b2c.dao.read.member.MemberEnquiryReadMapper;
import com.slodon.b2b2c.dao.write.member.MemberDownloadCenterWriteMapper;
import com.slodon.b2b2c.dao.write.system.EnquiryWriteMapper;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.member.example.MemberDownloadCenterExample;
import com.slodon.b2b2c.member.example.MemberEnquiryExample;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.member.pojo.MemberDownloadCenter;
import com.slodon.b2b2c.member.pojo.MemberEnquiry;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.system.dto.CountryAreaApiDto;
import com.slodon.b2b2c.system.dto.EnquiryAddDTO;
import com.slodon.b2b2c.system.example.EnquiryExample;
import com.slodon.b2b2c.system.pojo.Enquiry;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @author cyan
 */
@Component
@Slf4j
public class MemberDownloadCenterModel {

    private static DatabaseReader reader = null;

    @Resource
    private MemberDownloadCenterWriteMapper memberDownloadCenterWriteMapper;

    @Resource
    private MemberDownloadCenterReadMapper memberDownloadCenterReadMapper;

    @Resource
    private GoodsModel goodsModel;

    @Value("${geoip.static.city.mmdb}")
    private String geoipCityMmdb;

    /**
     * 下载记录
     *
     * @param downloadCenter
     * @return
     */
    public Integer saveDownloadRecord(HttpServletRequest request, MemberDownloadCenter downloadCenter) throws Exception {
        Member member = UserUtil.getUser(request, Member.class);
        downloadCenter.setMemberId(member.getMemberId());
        downloadCenter.setCreateTime(new Date());
        downloadCenter.setStatus(NumConstant.ONE);
        Goods goodsInfo = goodsModel.getGoodsByGoodsId(downloadCenter.getGoodsId());
        downloadCenter.setDefaultProductId(goodsInfo.getDefaultProductId());
        int count = memberDownloadCenterWriteMapper.insert(downloadCenter);
        AssertUtil.notNullOrZero(count, "商品说明下载失败，请重试");
        return count;
    }

    /**
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     */
    public List<MemberDownloadCenter> getDownloadCenterList(MemberDownloadCenterExample example, PagerInfo pager) {
        List<MemberDownloadCenter> downloadCenterList;
        if (pager != null) {
            pager.setRowsCount(memberDownloadCenterReadMapper.countByExample(example));
            downloadCenterList = memberDownloadCenterReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            downloadCenterList = memberDownloadCenterReadMapper.listByExample(example);
        }
        return downloadCenterList;
    }

    /**
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     */
    public List<MemberDownloadCenter> getDownloadCenterListByPage(MemberDownloadCenterExample example, PagerInfo pager) {
        List<MemberDownloadCenter> downloadCenterList = null;
        if (pager != null) {
            downloadCenterList = memberDownloadCenterReadMapper.downloadReacordListByPage(example.getMemberId(),example.getGoodsName(), pager.getStart(), pager.getPageSize());
        }
        return downloadCenterList;
    }

    /**
     *
     * @param example 查询条件信息
     * @return 购物车列表
     */
    public Integer downloadReacordListByPageCount(MemberDownloadCenterExample example) {
        return memberDownloadCenterReadMapper.downloadReacordListByPageCount(example.getMemberId(),example.getGoodsName());
    }



}
