package com.iformall.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.enums.EnumAppType;
import com.iformall.mapper.WxAppinfoMapper;
import com.iformall.service.WxAppinfoService;
import com.iformall.utils.Constant;
import com.iformall.utils.MaUtil;
import com.iformall.utils.QRCodeUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iformall.common.IdWorker;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class WxAppinfoServiceImpl implements WxAppinfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    WxAppinfoMapper wxAppinfoMapper;

	@Override
    public WxAppinfo getCAppInfo(String tenantId) {
        WxAppinfo appInfo = null;
        WxAppinfo appinfoQ = new WxAppinfo();
        appinfoQ.setTenantId(tenantId);
        appinfoQ.setType(EnumAppType.C.getCode());
        List<WxAppinfo> appList = wxAppinfoMapper.select(appinfoQ);
        if (appList.size() > 0) {
            appInfo = appList.get(0);
        }
        return appInfo;
    }

    @Override
    public WxAppinfo getByAppId(String appId) {
        return wxAppinfoMapper.findByAppId(appId);
    }

    @Override
    public PageInfo<WxAppinfo> listAsPage(WxAppinfo record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxAppinfoMapper.findList(record));
    }

    @Override
    public List<WxAppinfo> getList(WxAppinfo record) {
	    return wxAppinfoMapper.findList(record);
    }

    @Override
    public WxAppinfo getById(Long id) {
        return wxAppinfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(WxAppinfo record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            wxAppinfoMapper.insertSelective(record);
        } else {
            wxAppinfoMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        wxAppinfoMapper.deleteByPrimaryKey(id);
    }
}
