package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.SmsSwitch;
import com.lightinit.hsdataplatform.entity.SmsSwitchExample;
import com.lightinit.hsdataplatform.mapper.SmsSwitchMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.service.admin.ISmsSwitchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2019/3/5 10:12
 * @Description:
 **/
@Service
public class SmsSwitchServiceImpl implements ISmsSwitchService {

    @Autowired
    private SmsSwitchMapper smsSwitchMapper;
    @Override
    public List<SmsSwitch> getSmsSwitchList(BaseExample.Page pager) {
        SmsSwitchExample example = new SmsSwitchExample();
        example.setOrderByClause("id desc limit "+pager.getBeginIndex()+","+pager.getPageSize());
        return smsSwitchMapper.selectByExample(example);
    }

    @Override
    public long querySmsSwitchCount() {
        return smsSwitchMapper.countByExample(null);
    }

    @Override
    @Transactional
    public ResultState add(SmsSwitch inputModel) {
        ResultState resultState = new ResultState();
        inputModel.setCreatedAt(new Date());
        int i = smsSwitchMapper.insert(inputModel);
        if(i<1){
            resultState.setStateCode(400);
            resultState.setMsg("插入失败");
        }
        return resultState;
    }

    @Override
    public ResultState deleteById(long id) {
        ResultState resultState = new ResultState();
        int i = smsSwitchMapper.deleteByPrimaryKey(id);
        if(i<1){
            resultState.setStateCode(400);
            resultState.setMsg("更新失败");
        }
        return resultState;
    }

    @Override
    public ResultState updateById(SmsSwitch inputModel) {
        ResultState resultState = new ResultState();
        int i = smsSwitchMapper.updateByPrimaryKeySelective(inputModel);
        if(i<1){
            resultState.setStateCode(400);
            resultState.setMsg("修改失败");
        }
        return resultState;
    }

    @Override
    public SmsSwitch querySmsSwitchById(Long id) {
        return smsSwitchMapper.selectByPrimaryKey(id);
    }
}
