
package com.tmerclub.cloud.flow.controller.app;

import com.tmerclub.cloud.common.leaf.manager.SegmentManager;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.flow.constant.DistributedIdKey;
import com.tmerclub.cloud.flow.constant.FlowLogPageEnum;
import com.tmerclub.cloud.flow.constant.FlowVisitEnum;
import com.tmerclub.cloud.flow.dto.FlowLogDTO;
import com.tmerclub.cloud.flow.service.FlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 流量分析—页面分析
 *
 * @author YXF
 * @date 2021-05-21 15:25:19
 */
@RestController("appPageAnalyseController")
@RequestMapping("/ma/flow")
@Tag(name = "app流量分析—页面分析")
public class FlowLogController {

    private static final Logger log = LoggerFactory.getLogger(FlowLogController.class);

    @Autowired
    private FlowService flowService;
    @Autowired
    private SegmentManager segmentManager;

    private static final Pattern SPU_ID_PATTERN = Pattern.compile("[0-9]+");

    @PostMapping
    @Operation(summary = "保存用户操作信息", description = "保存用户操作信息")
    public ServerResponseEntity<Void> page(@RequestBody FlowLogDTO flowLogDTO) {
        // 数据为空，打印日志
        if (checkDate(flowLogDTO)) {
            return ServerResponseEntity.success();
        }
        flowService.log(flowLogDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getId")
    @Operation(summary = "获取用户操作id", description = "获取用户操作id")
    public ServerResponseEntity<Long> getId() {
        Long data = segmentManager.getSegmentId(DistributedIdKey.tmerclub_FLOW_USER_ID);
        return ServerResponseEntity.success(data);
    }
    /**
     * 数据是否错误
     * @param flowLogDTO
     * @return
     */
    private Boolean checkDate(FlowLogDTO flowLogDTO) {
        if(Objects.isNull(flowLogDTO.getPid()) || Objects.isNull(flowLogDTO.getOid()) ||
                Objects.isNull(flowLogDTO.getVe()) || Objects.isNull(flowLogDTO.getS()) || Objects.isNull(flowLogDTO.getSt())){
            log.error("用户流量记录-基本数据不全：{}", flowLogDTO);
            return Boolean.TRUE;
        }
        if (Objects.equals(flowLogDTO.getVe(), FlowVisitEnum.SHOP_CAT.value())) {
            if (Objects.isNull(flowLogDTO.getBid())) {
                log.error("用户流量记录-购物车:商品id为空{}", flowLogDTO);
                return Boolean.TRUE;
            }
            if (Objects.isNull(flowLogDTO.getN())) {
                log.error("用户流量记录-加购:加购数量为空{}", flowLogDTO);
                return Boolean.TRUE;
            }
        }
        if (Objects.equals(flowLogDTO.getPid(), FlowLogPageEnum.PROD_INFO.value())) {
            if (Objects.isNull(flowLogDTO.getBid())) {
                log.error("用户流量记录-商品详情:商品id为空{}", flowLogDTO);
                return Boolean.TRUE;
            }
            if (!SPU_ID_PATTERN.matcher(flowLogDTO.getBid()).matches()) {
                log.error("用户流量记录-商品详情:商品id数据错误{}", flowLogDTO);
                return Boolean.TRUE;
            }
        }
        boolean order = Objects.equals(flowLogDTO.getPid(), FlowVisitEnum.CONFIRM_ORDER.value()) ||
                Objects.equals(flowLogDTO.getPid(), FlowVisitEnum.PAY_SUCCESS.value());
        if (order && Objects.isNull(flowLogDTO.getBid())) {
            log.error("用户流量记录-支付:支付单号为空{}", flowLogDTO);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
