package com.slodon.b2b2c.controller.statistics.front;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.enums.ProvinceJson;
import com.slodon.b2b2c.enums.StatsConstant;
import com.slodon.b2b2c.statistics.pojo.MemberBehavior;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Api(tags = "front-用户行为埋点")
@RestController
@RequestMapping("v3/statistics/front/member/behavior")
public class FrontMemberBehaviorController {

    @Resource
    private MongoTemplate mongoTemplate;

    @ApiOperation("保存用户行为")
    @PostMapping("save")
    public JsonResult save(HttpServletRequest request, String u) {
        //解码
        String json = Base64.decodeStr(u);
        MemberBehavior memberBehavior = JSON.parseObject(json, MemberBehavior.class);
        memberBehavior.setWebSite(WebUtil.getWebSite(request));
        if (memberBehavior == null) {
            return SldResponse.success();
        }
        if (memberBehavior.getMemberId() == null) {
            memberBehavior.setMemberId(0);
        }
        memberBehavior.setBehaviorTime(new Date());
        //根据省名称获取省编码，采用名称模糊搜索
        if (StringUtils.isEmpty(memberBehavior.getProvinceName())){
            memberBehavior.setProvinceCode(ProvinceJson.defaultProvinceCode);
            memberBehavior.setProvinceName(ProvinceJson.defaultProvinceName);
        }else {
            for (Map.Entry<String, String> entry : ProvinceJson.provinceMap.entrySet()) {
                String provinceCode = entry.getKey();
                String provinceName = entry.getValue();
                if (provinceName.contains(memberBehavior.getProvinceName())) {
                    memberBehavior.setProvinceCode(provinceCode);
                    memberBehavior.setProvinceName(provinceName);
                    break;
                }
            }
        }

        mongoTemplate.insert(memberBehavior, "stats_member_behavior");
        switch (memberBehavior.getBehaviorType()) {
            case StatsConstant.GOODS_PAGE_VIEW:
                //商品浏览，额外存一条店铺浏览和平台浏览
                memberBehavior.setBehaviorType(StatsConstant.STORE_PAGE_VIEW);
                mongoTemplate.insert(memberBehavior, "stats_member_behavior");
                memberBehavior.setBehaviorType(StatsConstant.PAGE_VIEW);
                mongoTemplate.insert(memberBehavior, "stats_member_behavior");
                break;
            case StatsConstant.STORE_PAGE_VIEW:
                //店铺浏览，额外存一条平台浏览
                memberBehavior.setBehaviorType(StatsConstant.PAGE_VIEW);
                mongoTemplate.insert(memberBehavior, "stats_member_behavior");
                break;
            default:
                break;
        }
        //会员登录时更新之前的会员id
        if (!StringUtil.isNullOrZero(memberBehavior.getMemberId())) {
            Query query = new Query();
            Criteria criteria = new Criteria();
            criteria.and("uuid").is(memberBehavior.getUuid()).and("memberId").is(0);
            query.addCriteria(criteria);

            Update update = new Update();
            update.set("memberId", memberBehavior.getMemberId());

            mongoTemplate.updateMulti(query, update, "stats_member_behavior");
        }
        return SldResponse.success();
    }
}
