package com.kiisoo.ic.promotion.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kiisoo.ic.promotion.entity.PmnPromotionSrcVO;
import com.kiisoo.ic.promotion.mapper.PmnPromotionSrcMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class PromotionSrcService {

    @Autowired
    PmnPromotionSrcMapper pmnPromotionSrcMapper;
    @Autowired
    PoiStoreStaffDOMapper poiStoreStaffDOMapper;


    public Map<String, Object> listStaff(Long instanceId, Long storeId, int pageNum, int pageSize) {

        Map<String, Object> results = new HashMap<>();

        PageHelper.startPage(pageNum, pageSize);
        List<PmnPromotionSrcVO> pmnPromotionSrcList = pmnPromotionSrcMapper.queryList(instanceId, storeId);
        if (CollectionUtils.isEmpty(pmnPromotionSrcList)) {
            results.put("this", null);
            return results;
        }
        AtomicInteger number = new AtomicInteger(1 + ((pageNum - 1) * pageSize));
        //求分页总数
        Page<PmnPromotionSrcVO> pageList = (Page<PmnPromotionSrcVO>) pmnPromotionSrcList;
        Long totalSize = pageList.getTotal();
        for (PmnPromotionSrcVO m : pmnPromotionSrcList) {

            m.setTotalSize(totalSize);
            m.setNumber(number.get());
            number.incrementAndGet();
        }
        results.put("this", new PageInfo<>(pmnPromotionSrcList));
        return results;

    }
}
