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.SsmRepayDao;
import com.ssm.manage.entity.CourseList;
import com.ssm.manage.entity.PayList;
import com.ssm.manage.entity.RepayList;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.RepayListMapper;
import com.ssm.manage.query.RepayListRequest;
import com.ssm.manage.query.StoreDailyRequest;
import org.apache.commons.lang.StringUtils;
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 SsmRepayDaoImpl extends BaseDaoImpl<RepayList> implements SsmRepayDao {
    @Autowired
    private RepayListMapper mapper;

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

    @Override
    public PageInfo<RepayList> selectPage(RepayListRequest repayListRequest) {
        Example example = new Example(RepayList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != repayListRequest.getMemberId()){
            criteria.andEqualTo("memberId",repayListRequest.getMemberId());
        }
        if (null != repayListRequest.getStoreId()){
            criteria.andEqualTo("storeId",repayListRequest.getStoreId());
        }

        if (StringUtils.isNotBlank(repayListRequest.getRepayNum())){
            criteria.andLike("repayNum","%" + repayListRequest.getRepayNum() + "%");
        }
        if (StringUtils.isNotBlank(repayListRequest.getMemberName())){
            criteria.andLike("memberName","%" + repayListRequest.getMemberName() + "%");
        }
        if (StringUtils.isNotBlank(repayListRequest.getMobilePhone())){
            criteria.andLike("mobilePhone","%" + repayListRequest.getMobilePhone() + "%");
        }
        if ( null != repayListRequest.getStartTime() && null != repayListRequest.getEndTime()){
            criteria.andBetween("repayDate",repayListRequest.getStartTime(), repayListRequest.getEndTime());
        }
        PageHelper.startPage(repayListRequest.getPageNum(),repayListRequest.getPageSize(),repayListRequest.getOrderAndSort());
        List<RepayList> lists = mapper.selectByExample(example);
        PageInfo<RepayList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public PageInfo<RepayList> selectPageNew(RepayListRequest repayListRequest) {
        Example example = new Example(RepayList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != repayListRequest.getMemberId()){
            criteria.andEqualTo("memberId",repayListRequest.getMemberId());
        }
        criteria.andIn("storeId",repayListRequest.getStoreList());

        if (StringUtils.isNotBlank(repayListRequest.getRepayNum())){
            criteria.andLike("repayNum","%" + repayListRequest.getRepayNum() + "%");
        }
        if (StringUtils.isNotBlank(repayListRequest.getMemberName())){
            criteria.andLike("memberName","%" + repayListRequest.getMemberName() + "%");
        }
        if (StringUtils.isNotBlank(repayListRequest.getMobilePhone())){
            criteria.andLike("mobilePhone","%" + repayListRequest.getMobilePhone() + "%");
        }
        if ( null != repayListRequest.getStartTime() && null != repayListRequest.getEndTime()){
            criteria.andBetween("repayDate",repayListRequest.getStartTime(), repayListRequest.getEndTime());
        }
        PageHelper.startPage(repayListRequest.getPageNum(),repayListRequest.getPageSize(),repayListRequest.getOrderAndSort());
        List<RepayList> lists = mapper.selectByExample(example);
        //缓存分页
        PageInfo<RepayList> pageCheck = new PageInfo<>(lists);

        if(lists.size()==0&&pageCheck.getTotal()>lists.size()){
            //计算页数，从最后一页查询
            Integer count=Integer.valueOf(String.valueOf(pageCheck.getTotal()));
            Integer c=count%repayListRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/repayListRequest.getPageSize();
            }else{
                pages=count/repayListRequest.getPageSize()+pages;
            }
            PageHelper.startPage(pages,repayListRequest.getPageSize(),repayListRequest.getOrderAndSort());
            lists =mapper.selectByExample(example);
        }

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

    @Override
    public PageInfo<RepayList> pageDetail(RepayListRequest repayListRequest) {
        Example example = new Example(RepayList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",repayListRequest.getId());
        PageHelper.startPage(repayListRequest.getPageNum(),repayListRequest.getPageSize(),repayListRequest.getOrderAndSort());
        List<RepayList> lists = mapper.selectByExample(example);
        PageInfo<RepayList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

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

    @Override
    public StoreDaily selectRepay(StoreDailyRequest storeDailyRequest) {
        StoreDaily storeDaily=new StoreDaily();
        List<StoreDaily> list=mapper.selectRepay(storeDailyRequest);
        //作废还款单据
        List<StoreDaily> listCancel=mapper.selectRepayCancel(storeDailyRequest);
        list=this.filterRepay(list,listCancel);
        List<String> recNums=new ArrayList<>();
        List<String> courseNums=new ArrayList<>();
        List<String> promotionNums=new ArrayList<>();

        for(StoreDaily storeDaily1:list){
                    if(storeDaily1.getAccountType().equals("rec_repay")){
                        storeDaily.setRecRepay(storeDaily1.getAccountType());
                        recNums.add(storeDaily1.getDocNum());
                    }
                    if(storeDaily1.getAccountType().equals("course_repay") || storeDaily1.getAccountType().equals("card_change")){
                        storeDaily.setCourseRepay(storeDaily1.getAccountType());
                        courseNums.add(storeDaily1.getDocNum());
                    }
                    if(storeDaily1.getAccountType().equals("promotion_course_repay")){
                        storeDaily.setPromotionCourseRepay(storeDaily1.getAccountType());
                        promotionNums.add(storeDaily1.getDocNum());
                    }
        }
        //充值
        if(recNums.size()>0){
            StoreDaily sumRec=mapper.SumRepayByRecharge(recNums);
            storeDaily.setRecRepayMoney(sumRec.getRepayMoney());
        }
        //疗程
       if(courseNums.size()>0){
           StoreDaily sumCourse=mapper.SumRepayByCourse(courseNums);
           if(sumCourse!=null) {
               storeDaily.setCourseRepayMoney(sumCourse.getRepayMoney());
           }
       }
        //促销
        if(promotionNums.size()>0){
            StoreDaily sumPromotion=mapper.SumRepay(promotionNums);

            storeDaily.setPromotionCourseRepayMoney(sumPromotion.getRepayMoney());
        }


        List<String> ids=new ArrayList<>();
        list=this.filterRepay(mapper.selectRepay1(storeDailyRequest),listCancel);
        for(StoreDaily storeDaily1:list){
            ids.add(storeDaily1.getDocNum());
        }
        if(ids.size()>0){
            storeDaily.setIds(ids);
        }
        return storeDaily;
    }

    @Override
    public StoreDaily selectRepayType(StoreDailyRequest storeDailyRequest) {
        List<StoreDaily> storeDailies=mapper.selectRepayType(storeDailyRequest);
        //作废还款单据
        List<StoreDaily> listCancel=mapper.selectRepayCancel(storeDailyRequest);
        storeDailies=this.filterRepay(storeDailies,listCancel);
        List<StoreDaily> list=new ArrayList<>();
        StoreDaily storeDaily=new StoreDaily();
        //疗程还款单数（疗程促销、疗程购买、加价转疗程）
        int course_repay=0;
        //充值还款单数
        int rec_repay=0;
        for(StoreDaily storeDaily1:storeDailies){
            if(storeDaily1.getRepayTypeName().equals("course_repay")||storeDaily1.getRepayTypeName().equals("promotion_course_repay")){
                course_repay++;
            }else if(storeDaily1.getRepayTypeName().equals("rec_repay")){
                rec_repay++;
            }
        }
        storeDaily.setRecRepayNum(rec_repay);
        storeDaily.setCourseRepayNum(course_repay);
        return storeDaily;
    }

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

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

    //筛选出没作废的单据
    public List<StoreDaily> filterRepay(List<StoreDaily> storeDailyList1,List<StoreDaily> storeDailyList2){
        List<StoreDaily> storeDailies=new ArrayList<>();
        storeDailies.addAll(storeDailyList1);
        for(StoreDaily storeDaily1:storeDailyList1){
            for(StoreDaily storeDaily2:storeDailyList2){
                if(storeDaily1.getDocNum().hashCode()==storeDaily2.getDocNum().hashCode()){
                    storeDailies.remove(storeDaily1);
                }
            }
        }
        return storeDailies;
    }
}
