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.SsmCashDao;
import com.ssm.manage.entity.CashList;
import com.ssm.manage.extendEntity.StaffProgress;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.CashListMapper;
import com.ssm.manage.query.AccountTradeRecordRequest;
import com.ssm.manage.query.CashListRequest;
import com.ssm.manage.query.StoreDailyRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.CashDayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class SsmCashListDaoImpl extends BaseDaoImpl<CashList> implements SsmCashDao {
    @Autowired
    private CashListMapper mapper;

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

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

    @Override
    public PageInfo<CashList> selectPage(CashListRequest cashListRequest) {
        PageHelper.startPage(cashListRequest.getPageNum(),cashListRequest.getPageSize(),cashListRequest.getOrderAndSort());
        List<CashList> list = mapper.selectPage(cashListRequest);
        PageInfo<CashList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<CashList> selectPage2(CashListRequest cashListRequest) {
        return mapper.selectPage2(cashListRequest);
    }

    @Override
        public List<CashList> selectPage3(CashListRequest cashListRequest) {
            return mapper.selectPage3(cashListRequest);
        }

    @Override
    public PageInfo<CashList> selectCashList(CashListRequest cashListRequest) {
        //PageHelper.startPage(cashListRequest.getPageNum(),cashListRequest.getPageSize(),cashListRequest.getOrderAndSort());
        List<CashList> list = mapper.selectCashList(cashListRequest);
        PageInfo<CashList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


     @Override
    public PageInfo<CashList> selectCashListGroup(CashListRequest cashListRequest) {
        PageHelper.startPage(cashListRequest.getPageNum(),cashListRequest.getPageSize(),cashListRequest.getOrderAndSort());
        List<CashList> list = mapper.selectCashListGroup(cashListRequest);
        PageInfo<CashList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<CashList> selectCashListGroupNew(CashListRequest cashListRequest) {

       /* if((cashListRequest.getMobilePhone()!=null && !cashListRequest.getMobilePhone().trim().equals(""))
        || (cashListRequest.getMemberName()!=null&&!cashListRequest.getMemberName().trim().equals(""))
        || (cashListRequest.getCashNum()!=null&&!cashListRequest.getCashNum().trim().equals(""))
        ||cashListRequest.getStoreListboolean()==0){//&&cashListRequest.getPageNum()>1

        }else{
            PageHelper.startPage(cashListRequest.getPageNum(),cashListRequest.getPageSize(),cashListRequest.getOrderAndSort());
        }*/
        PageHelper.startPage(cashListRequest.getPageNum(),cashListRequest.getPageSize(),cashListRequest.getOrderAndSort());
        List<CashList> list=mapper.selectCashListGroupNew(cashListRequest);
        //缓存分页
        PageInfo<CashList> pageCheck =new PageInfo<>(list);
        if(list.size()==0&&pageCheck.getTotal()>list.size()){
            //计算页数，从最后一页查询
            Integer count=Integer.valueOf(String.valueOf(pageCheck.getTotal()));
            Integer c=count%cashListRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/cashListRequest.getPageSize();
            }else{
                pages=count/cashListRequest.getPageSize()+pages;
            }
            PageHelper.startPage(pages,cashListRequest.getPageSize(),cashListRequest.getOrderAndSort());
            list = mapper.selectCashListGroupNew(cashListRequest);
        }
        PageInfo<CashList> pageInfo =new PageInfo<>(list);
        return pageInfo;
    }
     @Override
    public PageInfo<CashList> selectCashListGroups(CashListRequest cashListRequest) {
        PageHelper.startPage(cashListRequest.getPageNum(),cashListRequest.getPageSize(),cashListRequest.getOrderAndSort());
        List<CashList> list = mapper.selectCashListGroups(cashListRequest);
        PageInfo<CashList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    public List<CashList> selectByNum(CashListRequest cashListRequest) {
        return mapper.selectByNum(cashListRequest);
    }

    @Override
    public List<CashList> getDocNum(CashListRequest cashListRequest) {
        return mapper.getDocNum(cashListRequest);
    }

    @Override
    public int addCash(CashList cashList) {
        return mapper.addCash(cashList);
    }

    @Override
    public int cancel(CashList cashList) {
        return mapper.cancel(cashList);
    }

    @Override
    public int doCash(CashList cashList) {
        return mapper.doCash(cashList);
    }

    @Override
    public CashList one(CashListRequest cashListRequest) {
        return mapper.one(cashListRequest);
    }

    @Override
    public int updateStaff(CashList cashList) {
        return mapper.updateStaff(cashList);
    }
    @Override
    public int updateFeedback(CashList cashList) {
        return mapper.updateFeedback(cashList);
    }

    @Override
    public PageInfo<CashList> selectProgress(CashListRequest cashListRequest) {
        PageHelper.startPage(cashListRequest.getPageNum(),cashListRequest.getPageSize(),cashListRequest.getOrderAndSort());
        List<CashList> list = mapper.selectProgress(cashListRequest);
        PageInfo<CashList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<StaffProgress> selectStaffProgress(StaffProgress staffProgress) {
        PageHelper.startPage(staffProgress.getPageNum(),staffProgress.getPageSize(),staffProgress.getOrderAndSort());
        List<StaffProgress> list = mapper.selectStaffProgress(staffProgress);
        PageInfo<StaffProgress> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<StoreDaily> selectDayRecord(StoreDailyRequest storeDailyRequest) {
        List<StoreDaily> list=mapper.selectDayRecord(storeDailyRequest);

        List<String> ids=new ArrayList<>();
        for(StoreDaily storeDaily:mapper.selectDayRecord1(storeDailyRequest)){
            ids.add(storeDaily.getId());
        }
        if(ids.size()>0){
            list.get(0).setIds(ids);
        }
        return list;
    }

    @Override
    public List<StoreDaily> selectDayRecordManAndWoman(StoreDailyRequest storeDailyRequest) {
        List<StoreDaily> list=mapper.selectDayRecordManAndWoman(storeDailyRequest);

        List<String> ids=new ArrayList<>();
        for(StoreDaily storeDaily:mapper.selectDayRecord1(storeDailyRequest)){
            ids.add(storeDaily.getId());
        }
        if(ids.size()>0){
            list.get(0).setIds(ids);
        }
        return list;
    }

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

    @Override
    public PageInfo<CashList> selectByRecharge(AccountTradeRecordRequest accountTradeRecordRequest) {
        PageHelper.startPage(accountTradeRecordRequest.getPageNum(),accountTradeRecordRequest.getPageSize(),accountTradeRecordRequest.getOrderAndSort());
        List<CashList> list = mapper.selectByRecharge(accountTradeRecordRequest);
        PageInfo<CashList> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<CashList> selectDownloadRecord(CashListRequest cashListRequest) {
        return  mapper.selectPage(cashListRequest);
    }

    @Override
    public List<CashList> getAllCashList(CashListRequest cashListRequest) {

        return mapper.getAllCashList(cashListRequest);
    }

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

    @Override
    public List<CashList> selectAbnormalOrder(CashListRequest cashListRequest) {
        return mapper.selectAbnormalOrder(cashListRequest);
    }

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

    @Override
    public List<CashList> selectListSalem(CashList cashList){
        return  mapper.selectListSalem(cashList);
    }

    @Override
    public int updatechia(CashList cashList){
        return  mapper.updatechia(cashList);
    }
}
