ActiveConfigBiz.java 9.75 KB
package com.cjs.cms.biz.user.point;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cjs.cms.dao.user.point.ActiveConfigDao;
import com.cjs.cms.dao.user.point.ActiveGiftDao;
import com.cjs.cms.dao.user.point.ActiveRuleDao;
import com.cjs.cms.model.user.UserInfo;
import com.cjs.cms.model.user.point.ActiveConfigInfo;
import com.cjs.cms.model.user.point.ActiveGiftInfo;
import com.cjs.cms.model.user.point.ActiveRuleInfo;
import com.cjs.cms.util.lang.StringUtil;
import com.cjs.cms.util.web.ActionUtil;

/**
 * 活动设置
 * @author tongxiaochuan
 *
 */
@Service
public class ActiveConfigBiz {

    Logger                  log = LogManager.getLogger();
    @Autowired
    private ActiveConfigDao activeConfigDao;
    @Autowired
    private ActiveGiftDao   activeGiftDao;
    @Autowired
    private ActiveRuleDao   activeRuleDao;

    /**活动新增或修改*/
    @Transactional
    public Map<String, Object> config(ActiveConfigInfo activeConfigInfo) {
        Map<String, Object> outmap = new HashMap<String, Object>();
        if (activeConfigInfo.getStartDate().after(activeConfigInfo.getEndDate())) {
            outmap.put("resultCode", "0");
            outmap.put("resultMsg", "活动开始时间必须早于结束时间");
            return outmap;
        }
        if ("0".equals(activeConfigInfo.getStatus())) {
            outmap.put("resultCode", "0");
            outmap.put("resultMsg", "已作废的活动不可修改");
            return outmap;
        }

        UserInfo userInfo = ActionUtil.getUser();
        activeConfigInfo.setCreateBy(userInfo.getTruename());
        if (activeConfigInfo.getEndDate().after(new Date())) {
            activeConfigInfo.setStatus("2");
        } else if (activeConfigInfo.getStartDate().after(new Date())) {
            activeConfigInfo.setStatus("1");
        } else if (activeConfigInfo.getEndDate().before(new Date())) {
            activeConfigInfo.setStatus("3");
        }

        if (StringUtil.isBlank(activeConfigInfo.getId())) {
            //新增
            activeConfigDao.insert(activeConfigInfo);
            if (StringUtil.isBlank(activeConfigInfo.getId())) {
                outmap.put("resultCode", "0");
                outmap.put("resultMsg", "活动信息添加失败");
                return outmap;
            }
            for (ActiveGiftInfo gift : activeConfigInfo.getGifts()) {
                gift.setActiveId(activeConfigInfo.getId());
                gift.setCreateBy(userInfo.getTruename());
                gift.setGiftRemainAmount(gift.getGiftAmount());
                activeGiftDao.insert(gift);
                if (StringUtil.isBlank(gift.getId())) {
                    outmap.put("resultCode", "0");
                    outmap.put("resultMsg", "活动奖品信息添加失败");
                    return outmap;
                }
            }
        } else {
            //修改
            activeConfigDao.update(activeConfigInfo);
            List<ActiveGiftInfo> gifts = activeGiftDao.queryByActiveId(activeConfigInfo.getId());
            Set<ActiveGiftInfo> add = new HashSet<ActiveGiftInfo>();
            Set<ActiveGiftInfo> update = new HashSet<ActiveGiftInfo>();
            Set<Integer> updateIndex = new HashSet<Integer>();
            Set<Integer> del = new HashSet<Integer>();

            if (activeConfigInfo.getGifts().size() == 0) {
                for (ActiveGiftInfo activeGiftInfo : gifts) {
                    del.add(activeGiftInfo.getId());
                }
            }
            if (gifts.size() == 0) {
                for (ActiveGiftInfo activeGiftInfo : activeConfigInfo.getGifts()) {
                    activeGiftInfo.setActiveId(activeConfigInfo.getId());
                    activeGiftInfo.setCreateBy(userInfo.getTruename());
                    activeGiftInfo.setGiftRemainAmount(activeGiftInfo.getGiftAmount());
                    add.add(activeGiftInfo);
                }
            }
            if (activeConfigInfo.getGifts().size() != 0 && gifts.size() != 0) {
                //修改的奖品
                for (ActiveGiftInfo oldGift : gifts) {
                    for (ActiveGiftInfo newGift : activeConfigInfo.getGifts()) {
                        if (oldGift.getGiftCode().equals(newGift.getGiftCode())) {
                            int mixAmount = oldGift.getGiftAmount() - newGift.getGiftAmount();
                            if (oldGift.getGiftRemainAmount() - mixAmount < 0) {
                                outmap.put("resultCode", "0");
                                outmap.put("resultMsg",
                                    "奖品:" + newGift.getGiftCode() + "的剩余数量为:"
                                                        + oldGift.getGiftRemainAmount()
                                                        + ",奖励总量的减少数量不可超过此数量。");
                                return outmap;
                            } else {
                                newGift
                                    .setGiftRemainAmount(oldGift.getGiftRemainAmount() - mixAmount);
                            }
                            newGift.setCreateBy(userInfo.getTruename());
                            newGift.setActiveId(activeConfigInfo.getId());
                            newGift.setId(oldGift.getId());
                            update.add(newGift);
                            updateIndex.add(oldGift.getId());
                        }
                    }
                }

                //新增的奖品
                for (ActiveGiftInfo activeGiftInfo : update) {
                    activeConfigInfo.getGifts().remove(activeGiftInfo);
                }
                for (ActiveGiftInfo activeGiftInfo : activeConfigInfo.getGifts()) {
                    activeGiftInfo.setGiftRemainAmount(activeGiftInfo.getGiftAmount());
                    activeGiftInfo.setCreateBy(userInfo.getTruename());
                    activeGiftInfo.setActiveId(activeConfigInfo.getId());
                    add.add(activeGiftInfo);
                }

                //删除的奖品
                List<String> giftsId = new ArrayList<String>();
                for (ActiveGiftInfo activeGiftInfo : gifts) {
                    giftsId.add(activeGiftInfo.getId().toString());
                }
                for (Integer index : updateIndex) {
                    giftsId.remove(index.toString());
                }
                for (String str : giftsId) {
                    del.add(Integer.parseInt(str));
                }
            }

            for (Integer index : del) {
                activeGiftDao.delete(index);
            }
            for (ActiveGiftInfo activeGiftInfo : update) {
                activeGiftDao.update(activeGiftInfo);
            }
            for (ActiveGiftInfo activeGiftInfo : add) {
                activeGiftDao.insert(activeGiftInfo);
            }
        }

        outmap.put("resultCode", "1");
        outmap.put("resultMsg", "操作完毕");
        return outmap;
    }

    public ActiveConfigInfo searchConfig(int activeId) {
        ActiveConfigInfo config = activeConfigDao.queryByIdForUpdate(activeId);
        log.info(config);
        return activeConfigDao.queryByIdForUpdate(activeId);
    }

    /**根据活动id查询规则*/
    public List<ActiveRuleInfo> searchRule(int activeId) {
        return activeRuleDao.queryByConfigId(activeId);
    }

    /**根据活动id查询奖品编码*/
    public List<Map<String, Object>> searchGiftCode(int activeId) {
        List<String> list = activeGiftDao.queryGiftCodeByActiveId(activeId);
        List<Map<String, Object>> returnlist = new ArrayList<Map<String, Object>>();
        for (String str : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", str);
            map.put("value", str);
            returnlist.add(map);
        }
        return returnlist;
    }

    /**设定活动规则*/
    @Transactional
    public Map<String, Object> addRule(ActiveConfigInfo config) {
        Map<String, Object> outmap = new HashMap<String, Object>();
        log.info(config);
        if ("0".equals(config.getStatus())) {
            outmap.put("resultCode", "0");
            outmap.put("resultMsg", "已作废的活动不可修改");
            return outmap;
        }
        UserInfo userInfo = ActionUtil.getUser();
        int activeId = config.getId();
        activeRuleDao.deleteByActiveId(config.getId());
        Map<String, Object> queryGift = new HashMap<String, Object>();
        ActiveGiftInfo gift;
        for (ActiveRuleInfo rule : config.getRules()) {
            queryGift.put("activeId", activeId);
            queryGift.put("giftCode", rule.getActiveGiftInfo().getGiftCode());
            int giftId = activeGiftDao.queryGiftId(queryGift);
            rule.setActiveId(activeId);
            rule.setGiftId(giftId);
            rule.setCreateBy(userInfo.getTruename());
            activeRuleDao.insert(rule);
            gift = activeGiftDao.queryById(giftId);
            gift.setGiftType(rule.getActiveGiftInfo().getGiftType());
            activeGiftDao.update(gift);
        }

        outmap.put("resultCode", "1");
        outmap.put("resultMsg", "操作完毕");
        return outmap;
    }

    /**根据藏品编号查询藏品名*/
    public String queryGiftName(String giftCode) {
        return activeGiftDao.queryGiftName(giftCode);
    }

    /**取消活动*/
    public void cancelActive(int activeId) {
        activeConfigDao.cancel(activeId);
    }
}