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.SsmCourseDao;
import com.ssm.manage.entity.CourseList;
import com.ssm.manage.entity.RechargeList;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.CourseListMapper;
import com.ssm.manage.mapper.CourseListMemberInitMapper;
import com.ssm.manage.query.CourseListRequest;
import com.ssm.manage.query.StoreDailyRequest;
import com.ssm.manage.response.CourseDayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class SsmCourseDaoImpl extends BaseDaoImpl<CourseList> implements SsmCourseDao {
    @Autowired
    private CourseListMapper mapper;

    @Autowired
    private CourseListMemberInitMapper courseListMemberInitMapper;

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

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

    @Override
    public PageInfo<CourseList> selectPage(CourseListRequest courseListRequest) {
        PageHelper.startPage(courseListRequest.getPageNum(),courseListRequest.getPageSize(),courseListRequest.getOrderAndSort());
        List<CourseList> lists = mapper.selectPage(courseListRequest);
        PageInfo<CourseList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public PageInfo<CourseList> selectPageNew(CourseListRequest courseListRequest) {
        PageHelper.startPage(courseListRequest.getPageNum(),courseListRequest.getPageSize(),courseListRequest.getOrderAndSort());
        List<CourseList> lists = mapper.selectPageNew(courseListRequest);
        //缓存分页
        PageInfo<CourseList> pageCheck = new PageInfo<>(lists);
        if(lists.size()==0&&pageCheck.getTotal()>lists.size()){
            //计算页数，从最后一页查询
            Integer count=Integer.valueOf(String.valueOf(pageCheck.getTotal()));
            Integer c=count%courseListRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/courseListRequest.getPageSize();
            }else{
                pages=count/courseListRequest.getPageSize()+pages;
            }
            PageHelper.startPage(pages,courseListRequest.getPageSize(),courseListRequest.getOrderAndSort());
            lists =mapper.selectPageNew(courseListRequest);
        }

        PageInfo<CourseList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public PageInfo<CourseList> memberPage(CourseListRequest courseListRequest) {
        PageHelper.startPage(courseListRequest.getPageNum(),courseListRequest.getPageSize(),courseListRequest.getOrderAndSort());
        List<CourseList> lists = courseListMemberInitMapper.selectPageNew(courseListRequest);
        //缓存分页
        PageInfo<CourseList> pageCheck = new PageInfo<>(lists);
        if(lists.size()==0&&pageCheck.getTotal()>lists.size()){
            //计算页数，从最后一页查询
            Integer count=Integer.valueOf(String.valueOf(pageCheck.getTotal()));
            Integer c=count%courseListRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/courseListRequest.getPageSize();
            }else{
                pages=count/courseListRequest.getPageSize()+pages;
            }
            PageHelper.startPage(pages,courseListRequest.getPageSize(),courseListRequest.getOrderAndSort());
            lists =courseListMemberInitMapper.selectPageNew(courseListRequest);
        }

        PageInfo<CourseList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public List<CourseList> selectPages(CourseListRequest courseListRequest) {
        List<CourseList> lists = mapper.selectPages(courseListRequest);
        return lists;
    }
    @Override
    public List<CourseList> selectMemberInitPages(CourseListRequest courseListRequest) {
        List<CourseList> lists = courseListMemberInitMapper.selectPages(courseListRequest);
        return lists;
    }

    @Override
    public List<CourseList> getDocNum(CourseListRequest courseListRequest) {
        return mapper.getDocNum(courseListRequest);
    }

    @Override
    public int addCourse(CourseList courseList) {
        return mapper.addCourse(courseList);
    }

    @Override
    public int cancel(CourseList courseList) {
        return mapper.cancel(courseList);
    }

    @Override
    public int memberInitCancel(CourseList courseList) {
        return courseListMemberInitMapper.cancel(courseList);
    }

    @Override
    public int updateStaff(CourseList courseList) {
        return mapper.updateStaff(courseList);
    }

    @Override
    public int updateMemberInitStaff(CourseList courseList) {
        return courseListMemberInitMapper.updateStaff(courseList);
    }
    @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 selectOutcourse(StoreDailyRequest storeDailyRequest) {
        StoreDaily storeDaily=new StoreDaily();
        storeDaily=mapper.selectOutcourse(storeDailyRequest);
        List<String> ids=new ArrayList<>();
        for(StoreDaily storeDaily1:mapper.selectOutcourse1(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 List<CourseList> selectListSalem(CourseList courseList) {
        return mapper.selectListSalem(courseList);
    }

    @Override
    public List<CourseList> selectByCourseListMemberInit(CourseList courseList) {
        return courseListMemberInitMapper.selectListSalem(courseList);
    }

    @Override
    public CourseList selectOneS(CourseList courseList) {
        return mapper.selectOneS(courseList);
    }

    @Override
    public int updateOne(CourseList courseList) {
        return mapper.updateOne(courseList);
    }

    @Override
    public List<CourseList> selectList(CourseList courseList) {
        return mapper.selectList(courseList);
    }
    @Override
    public List<CourseList> selectMemberInitList(CourseList courseList) {
        return courseListMemberInitMapper.selectList(courseList);
    }

    @Override
    public List<CourseList> selectList2(CourseList courseList) {
        return mapper.selectList2(courseList);
    }

    @Override
    public StoreDaily selectDocId(StoreDailyRequest storeDailyRequest) {
        StoreDaily storeDaily=mapper.selectDayRecordAndAmount(storeDailyRequest);
        List<String> ids=new ArrayList<>();
        List<StoreDaily> storeDailies = mapper.selectDocId(storeDailyRequest);
        for(StoreDaily storeDaily1: storeDailies){
            ids.add(storeDaily1.getDocId());
        }
        if(ids.size()>0){
            storeDaily.setIds(ids);
        }

        return storeDaily;
    }

    @Override
    public List<CourseList> selectCourseOne(CourseListRequest courseListRequest) {
        return mapper.selectCourseOne(courseListRequest);
    }

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

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

    @Override
    public List<CourseList> selectCourseByProjectAndDocId(CourseList courseList) {
        return mapper.selectCourseByProjectAndDocId(courseList);
    }

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

    @Override
    public List<CourseList> selectCourseMemberInit(CourseListRequest courseListRequest) {
        return courseListMemberInitMapper.selectCourseOne(courseListRequest);
    }
}
