package com.ssm.manage.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.dao.SmsBalanceDao;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.SmsBalance;
import com.ssm.manage.entity.SmsFix;
import com.ssm.manage.mapper.SmsBalanceMapper;
import com.ssm.manage.mapper.SmsFixMapper;
import com.ssm.manage.query.SmsBalanceRequest;
import com.ssm.manage.service.SmsBalanceService;
import com.ssm.manage.service.SmsFixService;
import com.ssm.manage.service.SsmBaseStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SmsBalanceServiceImpl extends BaseServiceImpl<SmsBalance> implements SmsBalanceService {

    @Autowired
    private SmsBalanceMapper smsBalanceMapper;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SmsBalanceDao smsBalanceDao;

    @Override
    public BaseDao<SmsBalance> getDao() {
        return smsBalanceDao;
    }

    @Override
    public SmsBalance selectBystoreId(SmsBalance smsBalance) {
        //根据当前门店获取到短信账号信息
        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(smsBalance.getStoreId());
        String smsAccount = baseStore.getSmsAccount();
        SmsBalance balance = new SmsBalance();
        balance.setSmsAccount(smsAccount);
        List<SmsBalance> balanceList = smsBalanceMapper.select(balance);
        if(balanceList!=null && balanceList.size()>0){
            return balanceList.get(0);
        }
        return null;
    }

    @Override
    public PageInfo<SmsBalance> page(SmsBalanceRequest smsBalance) {
        PageHelper.startPage(smsBalance.getPageNum(),smsBalance.getPageSize());
        List<SmsBalance> balanceList = smsBalanceMapper.selectGroupByAccount();
        PageInfo<SmsBalance> pageInfo = new PageInfo<>(balanceList);
        return pageInfo;
    }

    @Override
    public List<SmsBalance> selectBySmsBalance(SmsBalance smsBalance) {
        return smsBalanceMapper.selectBySmsBalance(smsBalance);
    }

    @Override
    public int insert(SmsBalance smsBalance) {
        return smsBalanceMapper.insert(smsBalance);
    }

    @Override
    public int updateById(SmsBalance smsBalance) {
        return smsBalanceMapper.updateByPrimaryKey(smsBalance);
    }
}
