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

import com.kmall.admin.dao.mk.store.MkStoreCampMinusGoodsDao;
import com.kmall.admin.dao.mk.store.MkStoreTicketGoodsTempDao;
import com.kmall.admin.entity.mk.store.MkStoreCampMinusGoodsEntity;
import com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity;
import com.kmall.admin.fromcomm.entity.SysUserEntity;
import com.kmall.admin.service.mk.store.MkStoreCampMinusGoodsService;
import com.kmall.admin.utils.ShiroUtils;
import com.kmall.common.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 门店满减活动商品，参与满减活动的商品Service实现类
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2019-05-29 14:43:29
 */
@Service("mkStoreCampMinusGoodsService")
public class MkStoreCampMinusGoodsServiceImpl implements MkStoreCampMinusGoodsService {
    @Autowired
    private MkStoreCampMinusGoodsDao mkStoreCampMinusGoodsDao;
    @Autowired
    private MkStoreTicketGoodsTempDao mkStoreTicketGoodsTempDao;

    @Override
    public MkStoreCampMinusGoodsEntity queryObject(Integer minusGoodId) {
        return mkStoreCampMinusGoodsDao.queryObject(minusGoodId);
    }

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

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

    @Override
    public List<MkStoreCampMinusGoodsEntity> queryCampMinusGoodsByIdList(Map<String, Object> map) {
        return mkStoreCampMinusGoodsDao.queryCampMinusGoodsByIdList(map);
    }

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

    @Override
    public int save(MkStoreCampMinusGoodsEntity mkStoreCampMinusGoods) {
        return mkStoreCampMinusGoodsDao.save(mkStoreCampMinusGoods);
    }

    @Override
    public int update(MkStoreCampMinusGoodsEntity mkStoreCampMinusGoods) {
        return mkStoreCampMinusGoodsDao.update(mkStoreCampMinusGoods);
    }

    @Override
    public int delete(Integer minusGoodId) {
        return mkStoreCampMinusGoodsDao.delete(minusGoodId);
    }

    @Transactional
    @Override
    public int deleteBatch(Integer[]minusGoodIds) {
        for(int i=0; i<minusGoodIds.length;i++){
            MkStoreCampMinusGoodsEntity entity = mkStoreCampMinusGoodsDao.queryCampMinusStoreTopicById(minusGoodIds[i]);
            if(entity == null){
                throw new RRException("当前选择的满减满折信息不存在");
            }
            mkStoreTicketGoodsTempDao.deleteByStoreTopicIdAndRealId(entity.getStoreRelaId(), entity.getStoreTopicId());
        }
        return mkStoreCampMinusGoodsDao.deleteBatch(minusGoodIds);
    }

    @Transactional
    @Override
    public int batchSaveCampMinusGoods(MkStoreCampMinusGoodsEntity mkStoreCampMinusGoods) {
        SysUserEntity user = ShiroUtils.getUserEntity();

        mkStoreCampMinusGoodsDao.deleteMinusGoodsByMinusId(mkStoreCampMinusGoods.getCampMinusId());

        List<MkStoreTicketGoodsTempEntity> tempEntityList = mkStoreTicketGoodsTempDao.queryGoodsTempByTopicId(mkStoreCampMinusGoods.getStoreTopicId());
        for(MkStoreTicketGoodsTempEntity tempEntity : tempEntityList){
            MkStoreCampMinusGoodsEntity minusGoodsEntity = new MkStoreCampMinusGoodsEntity();
            minusGoodsEntity.setCampMinusId(mkStoreCampMinusGoods.getCampMinusId());
            minusGoodsEntity.setApplyType(tempEntity.getApplyType());
            minusGoodsEntity.setStoreRelaId(tempEntity.getStoreRelaId());
            minusGoodsEntity.setIsValid("0");
            minusGoodsEntity.setCreaterSn(user.getUsername());
            minusGoodsEntity.setCreateTime(new Date());
            minusGoodsEntity.setModerSn(user.getUsername());
            minusGoodsEntity.setModTime(new Date());
            mkStoreCampMinusGoodsDao.save(minusGoodsEntity);
        }
        return 1;
    }
}
