package com.ssm.task;

import com.ssm.core.utils.StoreParaUtils;
import com.ssm.manage.entity.StoreMemberList;
import com.ssm.manage.entity.StoreParament;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.service.SsmStoreMemberService;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class SsmDayStopMemberTask extends QuartzJobBean {

    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Example example = new Example(StoreMemberList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("state",1);
        List<StoreMemberList> lists = ssmStoreMemberService.selectByExample(example);
        //按门店ID进行分组
        Map<Long,List<StoreMemberList>> map = lists.stream().collect(Collectors.groupingBy(StoreMemberList::getStoreId));
        Iterator<Map.Entry<Long,List<StoreMemberList>>> entryIterator = map.entrySet().iterator();
        while (entryIterator.hasNext()){
            Map.Entry<Long,List<StoreMemberList>> entry = entryIterator.next();
            Long storeId = entry.getKey();
            List<StoreMemberList> list = entry.getValue();
            //查询会员超过N天无消费停用参数
            StoreParament storeParament = StoreParaUtils.getStorePara(storeId,Constant.exceed);
            if (!"0".equals(storeParament.getParamentValue())){
                String value = storeParament.getParamentValue();
                //根据门店参数计算消费时间  会员最近消费时间<当前时间的 停用
                Date date = DateUtils.diffDay(new Date(), -Integer.parseInt(value));
                Example e = new Example(StoreMemberList.class);
                Example.Criteria c = e.createCriteria();
                c.andEqualTo("storeId", storeId);
                c.andLessThan("consumeDate", date);
                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setState(0);
                ssmStoreMemberService.updateByExampleSelective(storeMemberList, e);
            }


        }
//        for (StoreMemberList storeMemberList : lists) {
//            //查询会员超过N天无消费停用参数
//            StoreParament storeParament = StoreParaUtils.getStorePara(storeMemberList.getStoreId(),Constant.exceed);
//
//            if (null != storeParament) {
//                String value = storeParament.getParamentValue();
//
//                if (DateUtils.compare(DateUtils.ymdFormat(new Date()), DateUtils.ymdFormat(DateUtils.diffDay(storeMemberList.getConsumeDate(), Integer.parseInt(value)))) > 0) {
//                    storeMemberList.setState(0);
//                    ssmStoreMemberService.updateNotNull(storeMemberList);
//                }
//            }
//        }
    }
}
