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.SsmStaffAttendanceRecordDao;
import com.ssm.manage.entity.StaffAttendanceRecord;
import com.ssm.manage.mapper.StaffAttendanceRecordMapper;
import com.ssm.manage.query.StaffAttendanceRecordRequest;
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.List;

@Component
public class SsmStaffAttendanceRecordDaoImpl extends BaseDaoImpl<StaffAttendanceRecord> implements SsmStaffAttendanceRecordDao {
    @Autowired
    private StaffAttendanceRecordMapper mapper;

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

    @Override
    public PageInfo<StaffAttendanceRecord> selectPage(StaffAttendanceRecordRequest staffAttendanceRecordRequest) {
        Example example = new Example(StaffAttendanceRecord.class);
        example.setOrderByClause("date desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", staffAttendanceRecordRequest.getStoreId());
        if (StringUtils.isNotBlank(staffAttendanceRecordRequest.getMobilePhone())){
            criteria.andLike("mobilePhone", "%" + staffAttendanceRecordRequest.getMobilePhone() + "%");
        }
        if (null != staffAttendanceRecordRequest.getStaffId()){
            criteria.andEqualTo("staffId",  staffAttendanceRecordRequest.getStaffId());
        }
        if (null != staffAttendanceRecordRequest.getStartTime() && null != staffAttendanceRecordRequest.getEndTime()){
            criteria.andBetween("date", staffAttendanceRecordRequest.getStartTime(), staffAttendanceRecordRequest.getEndTime());
        }
        if (null != staffAttendanceRecordRequest.getState()) {
            criteria.andEqualTo("state", staffAttendanceRecordRequest.getState());
        }
        PageHelper.startPage(staffAttendanceRecordRequest.getPageNum(), staffAttendanceRecordRequest.getPageSize(), staffAttendanceRecordRequest.getOrderAndSort());
        List<StaffAttendanceRecord> list = mapper.selectByExample(example);
        PageInfo<StaffAttendanceRecord> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
