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.SsmRechargeDao;
import com.ssm.manage.entity.BaseGrouponCard;
import com.ssm.manage.entity.CashList;
import com.ssm.manage.entity.RechargeList;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.RechargeListMapper;
import com.ssm.manage.query.RechargeListRequest;
import com.ssm.manage.query.StoreDailyRequest;
import com.ssm.manage.response.RechargeListResponse;
import com.ssm.manage.response.RechargeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

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

@Component
public class SsmRechargeDaoImpl extends BaseDaoImpl<RechargeList> implements SsmRechargeDao {
    @Autowired
    private RechargeListMapper mapper;

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

    @Override
    public PageInfo<RechargeList> selectPage(RechargeListRequest rechargeListRequest) {
//        Example example = new Example(RechargeList.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storeId",rechargeListRequest.getStoreId());
        PageHelper.startPage(rechargeListRequest.getPageNum(),rechargeListRequest.getPageSize(),rechargeListRequest.getOrderAndSort());
//        List<RechargeList> list = selectByExample(example);
        List<RechargeList> list = mapper.selectPage(rechargeListRequest);
        PageInfo<RechargeList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<RechargeList> selectPageNew(RechargeListRequest rechargeListRequest) {
//        Example example = new Example(RechargeList.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storeId",rechargeListRequest.getStoreId());
        PageHelper.startPage(rechargeListRequest.getPageNum(),rechargeListRequest.getPageSize(),rechargeListRequest.getOrderAndSort());
//        List<RechargeList> list = selectByExample(example);
        List<RechargeList> list = mapper.selectPageNew(rechargeListRequest);
        //缓存分页
        PageInfo<RechargeList> pageCheck = new PageInfo<>(list);
        if(list.size()==0&&pageCheck.getTotal()>list.size()){
            //计算页数，从最后一页查询
            Integer count=Integer.valueOf(String.valueOf(pageCheck.getTotal()));
            Integer c=count%rechargeListRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/rechargeListRequest.getPageSize();
            }else{
                pages=count/rechargeListRequest.getPageSize()+pages;
            }
            PageHelper.startPage(pages,rechargeListRequest.getPageSize(),rechargeListRequest.getOrderAndSort());
            list = mapper.selectPageNew(rechargeListRequest);

        }
        PageInfo<RechargeList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<RechargeList> selectPages(RechargeListRequest rechargeListRequest) {
//        Example example = new Example(RechargeList.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storeId",rechargeListRequest.getStoreId());
        PageHelper.startPage(rechargeListRequest.getPageNum(),rechargeListRequest.getPageSize(),rechargeListRequest.getOrderAndSort());
//        List<RechargeList> list = selectByExample(example);
        List<RechargeList> list = mapper.selectPages(rechargeListRequest);
        PageInfo<RechargeList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    public void createTable(String date) {
        mapper.createTable(date);
    }
    @Override
    public void createTable1(String date) {
        mapper.createTable1(date);
    }

    @Override
    public RechargeList selectById(RechargeList rechargeList) {
        return mapper.selectById(rechargeList);
    }

    @Override
    public int addRecharge(RechargeList rechargeList) {
        return mapper.addRecharge(rechargeList);
    }

    @Override
    public List<RechargeList> getDocNum(RechargeListRequest rechargeListRequest) {
        return mapper.getDocNum(rechargeListRequest);
    }

    @Override
    public int cancel(RechargeList rechargeList) {
        return mapper.cancel(rechargeList);
    }

    @Override
    public List<RechargeList> getRecDoc(RechargeListRequest rechargeListRequest) {
        return mapper.getRecDoc(rechargeListRequest);
    }

    @Override
    public int updateStaff(RechargeList rechargeList) {
        return mapper.updateStaff(rechargeList);
    }

    @Override
    public StoreDaily selectDayRecord(StoreDailyRequest storeDailyRequest) {
        StoreDaily storeDaily=mapper.selectDayRecord(storeDailyRequest);
        List<String> ids=new ArrayList<>();
        for(StoreDaily storeDaily1:mapper.selectDayRecord1(storeDailyRequest)){
            ids.add(storeDaily1.getId());
        }
        if(ids.size()>0){
            storeDaily.setIds(ids);
        }
        return storeDaily;
    }

    @Override
    public StoreDaily NegativeKazin(StoreDailyRequest storeDailyRequest) {
        return mapper.NegativeKazin(storeDailyRequest);
    }

    @Override
    public StoreDaily grouponTuan(StoreDailyRequest storeDailyRequest) {
        return mapper.grouponTuan(storeDailyRequest);
    }

    @Override
    public PageInfo<BaseGrouponCard> grouponTuan1(StoreDailyRequest storeDailyRequest) {
        List<BaseGrouponCard> list=mapper.grouponTuan1(storeDailyRequest);
        PageInfo<BaseGrouponCard> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public RechargeList selectAchiEntities(RechargeList rechargeList) {

        return mapper.selectAchiEntities(rechargeList);
    }

    @Override
    public List<RechargeVo> selectByDay(StoreDailyRequest storeDailyRequest) {
        return mapper.selectByDay(storeDailyRequest);
    }

    @Override
    public List<StoreDaily> selectByDayGroupByDateAndStoreId(StoreDailyRequest storeDailyRequest) {
        return mapper.selectByDayGroupByDateAndStoreId(storeDailyRequest);
    }

    @Override
    public PageInfo<RechargeList> rechargeGifList(RechargeListRequest rechargeListRequest) {
        //        Example example = new Example(RechargeList.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("storeId",rechargeListRequest.getStoreId());
        PageHelper.startPage(rechargeListRequest.getPageNum(),rechargeListRequest.getPageSize(),rechargeListRequest.getOrderAndSort());
//        List<RechargeList> list = selectByExample(example);
        List<RechargeList> list = mapper.rechargeGifList(rechargeListRequest);
        //缓存分页
        PageInfo<RechargeList> pageCheck = new PageInfo<>(list);
        if(list.size()==0&&pageCheck.getTotal()>list.size()){
            //计算页数，从最后一页查询
            Integer count=Integer.valueOf(String.valueOf(pageCheck.getTotal()));
            Integer c=count%rechargeListRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/rechargeListRequest.getPageSize();
            }else{
                pages=count/rechargeListRequest.getPageSize()+pages;
            }
            PageHelper.startPage(pages,rechargeListRequest.getPageSize(),rechargeListRequest.getOrderAndSort());
            list = mapper.rechargeGifList(rechargeListRequest);

        }
        PageInfo<RechargeList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<RechargeList> selectListSalem(RechargeList rechargeList){
        return mapper.selectListSalem(rechargeList);
    }

    @Override
    public int updateRatio(RechargeList rechargeList){
        return mapper.updateRatio(rechargeList);
    }
}
