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.UseProjectDto;
import com.ssm.manage.entity.UseProject;
import com.ssm.manage.mapper.UseProjectMapper;
import com.ssm.manage.service.UseProjectService;
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;

/**
 * (UseProject)表服务实现类
 *
 * @author makejava
 * @since 2022-06-11 15:00:41
 */
@Service("useProjectService")
public class UseProjectServiceImpl extends BaseServiceImpl<UseProject> implements UseProjectService {

    @Autowired
    private UseProjectMapper useProjectDao;


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

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

        //项目名称
        if (null !=dto.getProjectName()){
            criteria.andLike("projectName","%"+dto.getProjectName()+"%");
        }
        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<UseProject> list = useProjectDao.selectByExample(example);
        Example examples = new Example(UseProject.class);
        Example.Criteria criterias = examples.createCriteria();
        //项目名称
        if (null !=dto.getProjectName()){
            criterias.andLike("projectName","%"+dto.getProjectName()+"%");
        }
        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 = useProjectDao.selectByExample(examples).size();

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

    /**
     * 新增
     *
     * @param useProject
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add(UseProject useProject) {
        return useProjectDao.insert(useProject);
    }

    /**
     * 编辑
     *
     * @param useProject
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int edit(UseProject useProject) {
        return useProjectDao.updateByPrimaryKeySelective(useProject);
    }

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

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