package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.enums.EnumAppType;
import com.iformall.mapper.WxAppinfoMapper;
import com.iformall.service.WxAppinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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.selectList(new QueryWrapper(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.selectById(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.insert(record);
        } else {
            wxAppinfoMapper.updateById(record);
        }
    }

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