package com.gic.haoban.manage.web.controller;

import com.alibaba.fastjson.JSON;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.common.anno.IgnoreLogin;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.MaidianLogDTO;
import com.gic.haoban.manage.api.service.MaidianLogApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MaidianLogController extends WebBaseController {
    private static final Logger logger = LoggerFactory.getLogger(MaidianLogController.class);

    @Autowired
    private MaidianLogApiService maidianLogApiService;
    @Autowired
    private ClerkService clerkService;
    @Autowired
    private StoreService storeService;

    @RequestMapping("save-maidian-log")
    @IgnoreLogin
    public HaobanResponse saveUserLoginLog(String maidianLogStr) {
        if (StringUtils.isBlank(maidianLogStr)) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        if (!isJsonArray(maidianLogStr)) {
            logger.error("埋点格式不正确:{}", maidianLogStr);
            return resultResponse(HaoBanErrCode.ERR_1);
        }
        List<MaidianLogDTO> maidianLogDTOList = JSON.parseArray(maidianLogStr, MaidianLogDTO.class);
        for (MaidianLogDTO maidianLogDTO : maidianLogDTOList) {
            String options = maidianLogDTO.getOptions();
            if (StringUtils.isNotBlank(options) && options.length() > 2000) {
                maidianLogDTO.setOptions(options.substring(0, 1999));
            }
            StoreDTO store = storeService.getStore(maidianLogDTO.getStoreId());
            if (store != null) {
                //好办无关业务，暂不替换
                com.gic.clerk.api.dto.ClerkDTO clerkDTO = clerkService.getClerkByClerkCode(store.getEnterpriseId(), maidianLogDTO.getClerkCode());
                maidianLogDTO.setEnterpriseId(store.getEnterpriseId());
                if (clerkDTO != null) {
                    //导购变更门店
                    if (!clerkDTO.getStoreId().equals(maidianLogDTO.getStoreId())){
                        continue;
                    }
                    maidianLogDTO.setClerkId(clerkDTO.getClerkId());
                }
            }
            maidianLogApiService.insert(maidianLogDTO);
        }
        return resultResponse(HaoBanErrCode.ERR_1);

    }

    /**
     * 检查JSON数据合法性
     */
    public boolean isJsonArray(String str) {
        try {
            JSON.parseArray(str);
            return true;
        } catch (Exception e) {
            logger.error("埋点格式不正确：{}", str, e);
            return false;
        }
    }

}
