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.manage.dao.BaseDao;
import com.ssm.manage.dto.UseStoreDto;
import com.ssm.manage.entity.UseStore;
import com.ssm.manage.mapper.UseStoreMapper;
import com.ssm.manage.service.UseStoreService;
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;

/**
 * (UseStore)表服务实现类
 *
 * @author makejava
 * @since 2022-06-11 15:00:25
 */
@Service("useStoreService")
public class UseStoreServiceImpl extends BaseServiceImpl<UseStore> implements UseStoreService {


    @Autowired
    private UseStoreMapper useStoreDao;


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

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageInfo<UseStore> queryByPage(UseStoreDto dto) {
        Example example = new Example(UseStore.class);
        Example.Criteria criteria = example.createCriteria();

        if (null !=dto.getCouponName()){
            criteria.andLike("couponName","%"+dto.getCouponName()+"%");
        }
        if (null !=dto.getStoreName()){
            criteria.andLike("storeName","%"+dto.getStoreName()+"%");
        }
        if (null !=dto.getCouponNum()){
            criteria.andLike("couponNum","%"+dto.getCouponNum()+"%");
        }

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


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

        if (null !=dto.getCouponName()){
            criterias.andLike("couponName","%"+dto.getCouponName()+"%");
        }
        if (null !=dto.getStoreName()){
            criterias.andLike("storeName","%"+dto.getStoreName()+"%");
        }
        if (null !=dto.getCouponNum()){
            criterias.andLike("couponNum","%"+dto.getCouponNum()+"%");
        }

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

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

    }

    /**
     * 新增
     *
     * @param useStore
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add(UseStore useStore) {
        return useStoreDao.insert(useStore);
    }

    /**
     * 编辑
     *
     * @param useStore
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int edit(UseStore useStore) {
        return useStoreDao.updateByPrimaryKeySelective(useStore);
    }

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

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