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.common.ResultData;
import com.iformall.domain.po.WxPowerBillAutoConfig;
import com.iformall.mapper.WxMerchantMapper;
import com.iformall.mapper.WxPowerBillAutoConfigMapper;
import com.iformall.service.WxPowerBillAutoConfigService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author gongbiao
 */
@Service
@Slf4j
public class WxPowerBillAutoConfigServiceImpl implements WxPowerBillAutoConfigService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxPowerBillAutoConfigMapper wxPowerBillAutoConfigMapper;

    @Autowired
    WxMerchantMapper wxMerchantMapper;


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

    @Override
    public WxPowerBillAutoConfig getConfig(String tenantId) {
        WxPowerBillAutoConfig wxPowerBillAutoConfig = new WxPowerBillAutoConfig();
        wxPowerBillAutoConfig.setTenantId(tenantId);
        return wxPowerBillAutoConfigMapper.selectOne(new QueryWrapper(wxPowerBillAutoConfig));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public ResultData modify(WxPowerBillAutoConfig config) {
        Date date = new Date();
        if (config.getId() == null) {
            final IdWorker idWorker = IdWorker.get();
            config.setId(idWorker.nextId());
            config.setCreateDate(date);
            config.setUpdateDate(date);
            wxPowerBillAutoConfigMapper.insert(config);
        } else {
            config.setUpdateDate(date);
            wxPowerBillAutoConfigMapper.updateById(config);
        }
        return new ResultData(config);
    }

}
