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.SsmPromotionDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.mapper.PromotionListMapper;
import com.ssm.manage.query.PromotionListRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmPromotionMemberService;
import com.ssm.manage.service.SsmPromotionStoreService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
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.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SsmPromotionDaoImpl extends BaseDaoImpl<PromotionList> implements SsmPromotionDao {

    @Autowired
    private PromotionListMapper mapper;

    @Autowired
    private SsmPromotionStoreService ssmPromotionStoreService;
    @Autowired
    private SsmPromotionMemberService ssmPromotionMemberService;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

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

    @Override
    public PageInfo<PromotionListRequest> selectPage(PromotionListRequest promotionListRequest) {
        Example example = new Example(PromotionList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != promotionListRequest.getStoreId()){
            criteria.andEqualTo("storeId",promotionListRequest.getStoreId());
        }
        if (null != promotionListRequest.getPushState()){
            criteria.andEqualTo("pushState",promotionListRequest.getPushState());
        }
        if (null != promotionListRequest.getType()){
                criteria.andEqualTo("type",promotionListRequest.getType());
        }
        if (StringUtils.isNotBlank(promotionListRequest.getPromotionNum())){
            criteria.andLike("promotionNum","%" + promotionListRequest.getPromotionNum() + "%");
        }
        if (StringUtils.isNotBlank(promotionListRequest.getPromotionName())){
            criteria.andLike("promotionName","%" + promotionListRequest.getPromotionName() + "%");
        }
        if ( null != promotionListRequest.getStartTime() && null != promotionListRequest.getEndTime()){
            criteria.andBetween("createdDate",promotionListRequest.getStartTime(), promotionListRequest.getEndTime());
        }

        PageHelper.startPage(promotionListRequest.getPageNum(),promotionListRequest.getPageSize(),promotionListRequest.getOrderAndSort());
        List<PromotionList> lists = selectByExample(example);
        List<PromotionListRequest> promotionListRequests = new ArrayList<>();
        for (PromotionList list : lists) {
            PromotionListRequest promotionListRequest1 = new PromotionListRequest();
            BeanUtils.copyProperties(list,promotionListRequest1);
            Map<String,Object> map = new HashMap<>();

            PromotionStore promotionStore = new PromotionStore();
            promotionStore.setPromotionId(list.getId());
            List<PromotionStore> select = ssmPromotionStoreService.select(promotionStore);
            for (PromotionStore store : select) {
                BaseStore baseStore = new BaseStore();
                baseStore.setId(store.getStoreId());
                BaseStore baseStore1 = ssmBaseStoreService.selectByPrimaryKey(baseStore.getId());
                store.setStoreNum(baseStore1.getStoreNum());
            }

            PromotionMember promotionMember = new PromotionMember();
            promotionMember.setPromotionId(list.getId());
            List<PromotionMember> select1 = ssmPromotionMemberService.select(promotionMember);
            promotionListRequest1.setPromotionMemberLists(select1);
            promotionListRequest1.setPromotionStores(select);
            promotionListRequests.add(promotionListRequest1);
        }
        PageHelper.startPage(promotionListRequest.getPageNum(),promotionListRequest.getPageSize(),promotionListRequest.getOrderAndSort());
        List<PromotionList> listsCount = selectByExample(example);
        PageInfo<PromotionListRequest> pageInfo = new PageInfo<>(promotionListRequests);
        pageInfo.setTotal(listsCount.size());
        return pageInfo;
    }

    @Override
    public List<Map<String,Object>> selectList(PromotionListRequest promotionListRequest) {
        List<Map<String,Object>> mapList=new ArrayList<>();
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(promotionListRequest.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        for (BaseStores pagesorganization : pagesorganizations) {
            Example example = new Example(PromotionList.class);
            Example.Criteria criteria = example.createCriteria();
            if (null != promotionListRequest.getStoreId()){
                criteria.andEqualTo("storeId",pagesorganization.getId());
            }
            if (null != promotionListRequest.getPushState()){
                criteria.andEqualTo("pushState",promotionListRequest.getPushState());
                criteria.andEqualTo("state",4);
            }
            if (null != promotionListRequest.getType()){
                criteria.andEqualTo("type",promotionListRequest.getType());
            }
            if (StringUtils.isNotBlank(promotionListRequest.getPromotionNum())){
                criteria.andLike("promotionNum","%" + promotionListRequest.getPromotionNum() + "%");
            }
            if (StringUtils.isNotBlank(promotionListRequest.getPromotionName())){
                criteria.andLike("promotionName","%" + promotionListRequest.getPromotionName() + "%");
            }
            if ( null != promotionListRequest.getStartTime() && null != promotionListRequest.getEndTime()){
                criteria.andBetween("createdDate",promotionListRequest.getStartTime(), promotionListRequest.getEndTime());
            }

            List<PromotionList> lists = selectByExample(example);
            for (PromotionList list : lists) {
                Map<String,Object> map = new HashMap<>();

                PromotionStore promotionStore = new PromotionStore();
                promotionStore.setPromotionId(list.getId());
                List<PromotionStore> select = ssmPromotionStoreService.select(promotionStore);
                map.put("promotionList",list);
                map.put("promotionStore",select);
                mapList.add(map);
            }
        }


        return mapList;
    }

    @Override
    public List<PromotionList> selectPages(PromotionListRequest promotionListRequest) {
        Example example = new Example(PromotionList.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != promotionListRequest.getStoreId()){
            criteria.andEqualTo("storeId",promotionListRequest.getStoreId());
        }
        if (null != promotionListRequest.getPushState()){
            criteria.andEqualTo("pushState",promotionListRequest.getPushState());
        }
        if (null != promotionListRequest.getType()){
            criteria.andEqualTo("type",promotionListRequest.getType());
        }
        if (StringUtils.isNotBlank(promotionListRequest.getPromotionNum())){
            criteria.andLike("promotionNum","%" + promotionListRequest.getPromotionNum() + "%");
        }
        if (StringUtils.isNotBlank(promotionListRequest.getPromotionName())){
            criteria.andLike("promotionName","%" + promotionListRequest.getPromotionName() + "%");
        }
        if ( null != promotionListRequest.getStartTime() && null != promotionListRequest.getEndTime()){
            criteria.andBetween("createdDate",promotionListRequest.getStartTime(), promotionListRequest.getEndTime());
        }

        PageHelper.startPage(promotionListRequest.getPageNum(),promotionListRequest.getPageSize(),promotionListRequest.getOrderAndSort());
        List<PromotionList> lists = selectByExample(example);
        PageInfo<PromotionList> pageInfo = new PageInfo<>(lists);
        return lists;
    }

    @Override
    public PageInfo<PromotionList> selectPagenew(PromotionListRequest promotionListRequest) {
        Example example = new Example(PromotionList.class);
        Example.Criteria criteria = example.createCriteria();
//        if (null != promotionListRequest.getStoreId()){
//            criteria.andEqualTo("storeId",promotionListRequest.getStoreId());
//        }
        if (null != promotionListRequest.getPushState()){
            criteria.andEqualTo("pushState",promotionListRequest.getPushState());
        }
        if (null != promotionListRequest.getType()){
            criteria.andEqualTo("type",promotionListRequest.getType());
        }
        if (StringUtils.isNotBlank(promotionListRequest.getPromotionNum())){
            criteria.andLike("promotionNum","%" + promotionListRequest.getPromotionNum() + "%");
        }
        if (StringUtils.isNotBlank(promotionListRequest.getPromotionName())){
            criteria.andLike("promotionName","%" + promotionListRequest.getPromotionName() + "%");
        }
        if ( null != promotionListRequest.getStartTime() && null != promotionListRequest.getEndTime()){
            criteria.andBetween("createdDate",promotionListRequest.getStartTime(), promotionListRequest.getEndTime());
        }

        if (null != promotionListRequest.getPromotionunitId()){
            criteria.andIn("id", promotionListRequest.getPromotionunitId());
        }



        PageHelper.startPage(promotionListRequest.getPageNum(),promotionListRequest.getPageSize(),promotionListRequest.getOrderAndSort());
        List<PromotionList> lists = selectByExample(example);
        PageInfo<PromotionList> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public List<PromotionList> selectPromotionType(PromotionListRequest promotionListRequest) {
        return mapper.selectPromotionType(promotionListRequest);
    }
}
