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


import com.mortals.framework.dao.ibatis.BaseCRUDDaoMybatis;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.ParamDto;
import com.mortals.framework.model.Result;
import com.mortals.xhx.module.matter.dao.MatterDao;
import com.mortals.xhx.module.matter.model.MatterEntity;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础事项DaoImpl DAO接口
 *
 * @author zxfei
 * @date 2022-01-12
 */
@Repository("matterDao")
public class MatterDaoImpl extends BaseCRUDDaoMybatis<MatterEntity, Long> implements MatterDao {


    @Override
    public Result<MatterEntity> getSubList(MatterEntity matterQuery, PageInfo pageInfo) {
        Result<MatterEntity> result = new Result();
        ParamDto paramDto = this.getQueryParam(matterQuery);
        int count = this.getSubCount(paramDto);
        List list = null;
        if (count == 0) {
            list = new ArrayList();
        } else if (pageInfo.getPrePageResult() == -1) {
            list = this.getSqlSession().selectList(this.getSqlId(SQLID_SUB_LIST), paramDto);
        } else {
            RowBounds rowBounds = new RowBounds(pageInfo.getBeginIndex(), pageInfo.getPrePageResult());
            list = this.getSqlSession().selectList(this.getSqlId(SQLID_SUB_LIST), this.cpyQueryParamDto(paramDto), rowBounds);
        }
        pageInfo.setTotalResult(count);
        result.setPageInfo(pageInfo);
        result.setList(list);
        return result;
    }

    public int getSubCount(ParamDto paramDto) {
        return this.getSqlSession().selectOne(this.getSqlId(SQLID_SUB_COUNT), this.cpyQueryParamDto(paramDto));
    }

    @Override
    public int getMatterListByAreaCodeCount(ParamDto paramDto) {
        return this.getSqlSession().selectOne(this.getSqlId(SQLID_MATTERLIST_AREACODE_COUNT), this.cpyQueryParamDto(paramDto));
    }

    @Override
    public List<MatterEntity> getMatterListByAreaCode(MatterEntity matterQuery) {

        ParamDto param = new ParamDto();
        param.getCondition().put("areaCode", matterQuery.getAreaCode());
        if (!ObjectUtils.isEmpty(matterQuery.getSource())) {
            param.getCondition().put("source", matterQuery.getSource());
        }
        return this.getSqlSession().selectList(this.getSqlId("getMatterListByAreaCode"), param);
    }

    /**
     * @param matterQuery
     * @param pageInfo
     * @return
     */
    @Override
    public Result<MatterEntity> getMatterListByAreaCodePage(MatterEntity matterQuery, PageInfo pageInfo) {
        Result<MatterEntity> result = new Result();
        ParamDto param = new ParamDto();
        param.getCondition().put("areaCode", matterQuery.getAreaCode());

        param.getCondition().put("source", matterQuery.getSource());

        RowBounds rowBounds = new RowBounds(pageInfo.getBeginIndex(), pageInfo.getPrePageResult());
        List<MatterEntity> list = this.getSqlSession().selectList(this.getSqlId(SQLID_MATTERLIST_AREACODE), param, rowBounds);

        // pageInfo.setTotalResult(count);
        result.setPageInfo(pageInfo);
        result.setList(list);
        return result;
    }

}
