package com.kmall.admin.service.impl.mk;

import com.kmall.admin.dao.mk.MkCenterMenuStoreRealDao;
import com.kmall.admin.entity.mk.MkCenterMenuEntity;
import com.kmall.admin.entity.mk.MkCenterMenuStoreRealEntity;
import com.kmall.admin.service.mk.MkCenterMenuStoreRealService;
import com.kmall.common.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 营销中心菜单门店关联表Service实现类
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2019-05-28 10:14:06
 */
@Service("mkCenterMenuStoreRealService")
public class MkCenterMenuStoreRealServiceImpl implements MkCenterMenuStoreRealService {
    @Autowired
    private MkCenterMenuStoreRealDao mkCenterMenuStoreRealDao;

    @Override
    public MkCenterMenuStoreRealEntity queryObject(Integer id) {
        MkCenterMenuStoreRealEntity entity = mkCenterMenuStoreRealDao.queryObject(id);
        List<Integer> centerIdList = new ArrayList<>();
        List<MkCenterMenuStoreRealEntity> storeRealEntityList = mkCenterMenuStoreRealDao.queryMenuByThirdMerchSn(entity.getThirdMerchSn());
        for(MkCenterMenuStoreRealEntity realEntity : storeRealEntityList){
            centerIdList.add(realEntity.getCenterMenuId());
        }
        entity.setCenterMenuIdList(centerIdList);
        return entity;
    }

    @Override
    public List<MkCenterMenuStoreRealEntity> queryList(Map<String, Object> map) {
        return mkCenterMenuStoreRealDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return mkCenterMenuStoreRealDao.queryTotal(map);
    }

    @Override
    public int saveOrUpdate(MkCenterMenuStoreRealEntity mkCenterMenuStoreReal) {
        List<Integer> idList = mkCenterMenuStoreReal.getCenterMenuIdList();
        if (idList == null || idList.size() <= 0) {
            throw new RRException("营销菜单不能为空！");
        }
        mkCenterMenuStoreRealDao.deleteByStoreId(mkCenterMenuStoreReal.getStoreId());

        List<Integer> centerIdList = new ArrayList<>();
        for (Integer centerId: idList){
            if(centerId != 0){
                centerIdList.add(centerId);
            }
        }
        mkCenterMenuStoreReal.setCenterMenuIdList(centerIdList);
        return mkCenterMenuStoreRealDao.save(mkCenterMenuStoreReal);
    }

    @Override
    public int update(MkCenterMenuStoreRealEntity mkCenterMenuStoreReal) {
        return mkCenterMenuStoreRealDao.update(mkCenterMenuStoreReal);
    }

    @Override
    public int delete(Integer id) {
        return mkCenterMenuStoreRealDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[]ids) {
        return mkCenterMenuStoreRealDao.deleteBatch(ids);
    }
}
