package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmMemberOrderDao;
import com.ssm.manage.entity.MemberOrder;
import com.ssm.manage.mapper.MemberOrderMapper;
import com.ssm.manage.query.MemberOrderRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

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

@Component
public class SsmMemberOrderDaoImpl extends BaseDaoImpl<MemberOrder> implements SsmMemberOrderDao {
    @Autowired
    private MemberOrderMapper mapper;

    @Override
    public MemberOrderMapper getMapper() {
        return mapper;
    }

    @Override
    public PageInfo<MemberOrder> selectPage(MemberOrderRequest memberOrderRequest) {
        Example example = new Example(MemberOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", memberOrderRequest.getStoreId());
        if (StringUtils.isNotBlank(memberOrderRequest.getMemberName())){
            criteria.andLike("memberName","%" + memberOrderRequest.getMemberName() + "%");
        }
        if (null != memberOrderRequest.getMemberId()){
            criteria.andEqualTo("memberId",memberOrderRequest.getMemberId());
        }
        if (null != memberOrderRequest.getStaffId()){
            criteria.andEqualTo("staffId",memberOrderRequest.getStaffId());
        }
        criteria.andEqualTo("state",0);
        criteria.andGreaterThan("orderDate", new Date());
        PageHelper.startPage(memberOrderRequest.getPageNum(), memberOrderRequest.getPageSize(), memberOrderRequest.getOrderAndSort());
        List<MemberOrder> list = selectByExample(example);
        PageInfo<MemberOrder> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
