package com.ssm.manage.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.dto.StoreWechatDto;
import com.ssm.manage.entity.StoreWechat;
import com.ssm.manage.mapper.StoreWechatMapper;
import com.ssm.manage.service.StoreWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.List;
import java.util.Random;

/**
 * (StoreWechat)表服务实现类
 *
 * @author makejava
 * @since 2022-07-07 15:49:57
 */
@Service("storeWechatService")
public class StoreWechatServiceImpl extends BaseServiceImpl<StoreWechat> implements StoreWechatService {

    @Autowired
    private StoreWechatMapper storeWechatMapper;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public StoreWechat queryById(Serializable id) {
        return storeWechatMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageInfo<StoreWechat> queryByPage(StoreWechatDto dto) {

        Example example = new Example(StoreWechat.class);
        Example.Criteria criteria = example.createCriteria();


        PageHelper.startPage(dto.getPageNum(),dto.getPageSize(),dto.getOrderAndSort());
        List<StoreWechat> list = storeWechatMapper.selectByExample(example);

        Example examples = new Example(StoreWechat.class);
        Example.Criteria criterias = examples.createCriteria();


        int size = storeWechatMapper.selectByExample(examples).size();

        PageInfo<StoreWechat> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(size);
        return pageInfo;
    }

    /**
     * 新增
     *
     * @param storeWechat
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add(StoreWechat storeWechat, String session) {
        Long refundId = new SnowflakeUtil(new Random().nextInt(1023)).nextId();
        storeWechat.setId(refundId);
        return storeWechatMapper.insert(storeWechat);
    }

    /**
     * 编辑
     *
     * @param storeWechat
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int edit(StoreWechat storeWechat,String session) {
        return storeWechatMapper.updateByPrimaryKeySelective(storeWechat);
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delete(Serializable id) {
        return storeWechatMapper.deleteByPrimaryKey(id);
    }

    @Override
    public BaseDao<StoreWechat> getDao() {
        return null;
    }
}