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.SsmRefundOrderDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.RefundOrderListMapper;
import com.ssm.manage.mapper.RefundRechargeMapper;
import com.ssm.manage.query.*;
import com.ssm.manage.response.RechargeAccountResponse;
import com.ssm.manage.response.RefundDayVo;
import com.ssm.manage.service.*;
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.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class SsmRefundOrderDaoImpl extends BaseDaoImpl<RefundOrderList> implements SsmRefundOrderDao {
    @Autowired
    private RefundOrderListMapper mapper;

    @Autowired
    private SsmPayService ssmPayService;

    @Autowired
    private RefundRechargeMapper refundRechargeMapper;

    @Autowired
    private SsmCashService ssmCashService;

    @Autowired
    private SsmAccountTradeRecordService ssmAccountTradeRecordService;

    @Autowired
    private SsmSalesmanService ssmSalesmanService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmRefundRechargeService ssmRefundRechargeService;
    @Autowired
    private SsmRefundCourseService ssmRefundCourseService;
    @Autowired
    private SsmRefundCashService ssmRefundCashService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreServices;

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

    @Override
    public PageInfo<RefundOrderList> selectPage(RefundOrderResquest refundOrderResquest) {
        PageInfo<RefundOrderList> pageInfo = new PageInfo<>();
        List<RefundOrderList> refundOrderLists = new ArrayList<>();
        BaseStores baseStores = new BaseStores();
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(refundOrderResquest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreServices.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                baseStores = pagesorganization;
            }
        }
        //总店
        if(baseStores.getId().equals(refundOrderResquest.getStoreId())){
            for (BaseStores pagesorganization : pagesorganizations) {
                //本店查询全部
                if (pagesorganization.getId().equals(refundOrderResquest.getStoreId())){
                    Example example = new Example(RefundOrderList.class);
                    Example.Criteria criteria = example.createCriteria();
                    if (null != refundOrderResquest.getType()){
                        criteria.andEqualTo("type", refundOrderResquest.getType());
                    }
                    if (null != refundOrderResquest.getStoreId()){
                        criteria.andEqualTo("storeId", pagesorganization.getId());
                    }
                    if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
                        criteria.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
                    }
                    if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
                        criteria.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
                    }
                    if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
                        criteria.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
                    }
                    if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
                        criteria.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
                    }
                    PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
                    List<RefundOrderList> lists = selectByExample(example);
                    for (RefundOrderList list : lists) {
                        Salesman salesman = new Salesman();
                        salesman.setOrderId(String.valueOf(list.getId()));
                        salesman.setOrderType(4);
                        List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
                        list.setSalesmens(salesmen);
                        BaseStore baseStore = new BaseStore();
                        baseStore.setId(list.getStoreId());
                        BaseStore baseStore1 = ssmBaseStoreServices.selectByPrimaryKey(baseStore.getId());
                        list.setStoreNum(baseStore1.getStoreNum());
                    }
                    refundOrderLists.addAll(lists);
                }else{ //分店查询送审的
                    Example example = new Example(RefundOrderList.class);
                    Example.Criteria criteria = example.createCriteria();
                    if (null != refundOrderResquest.getType()){
                        criteria.andEqualTo("type", refundOrderResquest.getType());
                    }
                    if (null != refundOrderResquest.getStoreId()){
                        criteria.andEqualTo("storeId", pagesorganization.getId());
                    }
                    if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
                        criteria.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
                    }
                    if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
                        criteria.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
                    }
                    if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
                        criteria.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
                    }
                    if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
                        criteria.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
                    }
                    criteria.andIn("state", Arrays.asList(1,3,4));
                    PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
                    List<RefundOrderList> lists = selectByExample(example);
                    for (RefundOrderList list : lists) {
                        Salesman salesman = new Salesman();
                        salesman.setOrderId(String.valueOf(list.getId()));
                        salesman.setOrderType(4);
                        List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
                        list.setSalesmens(salesmen);
                        BaseStore baseStore = new BaseStore();
                        baseStore.setId(list.getStoreId());
                        BaseStore baseStore1 = ssmBaseStoreServices.selectByPrimaryKey(baseStore.getId());
                        list.setStoreNum(baseStore1.getStoreNum());
                        if(list.getRefundReason()==null){
                            list.setRefundReason("");
                        }
                    }
                    refundOrderLists.addAll(lists);
                }
            }
            pageInfo.setList(refundOrderLists);
        }else{
            //分店
            Example example = new Example(RefundOrderList.class);
            Example.Criteria criteria = example.createCriteria();
            if (null != refundOrderResquest.getType()){
                criteria.andEqualTo("type", refundOrderResquest.getType());
            }
            if (null != refundOrderResquest.getStoreId()){
                criteria.andEqualTo("storeId", refundOrderResquest.getStoreId());
            }
            if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
                criteria.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
            }
            if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
                criteria.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
            }
            if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
                criteria.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
            }
            if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
                criteria.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
            }
            PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
            List<RefundOrderList> lists = selectByExample(example);
            for (RefundOrderList list : lists) {
                Salesman salesman = new Salesman();
                salesman.setOrderId(String.valueOf(list.getId()));
                salesman.setOrderType(4);
                List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
                list.setSalesmens(salesmen);
                BaseStore baseStore = new BaseStore();
                baseStore.setId(list.getStoreId());
                BaseStore baseStore1 = ssmBaseStoreServices.selectByPrimaryKey(baseStore.getId());
                list.setStoreNum(baseStore1.getStoreNum());
            }
            pageInfo.setList(lists);
        }



        return pageInfo;
    }
    @Autowired
    private SsmSysRoleStoreService ssmSysRoleStoreService;
    @Override
    public PageInfo<RefundOrderList> selectPageNew(RefundOrderResquest refundOrderResquest) {
        PageInfo<RefundOrderList> pageInfo = new PageInfo<>();
        List<RefundOrderList> refundOrderLists = new ArrayList<>();
        BaseStore baseStore1=new BaseStore();
        List<Long> list2 = new ArrayList<>();
        if(refundOrderResquest.getSelectType()==0){
            list2=refundOrderResquest.getStoreList();
        }
        else{
            baseStore1 = ssmBaseStoreService.selectByPrimaryKey(refundOrderResquest.getStoreId());
            if( baseStore1.getParentId()==null){
                SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
                sysStoreRole.setRoleId(refundOrderResquest.getRoleId());
                PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
                List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
                refundOrderResquest.setStoreList(collect);
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(refundOrderResquest.getStoreId());
//                List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
                //List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
//                List<Long> collect=new ArrayList<>();
//                for(BaseStores baseStores:pagesorganizations){
//                    if(baseStores.getId().hashCode()!=refundOrderResquest.getStoreId().hashCode()){
//                        collect.add(baseStores.getId());
//                    }
//                }
                list2=collect;
            }else{
                list2.add(refundOrderResquest.getStoreId());
            }
        }
        Example example = new Example(RefundOrderList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != refundOrderResquest.getType()){
            criteria.andEqualTo("type", refundOrderResquest.getType());
        }
        criteria.andIn("storeId", list2);
        if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
            criteria.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
        }
        if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
            criteria.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
        }
        if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
            criteria.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
        }
        if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
            criteria.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
        }
        //总店
        if(null == baseStore1.getParentId()&&refundOrderResquest.getStoreId()!=null){
            List<Integer> state=new ArrayList<>();
            state.add(1);
            state.add(3);
            criteria.andIn("state",state );
        }

        int total = selectCountByExample(example);
        PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
        List<RefundOrderList> lists = selectByExample(example);
        //总店
        for (RefundOrderList list : lists) {
            Salesman salesman = new Salesman();
            salesman.setOrderId(String.valueOf(list.getId()));
            salesman.setOrderType(4);
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            list.setSalesmens(salesmen);
            BaseStore baseStore = new BaseStore();
            baseStore.setId(list.getStoreId());
            BaseStore baseStore2 = ssmBaseStoreServices.selectByPrimaryKey(baseStore.getId());
            //查询现付金额与方式
            PayList payList = new PayList();
            payList.setDocId(list.getRefundNum());
            list.setPayLists(ssmPayService.select(payList));
            if(list.getPayLists()!=null && list.getPayLists().size()>0){
                BigDecimal cashAmount = list.getPayLists().stream().map(PayList::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                list.setCashAmount(cashAmount==null?new BigDecimal("0"):cashAmount);
            }
            //查询卡付方式
            AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
            accountTradeRecordRequest.setDocNum(list.getRefundNum());
            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sm.format(new Date());
            accountTradeRecordRequest.setInputDate(list.getRefundDate());
            List<AccountTradeRecord> records = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
            Map<Long,AccountTradeRecord> map = new HashMap<>();
            for (AccountTradeRecord record : records) {
                map.put(record.getAccountId(),record);
            }
            if(records!=null && records.size()>0){
                List<RechargeAccountResponse> collect = records.stream().filter(x->x.getCardMoney()!=null&&x.getCardMoney().compareTo(new BigDecimal("0"))>0).map(item -> {
                    RechargeAccountResponse rechargeAccountResponse = new RechargeAccountResponse();
                    rechargeAccountResponse.setCardRestMoney(item.getAccountRestMoney());
                    rechargeAccountResponse.setCardConsumeMoney(item.getAccountConsumeMoney());
                    rechargeAccountResponse.setCardGiftMoney(item.getAccountGiftRestMoney());
                    rechargeAccountResponse.setSpendMoney(item.getAccountSpendMoney()==null?BigDecimal.ZERO:item.getAccountSpendMoney());
                    rechargeAccountResponse.setAccountGiftSpendMoney(item.getAccountGiftSpendMoney());
                    rechargeAccountResponse.setRechargeTypeName(item.getAccountName());
                    return rechargeAccountResponse;
                }).collect(Collectors.toList());
                list.setRechargeAccountResponses(collect);
                if(collect!=null && collect.size()>0){
                    BigDecimal cardAomunt = collect.stream().filter(x->x.getSpendMoney()!=null).map(RechargeAccountResponse::getSpendMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                    list.setCardAmount(cardAomunt==null?new BigDecimal("0"):cardAomunt);
                    BigDecimal refundGiftMoney = collect.stream().filter(x -> x.getAccountGiftSpendMoney() != null).map(RechargeAccountResponse::getAccountGiftSpendMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                    list.setRefundGiftMoney(refundGiftMoney);
                }
            }
            list.setStoreNum(baseStore2.getStoreNum());
        }
        refundOrderLists.addAll(lists);
        Collections.sort(refundOrderLists, (o1, o2) -> o2.getRefundDate().compareTo(o1.getRefundDate()));
        pageInfo.setList(refundOrderLists);
        PageInfo<RefundOrderList> pageInfo1 =new PageInfo<>(refundOrderLists);
        pageInfo1.setTotal(total);
        return pageInfo1;
    }

    @Override
    public PageInfo<RefundOrderList> memberPageNew(RefundOrderResquest refundOrderResquest) {
        PageInfo<RefundOrderList> pageInfo = new PageInfo<>();
        List<RefundOrderList> refundOrderLists = new ArrayList<>();
        Example example = new Example(RefundOrderList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != refundOrderResquest.getType()){
            criteria.andEqualTo("type", refundOrderResquest.getType());
        }
        if(refundOrderResquest.getMemberId()!=null){
            criteria.andEqualTo("memberId", refundOrderResquest.getMemberId());
            criteria.andEqualTo("state",1);
        }

        int total = selectCountByExample(example);
        PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
        List<RefundOrderList> lists = selectByExample(example);
        for (RefundOrderList list : lists) {
            Salesman salesman = new Salesman();
            salesman.setOrderId(String.valueOf(list.getId()));
            salesman.setOrderType(4);
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            list.setSalesmens(salesmen);
            BaseStore baseStore = new BaseStore();
            baseStore.setId(list.getStoreId());
            BaseStore baseStore2 = ssmBaseStoreServices.selectByPrimaryKey(baseStore.getId());
            //查询现付金额与方式
            PayList payList = new PayList();
            payList.setDocId(list.getRefundNum());
            list.setPayLists(ssmPayService.select(payList));
            if(list.getPayLists()!=null && list.getPayLists().size()>0){
                BigDecimal cashAmount = list.getPayLists().stream().map(PayList::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                list.setCashAmount(cashAmount==null?new BigDecimal("0"):cashAmount);
            }
            //查询卡付方式
            AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
            accountTradeRecordRequest.setDocNum(list.getRefundNum());
            accountTradeRecordRequest.setInputDate(list.getRefundDate());
            List<AccountTradeRecord> records = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
            Map<Long,AccountTradeRecord> map = new HashMap<>();
            for (AccountTradeRecord record : records) {
                map.put(record.getAccountId(),record);
            }
            if(records!=null && records.size()>0){
                List<RechargeAccountResponse> collect = records.stream().filter(x->x.getCardMoney()!=null&&x.getCardMoney().compareTo(new BigDecimal("0"))>0).map(item -> {
                    RechargeAccountResponse rechargeAccountResponse = new RechargeAccountResponse();
                    rechargeAccountResponse.setCardRestMoney(item.getAccountRestMoney());
                    rechargeAccountResponse.setCardConsumeMoney(item.getAccountConsumeMoney());
                    rechargeAccountResponse.setCardGiftMoney(item.getAccountGiftRestMoney());
                    rechargeAccountResponse.setSpendMoney(item.getAccountSpendMoney()==null?BigDecimal.ZERO:item.getAccountSpendMoney());
                    rechargeAccountResponse.setAccountGiftSpendMoney(item.getAccountGiftSpendMoney());
                    rechargeAccountResponse.setRechargeTypeName(item.getAccountName());
                    return rechargeAccountResponse;
                }).collect(Collectors.toList());
                list.setRechargeAccountResponses(collect);
                if(collect!=null && collect.size()>0){
                    BigDecimal cardAomunt = collect.stream().filter(x->x.getSpendMoney()!=null).map(RechargeAccountResponse::getSpendMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                    list.setCardAmount(cardAomunt==null?new BigDecimal("0"):cardAomunt);
                    BigDecimal refundGiftMoney = collect.stream().filter(x -> x.getAccountGiftSpendMoney() != null).map(RechargeAccountResponse::getAccountGiftSpendMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                    list.setRefundGiftMoney(refundGiftMoney);
                }
            }
            list.setStoreNum(baseStore2.getStoreNum());
        }
        refundOrderLists.addAll(lists);
        Collections.sort(refundOrderLists, (o1, o2) -> o2.getRefundDate().compareTo(o1.getRefundDate()));
        pageInfo.setList(refundOrderLists);
        PageInfo<RefundOrderList> pageInfo1 =new PageInfo<>(refundOrderLists);
        pageInfo1.setTotal(total);
        return pageInfo1;
    }

    @Override
    public PageInfo<RefundRecharge> selectByRechargeRefund(RefundOrderResquest refundOrderResquest) {
        PageInfo<RefundRecharge> pageInfo = new PageInfo<>();
        PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
        List<RefundRecharge> refundRecharges = refundRechargeMapper.selectListsByRefundRecharge(refundOrderResquest);
        pageInfo.setList(refundRecharges);
        PageInfo<RefundRecharge> pageInfo1 =new PageInfo<>(refundRecharges);


        return pageInfo1;
    }

    @Override
    public List<RefundOrderList> refundCourseCash(RefundOrderResquest refundOrderResquest) {
        PageInfo<RefundOrderList> pageInfo = new PageInfo<>();
        List<RefundOrderList> refundOrderLists = new ArrayList<>();
        BaseStore baseStore1=new BaseStore();
        List<Long> list2 = new ArrayList<>();
        if(refundOrderResquest.getSelectType()==0){
            list2=refundOrderResquest.getStoreList();
        }else{

            baseStore1 = ssmBaseStoreService.selectByPrimaryKey(refundOrderResquest.getStoreId());
            if( baseStore1.getParentId()==null){
                SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
                sysStoreRole.setRoleId(refundOrderResquest.getRoleId());
                PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
                List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
                refundOrderResquest.setStoreList(collect);
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(refundOrderResquest.getStoreId());
//                List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
                //List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
//                List<Long> collect=new ArrayList<>();
//                for(BaseStores baseStores:pagesorganizations){
//                    if(baseStores.getId().hashCode()!=refundOrderResquest.getStoreId().hashCode()){
//                        collect.add(baseStores.getId());
//                    }
//                }
                list2=collect;
            }else{
                list2.add(refundOrderResquest.getStoreId());
            }
        }
        Example example = new Example(RefundOrderList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != refundOrderResquest.getType()){
            criteria.andEqualTo("type", refundOrderResquest.getType());
        }
        criteria.andIn("storeId", list2);
        if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
            criteria.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
        }
        if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
            criteria.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
        }
        if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
            criteria.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
        }
        if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
            criteria.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
        }
        criteria.andEqualTo("state",3);
        //总店
//        if(null == baseStore1.getParentId()&&refundOrderResquest.getStoreId()!=null){
//            List<Integer> state=new ArrayList<>();
//            state.add(1);
//            state.add(3);
//            criteria.andIn("state",state );
//        }
//        PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
        List<RefundOrderList> lists = selectByExample(example);
        //总店
//        List<RefundOrderList> lists1=new ArrayList<>();
//        if(null == baseStore1.getParentId()&&refundOrderResquest.getStoreId()!=null){
//            Example example1 = new Example(RefundOrderList.class);
//            Example.Criteria criteria1 = example1.createCriteria();
//            if (null != refundOrderResquest.getType()){
//                criteria1.andEqualTo("type", refundOrderResquest.getType());
//            }
//            criteria1.andEqualTo("storeId", refundOrderResquest.getStoreId());
//            if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
//                criteria1.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
//            }
//            if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
//                criteria1.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
//            }
//            if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
//                criteria1.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
//            }
//            if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
//                criteria1.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
//            }
//            PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
//            lists1 = selectByExample(example1);
//        }
//        if(lists1.size()>0)
//            lists.addAll(lists1);
        List<RefundOrderList> refundCourseCash = new ArrayList<>();
        Set<RefundOrderList> set = new LinkedHashSet<>();
        for (RefundOrderList list : lists) {

            Salesman salesman = new Salesman();
            salesman.setOrderId(String.valueOf(list.getId()));
            salesman.setOrderType(4);
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            list.setSalesmens(salesmen);
            BaseStore baseStore = new BaseStore();
            baseStore.setId(list.getStoreId());
            BaseStore baseStore2 = ssmBaseStoreServices.selectByPrimaryKey(baseStore.getId());

            //查询现付金额与方式
            PayList payList = new PayList();
            payList.setDocId(list.getRefundNum());
            list.setPayLists(ssmPayService.select(payList));
            if(list.getPayLists()!=null && list.getPayLists().size()>0){
                BigDecimal cashAmount = list.getPayLists().stream().map(PayList::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                list.setCashAmount(cashAmount==null?new BigDecimal("0"):cashAmount);
            }
            //查询卡付方式
            AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
            accountTradeRecordRequest.setDocNum(list.getRefundNum());
            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sm.format(new Date());
            accountTradeRecordRequest.setInputDate(list.getRefundDate());
            List<AccountTradeRecord> records = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
            if(records!=null && records.size()>0){
                List<RechargeAccountResponse> collect = records.stream().filter(x->x.getCardMoney()!=null&&x.getCardMoney().compareTo(new BigDecimal("0"))>0).map(item -> {
                    RechargeAccountResponse rechargeAccountResponse = new RechargeAccountResponse();
                    rechargeAccountResponse.setCardRestMoney(item.getAccountRestMoney());
                    rechargeAccountResponse.setCardConsumeMoney(item.getAccountConsumeMoney());
                    rechargeAccountResponse.setCardGiftMoney(item.getAccountGiftRestMoney());
                    rechargeAccountResponse.setSpendMoney(item.getAccountSpendMoney());
                    rechargeAccountResponse.setRechargeTypeName(item.getAccountName());
                    return rechargeAccountResponse;
                }).collect(Collectors.toList());
                list.setRechargeAccountResponses(collect);
                if(collect!=null && collect.size()>0){
                    BigDecimal cardAomunt = collect.stream().filter(x->x.getSpendMoney()!=null).map(RechargeAccountResponse::getSpendMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                    list.setCardAmount(cardAomunt==null?new BigDecimal("0"):cardAomunt);
                }
            }
            list.setStoreNum(baseStore2.getStoreNum());

            RefundCash refundCash = new RefundCash();
            refundCash.setStoreId(list.getStoreId());
            refundCash.setRefundNum(list.getRefundNum());
            List<RefundCash> select1 = ssmRefundCashService.select(refundCash);
            for (RefundCash cash : select1) {
                CashList cashList = new CashList();
                cashList.setId(cash.getCashId());
                cashList.setCashDate(cash.getCashDate());
                List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                CashList cashList1 = cashLists.get(0);
                if(refundOrderResquest.getOrderType()!=null && refundOrderResquest.getOrderType()==1)
                {
                    if(cashList1.getCourseAccountId()!=null){
                        set.add(list);
//                        refundCourseCash.add(list);
                    }
                }else if(refundOrderResquest.getOrderType()!=null && refundOrderResquest.getOrderType()==2){
                    if(cashList1.getCollectionRechargeAmount()!=null && cashList1.getCollectionRechargeAmount()
                            .subtract(cashList1.getCollectionGiftAmount()==null?BigDecimal.ZERO:cashList1.getCollectionGiftAmount())
                            .compareTo(BigDecimal.ZERO)==1){
                        set.add(list);
//                        refundCourseCash.add(list);
                    }
                }
                else if(refundOrderResquest.getOrderType()!=null && refundOrderResquest.getOrderType()==3){
                    if(cashList1.getCollectionGiftAmount()!=null &&cashList1.getCollectionGiftAmount().compareTo(BigDecimal.ZERO)==1){
                        set.add(list);
//                        refundCourseCash.add(list);
                    }
                }
            }


        }
        for (RefundOrderList refundOrderList : set) {

            refundCourseCash.add(refundOrderList);
        }
//        refundOrderLists.addAll(lists);
        Collections.sort(refundCourseCash, new Comparator<RefundOrderList>() {
            @Override
            public int compare(RefundOrderList o1, RefundOrderList o2) {
                return o2.getRefundDate().compareTo(o1.getRefundDate());
            }
        });
//        pageInfo.setList(refundOrderLists);

        return refundCourseCash;
    }

    @Override
    public PageInfo<RefundOrderList> selectPageFd(RefundOrderResquest refundOrderResquest) {
        PageInfo<RefundOrderList> pageInfo = new PageInfo<>();
                StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(refundOrderResquest.getStoreId());
        //总店
        BaseStores baseStore2 = new BaseStores();
        List<BaseStores> pagesorganizations = ssmBaseStoreServices.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                baseStore2= pagesorganization;
            }
        }
        List<RefundOrderList> lista = new ArrayList<>();
        for (BaseStores pagesorganization : pagesorganizations) {
            if(!baseStore2.getId().equals(pagesorganization.getId())){
                Example example = new Example(RefundOrderList.class);
                Example.Criteria criteria = example.createCriteria();
                if (null != refundOrderResquest.getType()){
                    criteria.andEqualTo("type", refundOrderResquest.getType());
                }
                if (null != refundOrderResquest.getStoreId()){
                    criteria.andEqualTo("storeId", pagesorganization.getId());
                }
                if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
                    criteria.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
                }
                if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
                    criteria.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
                }
                if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
                    criteria.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
                }
                if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
                    criteria.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
                }
                PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
                List<RefundOrderList> lists = selectByExample(example);
                for (RefundOrderList list : lists) {
                    Salesman salesman = new Salesman();
                    salesman.setOrderId(String.valueOf(list.getId()));
                    salesman.setOrderType(4);
                    List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
                    list.setSalesmens(salesmen);
                    BaseStore baseStore = new BaseStore();
                    baseStore.setId(list.getStoreId());
                    BaseStore baseStore1 = ssmBaseStoreServices.selectByPrimaryKey(baseStore.getId());
                    list.setStoreNum(baseStore1.getStoreNum());
                }
                lista.addAll(lists);
            }
        }
        pageInfo.setList(lista);

        return pageInfo;
    }

    @Override
    public PageInfo<RefundOrderList> pageDetail(RefundOrderResquest refundOrderResquest) {
        Example example = new Example(RefundOrderList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", refundOrderResquest.getId());
        if (null != refundOrderResquest.getType()){
            criteria.andEqualTo("type", refundOrderResquest.getType());
        }
        PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
        List<RefundOrderList> lists = selectByExample(example);
        RefundOrderList refundOrderList = new RefundOrderList();
        if(null == lists || 0==lists.size()){
            if(0==refundOrderResquest.getType()){//充值
                RefundRecharge refundRecharge = new RefundRecharge();
                refundRecharge.setId(refundOrderResquest.getId());
                RefundRecharge refundRecharge1 = ssmRefundRechargeService.selectOne(refundRecharge);
                refundOrderList.setRefundNum(refundRecharge1.getRefundNum());
                refundOrderList.setStoreId(refundRecharge1.getStoreId());

            }else if(1==refundOrderResquest.getType()){//疗程
                RefundCourse refundCourse = new RefundCourse();
                refundCourse.setId(refundOrderResquest.getId());
                RefundCourse refundCourse1 = ssmRefundCourseService.selectOne(refundCourse);
                refundOrderList.setRefundNum(refundCourse1.getRefundNum());
                refundOrderList.setStoreId(refundCourse1.getStoreId());

            }else if(2==refundOrderResquest.getType()){//收银
                RefundCash refundCash = new RefundCash();
                refundCash.setId(refundOrderResquest.getId());
                RefundCash refundCash1 = ssmRefundCashService.selectOne(refundCash);
                refundOrderList.setRefundNum(refundCash1.getRefundNum());
                refundOrderList.setStoreId(refundCash1.getStoreId());
            }
            Example example1 = new Example(RefundOrderList.class);
            refundOrderList.setType(refundOrderResquest.getType());
            lists = mapper.select(refundOrderList);
        }
        for (RefundOrderList list : lists) {
            Salesman salesman = new Salesman();
            salesman.setOrderId(String.valueOf(list.getId()));
            salesman.setOrderType(4);
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            list.setSalesmens(salesmen);
        }
        PageInfo<RefundOrderList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

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

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

    @Override
    public List<RefundOrderList> selectFKJ(RefundOrderList refundOrderResquest){
        return mapper.selectFKJ(refundOrderResquest);
    }

    @Override
    public PageInfo<RefundOrderList> refundByMemberId(RefundOrderResquest refundOrderResquest) {
        PageInfo<RefundOrderList> pageInfo = new PageInfo<>();
        List<RefundOrderList> refundOrderLists = new ArrayList<>();
        Example example = new Example(RefundOrderList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != refundOrderResquest.getType()){
            criteria.andEqualTo("type", refundOrderResquest.getType());
        }
        if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
            criteria.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
        }
        if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
            criteria.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
        }
        if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
            criteria.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
        }
        if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
            criteria.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
        }
        if(refundOrderResquest.getMemberId()!=null){
            criteria.andEqualTo("memberId",refundOrderResquest.getMemberId());
            List<Integer> state=new ArrayList<>();
            state.add(1);
            state.add(3);
            criteria.andIn("state",state );
        }
        int total = selectCountByExample(example);
        PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
        List<RefundOrderList> lists = selectByExample(example);
        //总店
//        List<RefundOrderList> lists1=new ArrayList<>();
//        if(null == baseStore1.getParentId()&&refundOrderResquest.getStoreId()!=null){
//            Example example1 = new Example(RefundOrderList.class);
//            Example.Criteria criteria1 = example1.createCriteria();
//            if (null != refundOrderResquest.getType()){
//                criteria1.andEqualTo("type", refundOrderResquest.getType());
//            }
//            criteria1.andEqualTo("storeId", refundOrderResquest.getStoreId());
//            if (StringUtils.isNotBlank(refundOrderResquest.getRefundNum())){
//                criteria1.andLike("refundNum","%" + refundOrderResquest.getRefundNum() + "%");
//            }
//            if (StringUtils.isNotBlank(refundOrderResquest.getMemberName())){
//                criteria1.andLike("memberName","%" + refundOrderResquest.getMemberName() + "%");
//            }
//            if (StringUtils.isNotBlank(refundOrderResquest.getMobilePhone())){
//                criteria1.andLike("mobilePhone","%" + refundOrderResquest.getMobilePhone() + "%");
//            }
//            if ( null != refundOrderResquest.getStartTime() && null != refundOrderResquest.getEndTime()){
//                criteria1.andBetween("refundDate",refundOrderResquest.getStartTime(), refundOrderResquest.getEndTime());
//            }
//            PageHelper.startPage(refundOrderResquest.getPageNum(),refundOrderResquest.getPageSize(),refundOrderResquest.getOrderAndSort());
//            lists1 = selectByExample(example1);
//        }
//        if(lists1.size()>0)
//            lists.addAll(lists1);

        for (RefundOrderList list : lists) {
            Salesman salesman = new Salesman();
            salesman.setOrderId(String.valueOf(list.getId()));
            salesman.setOrderType(4);
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            list.setSalesmens(salesmen);
            BaseStore baseStore = new BaseStore();
            baseStore.setId(list.getStoreId());
            BaseStore baseStore2 = ssmBaseStoreServices.selectByPrimaryKey(baseStore.getId());
            //查询现付金额与方式
            PayList payList = new PayList();
            payList.setDocId(list.getRefundNum());
            list.setPayLists(ssmPayService.select(payList));
            if(list.getPayLists()!=null && list.getPayLists().size()>0){
                BigDecimal cashAmount = list.getPayLists().stream().map(PayList::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                list.setCashAmount(cashAmount==null?new BigDecimal("0"):cashAmount);
            }
            //查询卡付方式
            AccountTradeRecordRequest accountTradeRecordRequest = new AccountTradeRecordRequest();
            accountTradeRecordRequest.setDocNum(list.getRefundNum());
            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sm.format(new Date());
            accountTradeRecordRequest.setInputDate(list.getRefundDate());
            List<AccountTradeRecord> records = ssmAccountTradeRecordService.selectList(accountTradeRecordRequest);
            if(records!=null && records.size()>0){
                List<RechargeAccountResponse> collect = records.stream().filter(x->x.getCardMoney()!=null&&x.getCardMoney().compareTo(new BigDecimal("0"))>0).map(item -> {
                    RechargeAccountResponse rechargeAccountResponse = new RechargeAccountResponse();
                    rechargeAccountResponse.setCardRestMoney(item.getAccountRestMoney());
                    rechargeAccountResponse.setCardConsumeMoney(item.getAccountConsumeMoney());
                    rechargeAccountResponse.setCardGiftMoney(item.getAccountGiftRestMoney());
                    rechargeAccountResponse.setSpendMoney(item.getAccountSpendMoney());
                    rechargeAccountResponse.setRechargeTypeName(item.getAccountName());
                    return rechargeAccountResponse;
                }).collect(Collectors.toList());
                list.setRechargeAccountResponses(collect);
                if(collect!=null && collect.size()>0){
                    BigDecimal cardAomunt = collect.stream().filter(x->x.getSpendMoney()!=null).map(RechargeAccountResponse::getSpendMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                    list.setCardAmount(cardAomunt==null?new BigDecimal("0"):cardAomunt);
                }
            }
            list.setStoreNum(baseStore2.getStoreNum());
        }
        refundOrderLists.addAll(lists);
        Collections.sort(refundOrderLists, new Comparator<RefundOrderList>() {
            @Override
            public int compare(RefundOrderList o1, RefundOrderList o2) {
                return o2.getRefundDate().compareTo(o1.getRefundDate());
            }
        });
        pageInfo.setList(refundOrderLists);
        PageInfo<RefundOrderList> pageInfo1 =new PageInfo<>(refundOrderLists);
        pageInfo1.setTotal(total);
        return pageInfo1;
    }
}
