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.SsmDebtDao;
import com.ssm.manage.entity.BaseStores;
import com.ssm.manage.entity.DebtList;
import com.ssm.manage.mapper.DebtListMapper;
import com.ssm.manage.query.DebtListRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.DebtDayVo;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.utils.BigDecimalUtils;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SsmDebtDaoImpl extends BaseDaoImpl<DebtList> implements SsmDebtDao {
    @Autowired
    private DebtListMapper mapper;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

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

    @Override
    public PageInfo<DebtList> selectPage(DebtListRequest debtListRequest) {


        Example example = new Example(DebtList.class);
        Example.Criteria criteria = example.createCriteria();

        if (null != debtListRequest.getStoreId()){
            criteria.andEqualTo("storeId", debtListRequest.getStoreId());
        }
//        if(null != debtListRequest.getMobilePhone()){
//            criteria.andEqualTo("mobilePhone", debtListRequest.getMobilePhone());
//        }
        if (null != debtListRequest.getIds()){
            criteria.andIn("storeId", debtListRequest.getIds());
        }
        if (null != debtListRequest.getMemberId()){
            criteria.andEqualTo("memberId", debtListRequest.getMemberId());
        }
        if (null != debtListRequest.getState()){
            criteria.andEqualTo("state", debtListRequest.getState());
        }
        if (null != debtListRequest.getAccountId()){
            criteria.andEqualTo("accountId", debtListRequest.getAccountId());
        }
        criteria.andGreaterThan("debtMoney", BigDecimalUtils.bigDecimal("0"));
        PageHelper.startPage(debtListRequest.getPageNum(),debtListRequest.getPageSize(),debtListRequest.getOrderAndSort());
        List<DebtList> lists = mapper.selectByExample(example);
//        List<DebtList> card_recharge = new ArrayList<>();
//        for (DebtList list : lists) {
//            if(list.getDocType().equals("card_recharge") && !list.getStoreId().equals(debtListRequest.getStoreId())){
//                break;
//            }else {
//                card_recharge.add(list);
//            }
//        }
        PageInfo<DebtList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }


    @Override
    public PageInfo<DebtList> selectPageNew(DebtListRequest debtListRequest) {
        Example example = new Example(DebtList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != debtListRequest.getStoreId()){
            criteria.andEqualTo("storeId", debtListRequest.getStoreId());
        }
        if (null != debtListRequest.getIds()){
            criteria.andIn("storeId", debtListRequest.getStoreList());
        }
        if (null != debtListRequest.getMemberId()){
            criteria.andEqualTo("memberId", debtListRequest.getMemberId());
        }
        if (null != debtListRequest.getState()){
            criteria.andEqualTo("state", debtListRequest.getState());
        }
        if (null != debtListRequest.getAccountId()){
            criteria.andEqualTo("accountId", debtListRequest.getAccountId());
        }
        criteria.andGreaterThan("debtMoney", BigDecimalUtils.bigDecimal("0"));
        PageHelper.startPage(debtListRequest.getPageNum(),debtListRequest.getPageSize(),debtListRequest.getOrderAndSort());
        List<DebtList> lists = mapper.selectByExample(example);
        PageInfo<DebtList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public List<DebtList> selectList(DebtListRequest debtListRequest) {
        Example example = new Example(DebtList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != debtListRequest.getMemberId()){
            criteria.andEqualTo("memberId", debtListRequest.getMemberId());
        }
        if (null != debtListRequest.getState()){
            criteria.andEqualTo("state", 0);
        }
        criteria.andEqualTo("docType", "card_recharge");
        criteria.andEqualTo("tradeId", debtListRequest.getTradeId());
        criteria.andGreaterThan("debtMoney", BigDecimalUtils.bigDecimal("0"));
        List<DebtList> lists = mapper.selectByExample(example);
        return lists;
    }

    @Override
    public List<DebtList> selectLists(DebtList debtListRequest) {
        Example example = new Example(DebtList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("accountId",  debtListRequest.getAccountId());
        criteria.andEqualTo("state", 0);
        criteria.andGreaterThan("debtMoney", BigDecimalUtils.bigDecimal("0"));
        List<DebtList> lists = mapper.selectByExample(example);
        return lists;
    }

    @Override
    public List<DebtList> queryByDocId(DebtList debtList) {
        Example example = new Example(DebtList.class);
        Example.Criteria criteria = example.createCriteria();
        if(debtList.getDocId()!=null){
            criteria.andEqualTo("docId",  debtList.getDocId());
        }
        if(debtList.getDocNum()!=null){
            criteria.andEqualTo("docNum",  debtList.getDocNum());
        }

        return mapper.selectByExample(example);
    }

    @Override
    public int insertBatch(List<DebtList> list) {
        return mapper.insertBatch(list);
    }

    @Override
    public List<DebtDayVo> selectByDay(List<Long> storeIds, Date startTime, Date endTime) {
        return mapper.selectByDay(storeIds,startTime,endTime);
    }
}
