package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.dto.WxCUserFromBDto;
import com.iformall.domain.po.WxCUserFromB;
import com.iformall.domain.po.WxMerchantBUser;
import com.iformall.mapper.WxCUserFromBMapper;
import com.iformall.mapper.WxMerchantBUserMapper;
import com.iformall.service.WxCUserFromBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author gongbiao
 */
@Service
public class WxCUserFromBServiceImpl implements WxCUserFromBService {
	
	@Autowired
    WxCUserFromBMapper wxCUserFromBMapper;

    @Autowired
    WxMerchantBUserMapper wxMerchantBUserMapper;

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

    @Override
    public WxCUserFromB getById(Long id) {
        return wxCUserFromBMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(WxCUserFromB record) {
        if (record.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
            record.setId(idWorker.nextId());
            Date date = new Date();
            record.setCreateTime(date);
            record.setUpdateTime(date);
            wxCUserFromBMapper.insert(record);
        } else {
            record.setUpdateTime(new Date());
            wxCUserFromBMapper.updateById(record);
        }
    }

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

    @Override
    public WxCUserFromB getByOpenidAndAppId(WxCUserFromBDto wxCUserFromBDto) {
        WxMerchantBUser wxMerchantBUser = wxMerchantBUserMapper.selectById(wxCUserFromBDto.getUserId());
        if (wxMerchantBUser == null) {
            return null;
        }
        wxCUserFromBDto.setMerchantId(wxMerchantBUser.getMerchantId());
        return wxCUserFromBMapper.getByOpenidAndAppId(wxCUserFromBDto);
    }

    @Override
    public PageInfo<WxCUserFromB> listCUser(WxCUserFromBDto wxCUserFromBDto, Integer pageIndex, Integer pageSize) {
        WxCUserFromB record = new WxCUserFromB();
        record.setBUserId(wxCUserFromBDto.getUserId());
        return PageHelper.startPage(pageIndex, pageSize)
                .doSelectPageInfo(() -> wxCUserFromBMapper.findList(record));
    }


}
