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.SsmCardChangeDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.CardChangeListMapper;
import com.ssm.manage.query.CardChangeListRequest;
import com.ssm.manage.query.StoreDailyRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.CardChangeDayVo;
import com.ssm.manage.response.RateContribution;
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.util.*;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;

@Component
public class SsmCardChangeDaoImpl extends BaseDaoImpl<CardChangeList> implements SsmCardChangeDao {
    @Autowired
    private CardChangeListMapper mapper;

    @Autowired
    private SsmSalesmanService ssmSalesmanService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;
    @Autowired
    private SsmCardChangeInProjectService ssmCardChangeInProjectService;
    @Autowired
    private SsmCardChangeOutProjectService ssmCardChangeOutProjectService;

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

    @Override
    public PageInfo<CardChangeList> selectPage(CardChangeListRequest cardChangeListRequest) {
        List<CardChangeList> returnArray = new ArrayList<>();
        PageInfo<CardChangeList> pageInfo = new PageInfo<>();
        //总店
        BaseStores baseStores = new BaseStores();
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(cardChangeListRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                baseStores = pagesorganization;
            }
        }

        //判断是否是总店
        if(baseStores.getId().equals(cardChangeListRequest.getStoreId())){
            List<CardChangeList> listsArray = new ArrayList<>();
            //总店
            for (BaseStores pagesorganization : pagesorganizations) {
                if(null == pagesorganization.getParentId()){
                    Example example = new Example(CardChangeList.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("storeId", pagesorganization.getId());
                    if (null != cardChangeListRequest.getMemberId()){
                        criteria.andEqualTo("memberId", cardChangeListRequest.getMemberId());
                    }
                    if (StringUtils.isNotBlank(cardChangeListRequest.getMemberName())){
                        criteria.andLike("memberName", "%" + cardChangeListRequest.getMemberName() + "%");
                    }
                    if (StringUtils.isNotBlank(cardChangeListRequest.getMobilePhone())){
                        criteria.andLike("mobilePhone", "%" + cardChangeListRequest.getMobilePhone() + "%");
                    }
                    if (StringUtils.isNotBlank(cardChangeListRequest.getDocNum())){
                        criteria.andLike("docNum", "%" + cardChangeListRequest.getDocNum() + "%");
                    }
                    if (null != cardChangeListRequest.getStartTime() && null != cardChangeListRequest.getEndTime()){
                        criteria.andBetween("docDate", cardChangeListRequest.getStartTime(), cardChangeListRequest.getEndTime());
                    }
                    if (null != cardChangeListRequest.getType()){
                        if (0 == cardChangeListRequest.getType())
                            criteria.andLike("docNum", "JJ" + cardChangeListRequest.getDocNum() + "%");
                        else
                            criteria.andLike("docNum", "NT" + cardChangeListRequest.getDocNum() + "%");
                    }
                    List<CardChangeList> lists = mapper.selectByExample(example);
                    listsArray.addAll(lists);
                }else{
                    Example example = new Example(CardChangeList.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("storeId", pagesorganization.getId());
                    if (null != cardChangeListRequest.getMemberId()){
                        criteria.andEqualTo("memberId", cardChangeListRequest.getMemberId());
                    }
                    if (StringUtils.isNotBlank(cardChangeListRequest.getMemberName())){
                        criteria.andLike("memberName", "%" + cardChangeListRequest.getMemberName() + "%");
                    }
                    if (StringUtils.isNotBlank(cardChangeListRequest.getMobilePhone())){
                        criteria.andLike("mobilePhone", "%" + cardChangeListRequest.getMobilePhone() + "%");
                    }
                    if (StringUtils.isNotBlank(cardChangeListRequest.getDocNum())){
                        criteria.andLike("docNum", "%" + cardChangeListRequest.getDocNum() + "%");
                    }
                    if (null != cardChangeListRequest.getStartTime() && null != cardChangeListRequest.getEndTime()){
                        criteria.andBetween("docDate", cardChangeListRequest.getStartTime(), cardChangeListRequest.getEndTime());
                    }
                    if (null != cardChangeListRequest.getType()){
                        if (0 == cardChangeListRequest.getType())
                            criteria.andLike("docNum", "JJ" + cardChangeListRequest.getDocNum() + "%");
                        else
                            criteria.andLike("docNum", "NT" + cardChangeListRequest.getDocNum() + "%");
                    }
                    criteria.andIn("state", Arrays.asList(1,3));
                    List<CardChangeList> lists = mapper.selectByExample(example);
                    for (CardChangeList list : lists) {
                        if(list.getChangeInPayMoney().compareTo(BigDecimal.valueOf(0))==0){
                            listsArray.add(list);
                        }
                    }
                }
            }
            returnArray.addAll(listsArray);
        }else{
            //分店
            Example example = new Example(CardChangeList.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("storeId", cardChangeListRequest.getStoreId());
            if (null != cardChangeListRequest.getMemberId()){
                criteria.andEqualTo("memberId", cardChangeListRequest.getMemberId());
            }
            if (StringUtils.isNotBlank(cardChangeListRequest.getMemberName())){
                criteria.andLike("memberName", "%" + cardChangeListRequest.getMemberName() + "%");
            }
            if (StringUtils.isNotBlank(cardChangeListRequest.getMobilePhone())){
                criteria.andLike("mobilePhone", "%" + cardChangeListRequest.getMobilePhone() + "%");
            }
            if (StringUtils.isNotBlank(cardChangeListRequest.getDocNum())){
                criteria.andLike("docNum", "%" + cardChangeListRequest.getDocNum() + "%");
            }
            if (null != cardChangeListRequest.getStartTime() && null != cardChangeListRequest.getEndTime()){
                criteria.andBetween("docDate", cardChangeListRequest.getStartTime(), cardChangeListRequest.getEndTime());
            }
            if (null != cardChangeListRequest.getType()){
                if (0 == cardChangeListRequest.getType())
                    criteria.andLike("docNum", "JJ" + cardChangeListRequest.getDocNum() + "%");
                else
                    criteria.andLike("docNum", "NT" + cardChangeListRequest.getDocNum() + "%");
            }
            List<CardChangeList> lists = mapper.selectByExample(example);
            returnArray.addAll(lists);
        }
        int pageNum = 0;
        int pageSize = 0;
        if(cardChangeListRequest.getPageNum()!=1){
            pageNum=(cardChangeListRequest.getPageNum()*cardChangeListRequest.getPageSize())-11;
        }
        if(pageNum==0){
            pageSize = cardChangeListRequest.getPageSize();
        }else{
            pageSize = (cardChangeListRequest.getPageNum()*cardChangeListRequest.getPageSize());
        }
        if(pageSize>returnArray.size()){
            pageSize= returnArray.size();
        }

        returnArray.sort(Comparator.comparing(CardChangeList::getDocDate, Comparator.naturalOrder()).reversed().thenComparing(CardChangeList::getState, Comparator.reverseOrder()));

        List<CardChangeList> rateContributions2 = returnArray.subList(pageNum, pageSize);
        for (CardChangeList list : rateContributions2) {
            Salesman salesman = new Salesman();
            salesman.setOrderId(list.getId().toString());
            salesman.setOrderNum(list.getDocNum());
            salesman.setOrderType(3);
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            list.setSalesmens(salesmen);
        }
        pageInfo.setTotal(returnArray.size());
        pageInfo.setList(rateContributions2);
        return pageInfo;
    }

    @Override
    public PageInfo<CardChangeList> selectPageNew(CardChangeListRequest cardChangeListRequest) {
        List<Long> lists = new ArrayList<>();
        if(cardChangeListRequest.getSelectType()==0){
            lists=cardChangeListRequest.getStoreList();
        }else{
            BaseStore baseStore1 = ssmBaseStoreService.selectByPrimaryKey(cardChangeListRequest.getStoreId());
            if(null == baseStore1.getParentId()){
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(cardChangeListRequest.getStoreId());
                List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
                List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
                lists=collect;
            }else{
                lists.add(cardChangeListRequest.getStoreId());
            }

        }
        //判断是否是总店
        Example example = new Example(CardChangeList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("storeId", lists);
        if (null != cardChangeListRequest.getMemberId()){
            criteria.andEqualTo("memberId", cardChangeListRequest.getMemberId());
        }
        if (StringUtils.isNotBlank(cardChangeListRequest.getMemberName())){
            criteria.andLike("memberName", "%" + cardChangeListRequest.getMemberName() + "%");
        }
        if (StringUtils.isNotBlank(cardChangeListRequest.getMobilePhone())){
            criteria.andLike("mobilePhone", "%" + cardChangeListRequest.getMobilePhone() + "%");
        }
        if (StringUtils.isNotBlank(cardChangeListRequest.getDocNum())){
            criteria.andLike("docNum", "%" + cardChangeListRequest.getDocNum() + "%");
        }
        if (null != cardChangeListRequest.getStartTime() && null != cardChangeListRequest.getEndTime()){
            criteria.andBetween("docDate", cardChangeListRequest.getStartTime(), cardChangeListRequest.getEndTime());
        }
        if (null != cardChangeListRequest.getType()){
            if (0 == cardChangeListRequest.getType())
                criteria.andLike("docNum", "ZLC" + cardChangeListRequest.getDocNum() + "%");
            else
                criteria.andLike("docNum", "LCNT" + cardChangeListRequest.getDocNum() + "%");
        }
        if(cardChangeListRequest.getCourseType()==1){
            criteria.andLike("docNum","ZLC%");
        }else if (cardChangeListRequest.getCourseType()==0){
            criteria.andLike("docNum","LCNT%");
        }
        if(cardChangeListRequest.getState()!=null){
            criteria.andEqualTo("state",cardChangeListRequest.getState());
        }
        PageHelper.startPage(cardChangeListRequest.getPageNum(),cardChangeListRequest.getPageSize(),cardChangeListRequest.getOrderAndSort());
        List<CardChangeList> listcard = mapper.selectByExample(example);

        //缓存分页
        PageInfo<CardChangeList> pageCheck =new PageInfo<>(listcard);
        if(listcard.size()==0&&pageCheck.getTotal()>listcard.size()){
            //计算页数，从最后一页查询
            Integer count=Integer.valueOf(String.valueOf(pageCheck.getTotal()));
            Integer c=count%cardChangeListRequest.getPageSize();
            Integer pages=1;
            if(c==0){
                pages=count/cardChangeListRequest.getPageSize();
            }else{
                pages=count/cardChangeListRequest.getPageSize()+pages;
            }
            PageHelper.startPage(pages,cardChangeListRequest.getPageSize(),cardChangeListRequest.getOrderAndSort());
            listcard = mapper.selectByExample(example);
        }
        PageInfo<CardChangeList> pageInfo = new PageInfo<>(listcard);
        for (CardChangeList list : pageInfo.getList()) {
            Salesman salesman = new Salesman();
            salesman.setOrderId(list.getId().toString());
            salesman.setOrderNum(list.getDocNum());
            salesman.setOrderType(3);
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            list.setSalesmens(salesmen);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CardChangeList> pageDetail(CardChangeListRequest cardChangeListRequest) {
        Example example = new Example(CardChangeList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", cardChangeListRequest.getId());
        PageHelper.startPage(cardChangeListRequest.getPageNum(), cardChangeListRequest.getPageSize(), cardChangeListRequest.getOrderAndSort());
        List<CardChangeList> lists = mapper.selectByExample(example);
        if(null == lists|| 0==lists.size()){
            CardChangeInProject cardChangeInProject = new CardChangeInProject();
            cardChangeInProject.setId(cardChangeListRequest.getId());
            CardChangeInProject cardChangeInProject1 = ssmCardChangeInProjectService.selectOne(cardChangeInProject);

            if (null == cardChangeInProject1){
                CardChangeOutProject cardChangeOutProject = new CardChangeOutProject();
                cardChangeOutProject.setId(cardChangeListRequest.getId());
                CardChangeOutProject cardChangeOutProject1 = ssmCardChangeOutProjectService.selectOne(cardChangeOutProject);

                CardChangeList cardChangeList = new CardChangeList();
                cardChangeList.setStoreId(cardChangeOutProject1.getStoreId());
                cardChangeList.setDocNum(cardChangeOutProject1.getDocNum());
                CardChangeList cardChangeList1 = mapper.selectOne(cardChangeList);
                Salesman salesman = new Salesman();
                salesman.setOrderId(cardChangeList1.getId().toString());
                salesman.setOrderNum(cardChangeList1.getDocNum());
                salesman.setOrderType(3);
                List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
                cardChangeList1.setSalesmens(salesmen);
                List<CardChangeList> list = new ArrayList<>();
                list.add(cardChangeList1);
                PageInfo<CardChangeList> pageInfo = new PageInfo<>(list);
                return pageInfo;
            }


            CardChangeList cardChangeList = new CardChangeList();
            cardChangeList.setStoreId(cardChangeInProject1.getStoreId());
            cardChangeList.setDocNum(cardChangeInProject1.getDocNum());
            CardChangeList cardChangeList1 = mapper.selectOne(cardChangeList);
            Salesman salesman = new Salesman();
            salesman.setOrderId(cardChangeList1.getId().toString());
            salesman.setOrderNum(cardChangeList1.getDocNum());
            salesman.setOrderType(3);
            List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
            cardChangeList1.setSalesmens(salesmen);
            List<CardChangeList> list = new ArrayList<>();
            list.add(cardChangeList1);
            PageInfo<CardChangeList> pageInfo = new PageInfo<>(list);
            return pageInfo;
        }else{
            for (CardChangeList list : lists) {
                Salesman salesman = new Salesman();
                salesman.setOrderId(list.getId().toString());
                salesman.setOrderNum(list.getDocNum());
                salesman.setOrderType(3);
                List<Salesman> salesmen = ssmSalesmanService.selectList(salesman);
                list.setSalesmens(salesmen);
            }
            PageInfo<CardChangeList> pageInfo = new PageInfo<>(lists);
            return pageInfo;
        }
    }

    @Override
    public  List<CardChangeList> selectListSalem(CardChangeList cardChangeList){
        Example example = new Example(CardChangeList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != cardChangeList.getId()){
            criteria.andEqualTo("id", cardChangeList.getId());
        }
        List<CardChangeList> lists = mapper.selectByExample(example);
        return lists;
    }

    @Override
    public List<CardChangeList> selectListDocNum(CardChangeList cardChangeList) {
        Example example = new Example(CardChangeList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != cardChangeList.getDocNum()){
            criteria.andEqualTo("docNum", cardChangeList.getDocNum());
        }
        if (null != cardChangeList.getStoreId()){
            criteria.andEqualTo("storeId", cardChangeList.getStoreId());
        }
        List<CardChangeList> lists = mapper.selectByExample(example);
        return lists;
    }

    @Override
    public List<CardChangeList> selectFKJ(CardChangeList cardChangeList) {
        return mapper.selectFKJ(cardChangeList);
    }

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

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