package com.ssj.service.weixin.library.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.weixin.libmy.domain.LibReadVip;
import com.ssj.dao.weixin.library.dao.IReadVipDao;
import com.ssj.dao.weixin.library.dao.IReadVipQueryDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.PropertiesUtil;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.weixin.util.BarcodeFactory;
import com.ssj.service.weixin.library.service.IReadVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author sh
 * @className ReadVipServiceImpl
 * @description 阅读会员service实现类
 * @date 2020/4/17
 */
@Service(version="1.0")
@Transactional
public class ReadVipServiceImpl extends BaseServiceImpl<LibReadVip,String> implements IReadVipService {

    @Autowired
    private IReadVipDao dao;

    @Autowired
    private IReadVipQueryDao queryDao;

    @Override
    public PagingAndSortingRepository<LibReadVip, String> getDao() {
        return dao;
    }

    @Override
    public List<LibReadVip> findByUserIdAndSpeciseService(String userId, String s001, String libId) {
        return dao.findByUserIdAndSpeciseService(userId,s001,libId);
    }

    @Override
    public List<Map<String, Object>> getServiceVip(String userId, String s001) {
        return queryDao.getServiceVip(userId,s001);
    }

    @Override
    public void vipEnCodeAll() throws Exception{
        List<LibReadVip>  list= dao.getLibVipIsNullQrCodeAll();
        String file_path = (String) SystemResourceLocator.getValue("file_save_path");
        int pageSize = Integer.parseInt( PropertiesUtil.getValue("export_page_size"));
        int i=0;
        String fPath ="";
        for (LibReadVip vip : list) {
            if(i%pageSize ==0 || i==0){
                fPath+="/VIP/"+ DateHelper.formatDateByFormat(new Date(), "yyyy/MM/dd")+"/"+i;
                if (!fPath.endsWith("/")) {
                    fPath += "/";
                }
                File f2 = new File(file_path+fPath);
                if (!f2.exists()) {
                    f2.mkdirs();
                }
            }
            String url=(String) SystemResourceLocator.getValue("sys_url")+"/weixin/libVip/bindingLibVip_VIPVIP.html";
            String fPathId = fPath + vip.getFlowId() + ".jpg";
            url=url.replace("VIPVIP", vip.getId());
            BarcodeFactory.bookencode(url,file_path+fPathId,300, 300);
            vip.setQrcodeUrl(fPathId);
            this.save(vip);
            i++;
        }
    }

    @Override
    public LibReadVip findByUserId(String userId) {
        return dao.findByUserId(userId);
    }

    @Override
    public LibReadVip findByUserIdAndChildName(String id, String childName) {
        return dao.findByUserIdAndChildName(id,childName);
    }
}
