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.SsmDocPromotionDao;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.BaseStores;
import com.ssm.manage.entity.DocPromotion;
import com.ssm.manage.extendEntity.StoreDaily;
import com.ssm.manage.mapper.DocPromotionMapper;
import com.ssm.manage.query.DocPromotionRequest;
import com.ssm.manage.query.StoreDailyRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.DocPromotionResponse;
import com.ssm.manage.response.PromotionDayVo;
import com.ssm.manage.service.SsmBaseStoreService;
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 SsmDocPromotionDaoImpl extends BaseDaoImpl<DocPromotion> implements SsmDocPromotionDao {
    @Autowired
    private DocPromotionMapper mapper;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

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

    @Override
    public PageInfo<DocPromotion> selectPage(DocPromotionRequest docPromotionRequest) {
        Example example = new Example(DocPromotion.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != docPromotionRequest.getStoreId()){
            criteria.andEqualTo("storeId",docPromotionRequest.getStoreId());
        }
        if (null != docPromotionRequest.getId()){
            criteria.andEqualTo("id",docPromotionRequest.getId());
        }
        if (StringUtils.isNotBlank(docPromotionRequest.getDocNum())){
            criteria.andLike("docNum","%" + docPromotionRequest.getDocNum() + "%");
        }
        if (StringUtils.isNotBlank(docPromotionRequest.getMemberName())){
            criteria.andLike("memberName","%" + docPromotionRequest.getMemberName() + "%");
        }
        if (StringUtils.isNotBlank(docPromotionRequest.getMobilePhone())){
            criteria.andLike("mobilePhone","%" + docPromotionRequest.getMobilePhone() + "%");
        }
        if (null !=  docPromotionRequest.getPromotionId() && !docPromotionRequest.getPromotionId().equals("")){
            criteria.andEqualTo("promotionId",docPromotionRequest.getPromotionId());
        }
        if ( null != docPromotionRequest.getStartTime() && null != docPromotionRequest.getEndTime()){
            criteria.andBetween("docDate",docPromotionRequest.getStartTime(), docPromotionRequest.getEndTime());
        }

        PageHelper.startPage(docPromotionRequest.getPageNum(),docPromotionRequest.getPageSize(),docPromotionRequest.getOrderAndSort());
        List<DocPromotion> list = selectByExample(example);
        PageInfo<DocPromotion> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<DocPromotion> selectPageNew(DocPromotionRequest docPromotionRequest) {
        List<Long> lists = new ArrayList<>();
        if(docPromotionRequest.getSelectType()==0){
            lists=docPromotionRequest.getStoreList();
        }else{
            BaseStore baseStore1 = ssmBaseStoreService.selectByPrimaryKey(docPromotionRequest.getStoreId());
            if(null == baseStore1.getParentId()){
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(docPromotionRequest.getStoreId());
                List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
                List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
                lists=collect;
            }else{
                lists.add(docPromotionRequest.getStoreId());
            }
        }
        Example example = new Example(DocPromotion.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("storeId",lists);
        if (null != docPromotionRequest.getId()){
            criteria.andEqualTo("id",docPromotionRequest.getId());
        }
        if (StringUtils.isNotBlank(docPromotionRequest.getDocNum())){
            criteria.andLike("docNum","%" + docPromotionRequest.getDocNum() + "%");
        }
        if (StringUtils.isNotBlank(docPromotionRequest.getMemberName())){
            criteria.andLike("memberName","%" + docPromotionRequest.getMemberName() + "%");
        }
        if (StringUtils.isNotBlank(docPromotionRequest.getMobilePhone())){
            criteria.andLike("mobilePhone","%" + docPromotionRequest.getMobilePhone() + "%");
        }
        if (null !=  docPromotionRequest.getPromotionId()){
            criteria.andEqualTo("promotionId",docPromotionRequest.getPromotionId());
        }
        if ( null != docPromotionRequest.getStartTime() && null != docPromotionRequest.getEndTime()){
            criteria.andBetween("docDate",docPromotionRequest.getStartTime(), docPromotionRequest.getEndTime());
        }
        if ( null != docPromotionRequest.getIsCancel()){
            criteria.andEqualTo("isCancel",docPromotionRequest.getIsCancel());
        }
        if( null != docPromotionRequest.getCollectionRechargeAmount()){
            criteria.andNotEqualTo("collectionRechargeAmount", BigDecimal.ZERO);
        }
        if(docPromotionRequest.getPromotionId() !=null){
            criteria.andEqualTo("promotionId",docPromotionRequest.getPromotionId());
        }
        PageHelper.startPage(docPromotionRequest.getPageNum(),docPromotionRequest.getPageSize(),docPromotionRequest.getOrderAndSort());
        List<DocPromotion> list = selectByExample(example);
        PageInfo<DocPromotion> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int updateStaff(DocPromotion docPromotion) {
        return mapper.updateStaff(docPromotion);
    }

    @Override
    public List<DocPromotion> selectFKJ(DocPromotionRequest docPromotion) {
        return mapper.selectFKJ(docPromotion);
    }

    @Override
    public DocPromotion selectOne(DocPromotion docPromotion) {
        Example example = new Example(DocPromotion.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != docPromotion.getId()){
            criteria.andEqualTo("id",docPromotion.getId());
        }
        if (null != docPromotion.getIsCancel()){
            criteria.andEqualTo("is_cancel",0);
        }
        return mapper.selectOneByExample(example);
    }

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

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