package com.mortals.xhx.module.check.dao.ibatis;

import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.ParamDto;
import com.mortals.framework.model.Result;
import com.mortals.xhx.module.check.dao.CheckAllRecordDao;
import com.mortals.xhx.module.check.model.CheckAllRecordQuery;
import com.mortals.xhx.module.check.model.vo.CheckAllRecordVo;
import com.mortals.xhx.module.check.model.vo.CheckAllScoreSummaryVo;
import com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 全部类型核查信息Dao
 */
@Repository("checkAllRecordDao")
public class CheckAllRecordDaoImpl extends SqlSessionDaoSupport implements CheckAllRecordDao {

    protected String namespace = this.getClass().getName();

    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    protected String getSqlId(String id) {
        return this.namespace + "." + id;
    }

    @Override
    public Result<CheckAllRecordVo> getAllCheckRecord(CheckAllRecordQuery checkAllRecordQuery, PageInfo pageInfo) {
        Result result = new Result();
        List list = null;
        if (pageInfo.isCountPage()) {
            int count = this.getCount(checkAllRecordQuery);
            if (count == 0) {
                list = new ArrayList();
            } else if (pageInfo.getPrePageResult() == -1) {
                list = this.getSqlSession().selectList(this.getSqlId("getList"), checkAllRecordQuery);
            } else {
                RowBounds rowBounds = new RowBounds(pageInfo.getBeginIndex(), pageInfo.getPrePageResult());
                list = this.getSqlSession().selectList(this.getSqlId("getList"), checkAllRecordQuery, rowBounds);
            }

            pageInfo.setTotalResult(count);
            result.setPageInfo(pageInfo);
            result.setList((List)list);
        } else {
            if (pageInfo.getPrePageResult() == -1) {
                list = this.getSqlSession().selectList(this.getSqlId("getList"), checkAllRecordQuery);
            } else {
                RowBounds rowBounds = new RowBounds(pageInfo.getBeginIndex(), pageInfo.getPrePageResult());
                list = this.getSqlSession().selectList(this.getSqlId("getList"), checkAllRecordQuery, rowBounds);
            }

            pageInfo.setTotalResult(-1);
            result.setPageInfo(pageInfo);
            result.setList(list);
        }
        return result;
    }
    @Override
    public int getCount(CheckAllRecordQuery checkAllRecordQuery) {
        return this.getSqlSession().selectOne(this.getSqlId("getListCount"), checkAllRecordQuery);
    }

    @Override
    public CheckAllScoreSummaryVo getScoreSummary(StaffCheckSummaryQuery query) {
        return this.getSqlSession().selectOne(this.getSqlId("getScoreSummary"), query);
    }
}
