PointCommonBiz.java 9.66 KB
package com.cjs.cms.biz.user.point;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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 com.cjs.cms.biz.ServiceException;
import com.cjs.cms.biz.user.WeiXinBiz;
import com.cjs.cms.dao.pub.MySqlSysDateDao;
import com.cjs.cms.dao.user.WeixinDao;
import com.cjs.cms.dao.user.account.ClientDao;
import com.cjs.cms.dao.user.account.ClientInfoDao;
import com.cjs.cms.dao.user.point.PointActiveDao;
import com.cjs.cms.dao.user.point.PointRecordDao;
import com.cjs.cms.dao.user.point.PointUserDao;
import com.cjs.cms.model.user.WxUserInfo;
import com.cjs.cms.model.user.point.PointActiveInfo;
import com.cjs.cms.model.user.point.PointRecordInfo;
import com.cjs.cms.model.user.point.PointUserInfo;
import com.cjs.cms.util.lang.DateEnum;
import com.cjs.cms.util.lang.DateUtil;
import com.cjs.cms.util.lang.StringUtil;

@Service
public class PointCommonBiz {

    Logger                  log = LogManager.getLogger();
    @Autowired
    private MySqlSysDateDao mySqlSysDateDao;
    @Autowired
    private PointRecordDao  pointRecordDao;
    @Autowired
    private PointActiveDao  pointActiveDao;
    @Autowired
    private PointUserDao    pointUserDao;
    @Autowired
    private WeixinDao       weixinDao;
    @Autowired
    private WeiXinBiz       weiXinBiz;
    @Autowired
    private ClientInfoDao   clientInfoDao;
    @Autowired
    private ClientDao       clientDao;

    /**
     * 根据有效期天数查询有效期截止时间
     * @param expiredDate 有效期天数
     * @return
     */
    public Date getExpiredDate(int expiredDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(mySqlSysDateDao.getSysDate());
        calendar.add(Calendar.DAY_OF_YEAR, expiredDate);
        return calendar.getTime();
    }

    /**
     * 积分发放
     * @param map
     * @param pointActiveInfo 活动信息
     * @param expiredDate 有效期日期
     * @param initDate 采样数据日期,每日发放奖励的活动需要
     * @throws Exception 
     */
    public void sendPoint(Map<String, Object> map, PointActiveInfo pointActiveInfo,
                          Date expiredDate, String initDate, double giftAmount) throws Exception {
        String userId = map.get("CLIENT_ID").toString();
        //验证当日指定用户是否已发放配额
        if (this.checkRecord(map, pointActiveInfo, initDate)) {
            return;
        }

        if (pointActiveInfo.getGiftAmount() > 0 && pointActiveInfo.getGiftRemain() < giftAmount) {
            throw new ServiceException("剩余数量不足");
        }

        if (pointActiveInfo.getGiftAmount() != 0) {
            pointActiveInfo.setGiftRemain(pointActiveInfo.getGiftRemain() - giftAmount);
            pointActiveDao.update(pointActiveInfo);
        }

        PointRecordInfo record = new PointRecordInfo();
        record.setActiveId(pointActiveInfo.getId());
        record.setUserId(userId);
        record.setOccurCount(giftAmount);
        record.setRemainCount(giftAmount);

        PointUserInfo pointUserInfo = pointUserDao.queryByUserId(userId);
        double postAmount;
        if (pointUserInfo != null) {
            postAmount = pointUserInfo.getPointAmount() + giftAmount;
            pointUserInfo.setPointAmount(postAmount);
            pointUserDao.update(pointUserInfo);
        } else {
            postAmount = giftAmount;
            pointUserInfo = new PointUserInfo();
            pointUserInfo.setPointAmount(postAmount);
            pointUserInfo.setUserId(userId);
            pointUserDao.insert(pointUserInfo);
        }
        record.setPostCount(postAmount);
        record.setExpiredDate(expiredDate);
        record.setBusinessType("0");
        record.setRemark("完成" + pointActiveInfo.getName() + "活动");
        record.setGiftBy(map.get("remark").toString());
        record.setCreateBy("管理员");
        pointRecordDao.insert(record);
        log.info("用户:" + userId + "," + initDate + "奖励配额已发放");

        //发送微信提示
        WxUserInfo wxUserInfo = weixinDao.queryByUserId(userId);
        if (wxUserInfo != null) {
            Map<String, String> rationPointInfo = new HashMap<String, String>();
            rationPointInfo.put("openId", wxUserInfo.getOpenId());
            rationPointInfo.put("active", pointActiveInfo.getName());
            rationPointInfo.put("occurCount", record.getOccurCount().toString());
            rationPointInfo.put("postCount", record.getPostCount().toString());
            weiXinBiz.postTemplate(rationPointInfo,
                "/template/rationPointInfo?openId={openId}&active={active}&occurCount={occurCount}&postCount={postCount}");
        }
    }

    /**
     * 检查是否已发放  true:已发放    false:未发放
     * @param map
     * @param pointActiveInfo
     * @param initDate
     * @return
     */
    public boolean checkRecord(Map<String, Object> map, PointActiveInfo pointActiveInfo,
                               String initDate) {
        String userId = map.get("CLIENT_ID").toString();
        //验证当日指定用户是否已发放配额
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        if (!"".equals(initDate)) {
            params.put("startDate", DateUtil.getNow(DateEnum.DATE) + " 9:00:00");
            params.put("endDate", DateUtil.getNow(DateEnum.DATE) + " 23:59:59");
        }
        params.put("activeId", pointActiveInfo.getId());
        params.put("businessType", "0");
        if (!StringUtil.isBlank(map.get("flag"))) {
            params.put("flag", map.get("flag"));
        }
        PointRecordInfo validRecord = pointRecordDao.queryRecordByCondition(params);
        if (validRecord != null) {
            return true;
        } else {
            return false;
        }
    }

    /**活动_验证直推用户。
     * 若直推用户等级高于该用户等级,则返回直推用户会员号、直推用户等级,否则而返回失败
     * @param params {clientId : 当前会员号, userLevel : 当前会员会员等级}
     * @return {developer : 直推用户 ,  developerLevel : 直推用户会员等级}
     */
    public Map<String, Object> checkDeveloper(Map<String, Object> params) {
        Map<String, Object> outmap = new HashMap<String, Object>();
        String developer = clientInfoDao
            .queryDeveloperByClientId(params.get("clientId").toString());
        if ("".equals(developer) || "5001".equals(developer) || StringUtil.isBlank(developer)) {
            outmap.put("resultCode", "0");
            log.info("用户:" + params.get("clientId") + "没有推荐人");
            return outmap;
        }
        String develoeprLevel = clientDao.queryUserLevel(developer);
        //若推荐人等级不高于该会员等级,则返回失败
        if (Integer.parseInt(develoeprLevel) <= Integer.parseInt(params.get("userLevel").toString())
            && "0".equals(params.get("baseUser").toString())) {
            outmap.put("resultCode", "0");
            log.info("等级为:" + params.get("userLevel") + "的会员:" + params.get("clientId") + "的推荐人等级为:"
                     + develoeprLevel);
        } else {
            outmap.put("resultCode", "1");
            outmap.put("developer", developer);
            outmap.put("developerLevel", develoeprLevel);
        }
        return outmap;
    }

    /**活动_计算配额*/
    public void setPoint(Map<String, Object> params,
                         PointActiveInfo pointActiveInfo) throws Exception {
        String giftBy = "check,会员为:" + params.get("CLIENT_ID") + ",买入交易额:" + params.get("TOTAL");
        params.put("remark", giftBy);
        //flag为重复发放验证方式标识,不为空则验证remark字段
        params.put("flag", "check");
        int num = Integer.parseInt(params.get("NUM").toString());
        Date expiredDate = this.getExpiredDate(pointActiveInfo.getExpiredDate());
        this.sendPoint(params, pointActiveInfo, expiredDate, params.get("INIT_DATE").toString(),
            pointActiveInfo.getRules().get(0).getGiftAmount() * num);
        Map<String, Object> inmap = new HashMap<String, Object>();
        Map<String, Object> retmap = new HashMap<String, Object>();
        for (int i = 0; i <= 4; i++) {
            int devPoint;
            if (retmap.size() == 0) {
                inmap.put("clientId", params.get("CLIENT_ID"));
                inmap.put("userLevel", 0);
                inmap.put("baseUser", 1);
            } else {
                inmap.put("clientId", retmap.get("developer"));
                inmap.put("userLevel", retmap.get("developerLevel"));
                inmap.put("baseUser", 0);
            }
            retmap = this.checkDeveloper(inmap);
            if ("0".equals(retmap.get("resultCode"))) {
                break;
            }
            if (inmap.get("clientId").equals(params.get("CLIENT_ID"))) {
                devPoint = 5;
            } else {
                devPoint = 1;
            }
            giftBy = "交易用户为:" + params.get("CLIENT_ID") + ",直推用户为:" + inmap.get("clientId")
                     + ",推荐人为:" + retmap.get("developer");
            retmap.put("flag",
                "交易用户为:" + params.get("CLIENT_ID") + ",直推用户为:" + inmap.get("clientId"));
            retmap.put("remark", giftBy);
            retmap.put("CLIENT_ID", retmap.get("developer"));
            this.sendPoint(retmap, pointActiveInfo, expiredDate, params.get("INIT_DATE").toString(),
                devPoint * num);
        }
    }

}