package com.kmall.admin.service.impl;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.dao.AdDao;
import com.kmall.admin.dao.StoreDao;
import com.kmall.admin.dto.CopyAdDto;
import com.kmall.admin.entity.AdEntity;
import com.kmall.admin.entity.StoreEntity;
import com.kmall.admin.service.AdService;
import com.kmall.common.utils.MapBeanUtil;
import com.kmall.common.utils.R;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Service实现类
 *
 * @author Scott
 * @email
 * @date 2017-08-19 09:37:35
 */
@Service("adService")
public class AdServiceImpl implements AdService {
    @Autowired
    private AdDao adDao;
    @Autowired
    private StoreDao storeDao;

    @Override
    public AdEntity queryObject(Integer id) {
        return adDao.queryObject(id);
    }

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

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

    @Override
    public int save(AdEntity ad) {

        Map<String, Object> valideDate = MapBeanUtil.fromObject(ad);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        builder.put("adPositionId", "广告位置");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }

        return adDao.save(ad);
    }

    @Override
    public int update(AdEntity ad) {

        Map<String, Object> valideDate = MapBeanUtil.fromObject(ad);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        builder.put("adPositionId", "广告位置");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }

        return adDao.update(ad);
    }

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

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

    @Override
    public int saveCopyAd(CopyAdDto copyAdDto){
        AdEntity adEntity = adDao.queryObject(copyAdDto.getAdId());
        if(adEntity != null){
            List<Integer> storeIdList = new ArrayList<>();
            String[] storeIds = copyAdDto.getStoreList().split(";");
            for(int i = 0; i < storeIds.length; i++){
                storeIdList.add(Integer.valueOf(storeIds[i]));
            }
            for(int i = 0; i < storeIdList.size(); i++){
                AdEntity ad = new AdEntity();
                StoreEntity storeEntity = storeDao.queryObject(storeIdList.get(i));
                if(storeEntity == null){
                    throw new RRException("门店不存在");
                }
                ad.setName(adEntity.getName());
                ad.setAdPositionId(adEntity.getAdPositionId());
                ad.setMediaType(adEntity.getMediaType());
                ad.setSortOrder(adEntity.getSortOrder());
                ad.setLink(adEntity.getLink());
                ad.setImageUrl(adEntity.getImageUrl());
                ad.setContent(adEntity.getContent());
                ad.setEndTime(adEntity.getEndTime());
                ad.setEnabled(adEntity.getEnabled());
                ad.setMerchSn(storeEntity.getMerchSn());
                ad.setStoreId(storeIdList.get(i));
                //复制广告信息不保存其他门店的优惠券、活动
                adDao.save(ad);
            }
        }
        return 1;
    }
}
