门店推广数据接口

master
jiangyunjie 6 years ago
parent 0c7fe8852a
commit c4aab3efa2

@ -215,6 +215,12 @@
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>1.2.1</version>
</dependency>
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>1.9.0</version>
</dependency>
</dependencies>
<build>

@ -11,6 +11,28 @@ public class Constants {
*/
public static final String DATABASE_STORE_ID_KEY = "store_id";
/**
* 0
*/
public static int CONTACT_COUNT = 0;
/**
*
*/
public static int INSERT_FAIL = 0;
/**
* "region".
*/
public static final String STORE_DATA_TYPE_REGION = "region";
/**
* "store".
*/
public static final String STORE_DATA_TYPE_STORE = "store";
/**
* "seller".
*/
public static final String STORE_DATA_TYPE_SELLER = "seller";
}

@ -0,0 +1,70 @@
package com.kiisoo.ic.store.controller;
import com.github.pagehelper.PageInfo;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.store.entity.StorePromotionDataDO;
import com.kiisoo.ic.store.service.StoreEmployeeService;
import com.kiisoo.ic.store.service.StorePromotionDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author yunjie.jiang
* @date 2020/4/10 10:49
* @company kiisoo
* @details 广controller
*/
@Controller
@RequestMapping("/store/promotion/data")
@Slf4j
public class StorePromotionDataController extends BaseController {
/**
* 广
*/
@Autowired
private StorePromotionDataService storePromotionDataService;
/**
* 广
* @param userId
* @param storeId
* @param regionId
* @param sellerId
* @param startDate
* @param endDate
* @param type "region"., "store"., "seller".
* @param pageNum
* @param pageSize
* @return
*/
@RequestMapping(value = "list",method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> listStorePromotionDate(@RequestParam("userId") Long userId,
@RequestParam("storeId") Long storeId,
@RequestParam("regionId") Long regionId,
@RequestParam("sellerId") Long sellerId,
@RequestParam("startDate") Date startDate,
@RequestParam("endDate") Date endDate,
@RequestParam("type") String type,
@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "15") int pageSize){
try {
PageInfo<StorePromotionDataDO> storePromotionDataDOS = storePromotionDataService.listStorePromotionDate(userId, storeId,
regionId, sellerId, startDate, endDate, type, pageNum, pageSize);
return data(storePromotionDataDOS);
}catch (Exception e){
log.error("查询门店推广数据列表",e);
return fail();
}
}
}

@ -0,0 +1,80 @@
package com.kiisoo.ic.store.entity;
import lombok.Data;
import java.math.BigDecimal;
/**
* @Description:
* @Author: JYJ
* @Date: 2020/4/10 14:39
* @Version: v1.0
*/
@Data
public class StorePromotionDataDO {
/**
*
*/
public Long id;
/**
* id
*/
public String userId;
/**
* id
*/
public Long storeId;
/**
*
*/
public Long storeName;
/**
* id
*/
public Long regionId;
/**
*
*/
public String regionName;
/**
* id
*/
public Long sellerId;
/**
*
*/
public String sellerName;
/**
*
*/
public Integer cusNum;
/**
*
*/
public Integer newCusNum;
/**
*
*/
public Integer avgNewCusNum;
/**
* /()
*/
public Integer delCusNum;
/**
* ()
*/
public Float delCusRate;
}

@ -0,0 +1,40 @@
package com.kiisoo.ic.store.entity;
import lombok.Data;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.util.Date;
/**
* @Description:
* @Author: JYJ
* @Date: 2020/4/10 16:12
* @Version: v1.0
*/
@Data
public class WxCusInfoReqDO {
/**
*
*/
private String access_token;
/**
* id
*/
private String userid;
/**
*
*/
private Long start_time;
/**
*
*/
private Long end_time;
public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}
}

@ -0,0 +1,41 @@
package com.kiisoo.ic.store.entity;
import lombok.Data;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.util.Date;
/**
* @Description:
* @Author: JYJ
* @Date: 2020/4/10 16:12
* @Version: v1.0
*/
@Data
public class WxCusInfoRespDO {
/**
* / ()
*/
private Integer negative_feedback_cnt;
/**
*
*/
private Integer new_contact_cnt;
/**
* id
*/
private Long storeId;
/**
* id
*/
private Long regionId;
/**
*
*/
private Date statTime;
}

@ -0,0 +1,99 @@
package com.kiisoo.ic.store.mapper;
import com.kiisoo.ic.store.entity.StorePromotionDataDO;
import com.kiisoo.ic.store.entity.WxCusInfoRespDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
/**
* -- Mapper
* @author yechenhao
* @since 2020-04-09
*/
@Repository
public interface StorePromotionDataDOMapper {
/**
* 广
* @param storeIds
* @param regionId
* @param startDate
* @param endDate
* @return
*/
List<StorePromotionDataDO> selectPromotionDataGroupByRegion(@Param("storeIds") List<Long> storeIds,
@Param("regionId") Long regionId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
/**
* 广
* @param storeIds
* @param storeId
* @param regionId
* @param startDate
* @param endDate
* @return
*/
List<StorePromotionDataDO> selectPromotionDataGroupByStore(@Param("storeIds") List<Long> storeIds,
@Param("storeId") Long storeId,
@Param("regionId") Long regionId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
/**
* 广
* @param storeIds
* @param storeId
* @param regionId
* @param sellerId
* @param startDate
* @param endDate
* @return
*/
List<StorePromotionDataDO> selectPromotionDataGroupBySeller(@Param("storeIds") List<Long> storeIds,
@Param("storeId") Long storeId,
@Param("regionId") Long regionId,
@Param("sellerId") Long sellerId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
/**
*
* @return storePromotionDataDO
*/
List<StorePromotionDataDO> selectCustomer();
/**
*
* @param wxCusInfoRespDO
* @return
*/
int insertStorePromotionData(WxCusInfoRespDO wxCusInfoRespDO);
/**
*
* @param regionId
* @param startDate
* @param endDate
* @return
*/
StorePromotionDataDO selectWxCusInfoByRegionId(@Param("regionId") Long regionId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
/**
*
* @param storeId
* @param startDate
* @param endDate
* @return
*/
StorePromotionDataDO selectWxCusInfoByStoreId(@Param("storeId") Long storeId,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
}

@ -0,0 +1,40 @@
package com.kiisoo.ic.store.service;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author jinchaofan
* @date 2019/4/28 15:20
* @company kiisoo
* @details job 0 0 1 * * ?
*/
@JobHandler(value="storePromotionData")
@Component
@Slf4j
public class StorePromotionDataJob extends IJobHandler {
/**
* service
*/
@Autowired
private StorePromotionDataService storePromotionDataService;
@Override
public ReturnT<String> execute(String s) throws Exception {
try {
log.info("<--获取联系客户统计数据定时任务执行-->");
storePromotionDataService.syncCustomerBlackList();
log.info("<--获取联系客户统计数据定时任务结束-->");
} catch (Exception e) {
log.error("获取联系客户统计数据任务出错", e);
}
return SUCCESS;
}
}

@ -0,0 +1,160 @@
package com.kiisoo.ic.store.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kiisoo.ic.config.WxCpConfiguration;
import com.kiisoo.ic.domain.service.PrivilageDomainService;
import com.kiisoo.ic.store.constant.Constants;
import com.kiisoo.ic.store.entity.StorePromotionDataDO;
import com.kiisoo.ic.store.entity.WxCusInfoReqDO;
import com.kiisoo.ic.store.entity.WxCusInfoRespDO;
import com.kiisoo.ic.store.mapper.StorePromotionDataDOMapper;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @Description:
* @Author: JYJ
* @Date: 2020/4/10 10:50
* @Version: v1.0
*/
@Service
@Slf4j
public class StorePromotionDataService {
private WxCpService mainService = WxCpConfiguration.getCpService(1000037);
/**
* mapper
*/
@Autowired
private StorePromotionDataDOMapper storePromotionDataDOMapper;
/**
*
*/
@Autowired
private PrivilageDomainService privilageDomainService;
/**
* 广
* @param storeId
* @param regionId
* @param sellerId
* @param startDate
* @param endDate
* @param pageNum
* @param pageSize
* @return
*/
public PageInfo<StorePromotionDataDO> listStorePromotionDate(Long userId, Long storeId, Long regionId, Long sellerId,
Date startDate, Date endDate, String type, int pageNum, int pageSize) {
//用户权限下的店铺
List<Long> storeIds = privilageDomainService.listUserDatePermission(userId);
List<StorePromotionDataDO> promotionDataDOS;
if (Constants.STORE_DATA_TYPE_REGION.equals(type)) {
PageHelper.startPage(pageNum, pageSize);
promotionDataDOS = storePromotionDataDOMapper.selectPromotionDataGroupByRegion(storeIds, regionId, startDate, endDate);
} else if (Constants.STORE_DATA_TYPE_STORE.equals(type)){
PageHelper.startPage(pageNum, pageSize);
promotionDataDOS = storePromotionDataDOMapper.selectPromotionDataGroupByStore(storeIds, storeId, regionId, startDate, endDate);
} else {
PageHelper.startPage(pageNum, pageSize);
promotionDataDOS = storePromotionDataDOMapper.selectPromotionDataGroupBySeller(storeIds, storeId, regionId, sellerId, startDate, endDate);
}
if (!Constants.STORE_DATA_TYPE_SELLER.equals(type)) {
promotionDataDOS.forEach(promotionDataDO -> {
Long storeIdKey = promotionDataDO.getStoreId();
Long regionIdKey = promotionDataDO.getRegionId();
// 查询新增客户数 拉黑数量
StorePromotionDataDO wxCusInfoDO = null;
if (Constants.STORE_DATA_TYPE_REGION.equals(type)) {
wxCusInfoDO = storePromotionDataDOMapper.selectWxCusInfoByRegionId(regionIdKey, startDate, endDate);
} else if (Constants.STORE_DATA_TYPE_STORE.equals(type)) {
wxCusInfoDO = storePromotionDataDOMapper.selectWxCusInfoByStoreId(storeIdKey, startDate, endDate);
}
if (null != wxCusInfoDO) {
Integer newCusNum = wxCusInfoDO.getNewCusNum();
Integer delCusNum = wxCusInfoDO.getDelCusNum();
promotionDataDO.setNewCusNum(newCusNum);
promotionDataDO.setDelCusNum(delCusNum);
// 日增好友平均
int nDay = (int) ((startDate.getTime() - endDate.getTime()) / (24 * 60 * 60 * 1000));
promotionDataDO.setAvgNewCusNum(newCusNum / nDay);
// 拉黑率
Integer cusNum = promotionDataDO.getCusNum();
promotionDataDO.setDelCusRate(cusNum == null || cusNum == Constants.CONTACT_COUNT ?
0 : delCusNum * 1f / promotionDataDO.getCusNum());
}
});
}
return new PageInfo<>(promotionDataDOS);
}
/**
*
* @throws Exception
*/
@Transactional(rollbackFor = Exception.class)
public void syncCustomerBlackList() throws Exception{
//获取token
String token = mainService.getAccessToken();
List<StorePromotionDataDO> cusList = storePromotionDataDOMapper.selectCustomer();
cusList.forEach(customer -> {
// 返回
try {
String userId = customer.getUserId();
WxCusInfoReqDO wxCusInfoReqDO = new WxCusInfoReqDO();
wxCusInfoReqDO.setAccess_token(token);
wxCusInfoReqDO.setUserid(userId);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//todo 分开写便于同步历史数据 最大跨度30天 最久当天往前180天 上线时改为System.currentTimeMillis()
Date startDate = sdf.parse("2020-04-12");
Date endDate = sdf.parse("2020-04-12");
wxCusInfoReqDO.setStart_time(startDate.getTime()/1000);
wxCusInfoReqDO.setEnd_time(endDate.getTime()/1000);
WxCusInfoRespDO wxCusInfoRespDO = getWxCusInfo(wxCusInfoReqDO);
if (wxCusInfoRespDO.getNegative_feedback_cnt() == Constants.CONTACT_COUNT &&
wxCusInfoRespDO.getNew_contact_cnt() == Constants.CONTACT_COUNT) {
return;
}
wxCusInfoRespDO.setStoreId(customer.getStoreId());
wxCusInfoRespDO.setRegionId(customer.getRegionId());
wxCusInfoRespDO.setStatTime(startDate);
int syncRes = storePromotionDataDOMapper.insertStorePromotionData(wxCusInfoRespDO);
if (syncRes == Constants.INSERT_FAIL) {
throw new RuntimeException("向数据库导入客户统计数据时出错");
}
} catch (WxErrorException | ParseException e) {
log.error("请求企业微信api获取客户统计数据或日期转换出错", e);
}
});
}
/**
*
*
* @param wxCusInfoReqDO
* @return
* @throws WxErrorException
*/
public WxCusInfoRespDO getWxCusInfo(WxCusInfoReqDO wxCusInfoReqDO) throws WxErrorException {
String url = mainService.getWxCpConfigStorage().getApiUrl("/cgi-bin/externalcontact/get_user_behavior_data");
String result = mainService.post(url, wxCusInfoReqDO.toJson());
JSONObject parseObject = JSON.parseObject(result);
System.out.println(parseObject);
String[] behavior_data = JSONObject.parseObject(parseObject.getString("behavior_data"), String[].class);
return JSONObject.parseObject(behavior_data[0], WxCusInfoRespDO.class);
}
}

@ -0,0 +1,161 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kiisoo.ic.store.mapper.StorePromotionDataDOMapper">
<select id="selectPromotionDataGroupByRegion" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
SELECT
t3.id as regionId,
t3.NAME AS regionName,
sum( t4.customer_id ) AS cusNum
FROM
poi_store t1
LEFT JOIN op_seller_customer_relation t4 on t1.id = t4.store_id
<if test="startDate != null">
and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &gt; DATE_FORMAT(#{endDate},'%Y-%m-%d')
</if>
<if test="endDate != null">
and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &lt; DATE_FORMAT(#{endDate},'%Y-%m-%d')
</if>,
poi_store_region t2,
poi_region t3
WHERE
t1.id = t2.store_id
and t2.region_id = t3.id
<choose>
<when test="storeIds != null and storeIds.size > 0">
and t1.id in
<foreach collection="storeIds" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<otherwise>
and 1 = 0
</otherwise>
</choose>
<if test="regionId != null">
and t3.id = #{regionId}
</if>
and t1.status = 1
GROUP BY
t3.id
</select>
<select id="selectPromotionDataGroupByStore" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
SELECT
t1.id as storeId,
t1.NAME as storeName,
sum( t4.customer_id ) AS cusNum
FROM
poi_store t1
LEFT JOIN op_seller_customer_relation t4 on t1.id = t4.store_id
<if test="startDate != null">
and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &gt; DATE_FORMAT(#{endDate},'%Y-%m-%d')
</if>
<if test="endDate != null">
and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &lt; DATE_FORMAT(#{endDate},'%Y-%m-%d')
</if>,
poi_store_region t2
WHERE
t1.id = t2.store_id
<choose>
<when test="storeIds != null and storeIds.size > 0">
and t1.id in
<foreach collection="storeIds" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<otherwise>
and 1 = 0
</otherwise>
</choose>
<if test="storeId != null">
and t1.id = #{storeId}
</if>
<if test="regionId != null">
and t3.id = #{regionId}
</if>
and t1.status = 1
GROUP BY
t1.id
</select>
<select id="selectPromotionDataGroupBySeller" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
SELECT
t1.id AS sellerId,
t3.NAME AS sellerName,
sum( t2.customer_id ) AS cusNum
FROM
poi_store_staff t1
LEFT JOIN op_seller_customer_relation t2 ON t1.id = t2.user_id
<if test="startDate != null">
and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &gt; DATE_FORMAT(#{endDate},'%Y-%m-%d')
</if>
<if test="endDate != null">
and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &lt; DATE_FORMAT(#{endDate},'%Y-%m-%d')
</if>,
privilage_user t3,
poi_store_region t4
WHERE
t1.user_id = t3.id
and t1.store_id = t4.store_id
<choose>
<when test="storeIds != null and storeIds.size > 0">
and t1.id in
<foreach collection="storeIds" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<otherwise>
and 1 = 0
</otherwise>
</choose>
AND t1.type = 4
AND t1.STATUS = 1
AND t1.deleted = 0
AND t3.STATUS = 1
<if test="storeId != null">
and t1.store_id = #{storeId}
</if>
<if test="regionId != null">
and t4.region_id = #{regionId}
</if>
group by t1.id
</select>
<select id="selectCustomer" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
SELECT
t1.cp_user_id AS userid,
t2.store_id AS storeId,
t3.region_id AS regionId
FROM
privilage_cp_user t1,
privilage_cp_user_store t2,
poi_store_region t3
WHERE
t1.id = t2.cp_user_id
AND t2.store_id = t3.store_id
</select>
<insert id="insertStorePromotionData" parameterType="com.kiisoo.ic.store.entity.WxCusInfoRespDO">
INSERT INTO poi_customer_contact_data_stat (store_id, region_id,
negative_feedback_cnt, new_contact_cnt, stat_time, create_time) VALUES (#{storeId}, #{regionId},
#{negative_feedback_cnt}, #{new_contact_cnt}, #{statTime}, NOW())
</insert>
<select id="selectWxCusInfoByRegionId" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
SELECT sum( new_contact_cnt ) AS newCusNum,
sum( negative_feedback_cnt ) AS delCusNum
FROM poi_customer_contact_data_stat
WHERE region_id = #{regionId}
AND stat_time &gt; #{startDate}
AND stat_time &lt; #{endDate}
</select>
<select id="selectWxCusInfoByStoreId" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
SELECT sum( new_contact_cnt ) AS newCusNum,
sum( negative_feedback_cnt ) AS delCusNum
FROM poi_customer_contact_data_stat
WHERE store_id = #{storeId}
AND stat_time &gt; #{startDate}
AND stat_time &lt; #{endDate}
</select>
</mapper>
Loading…
Cancel
Save