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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.DataApiUtils;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.manage.web.qo.canvas.CanvasConversionClerkQO;
import com.gic.haoban.manage.web.qo.canvas.CanvasConversionDataQO;
import com.gic.haoban.manage.web.qo.canvas.CanvasConversionMemberQO;
import com.gic.haoban.manage.web.utils.EsMemberInfoUtils;
import com.gic.haoban.manage.web.vo.canvas.CanvasConversionDataVO;
import com.gic.haoban.manage.web.vo.canvas.CanvasConversionMemberVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 画布数据
 */
@Slf4j
@RestController
@RequestMapping("/canvas-data")
public class CanvasDataController {
    @Autowired
    private EsMemberInfoUtils esMemberInfoUtils;

    /**
     * 活动转化数据
     * @param qo
     * @return
     */
    @RequestMapping("get-canvas-conversion-data")
    public RestResponse<CanvasConversionDataVO> getCanvasConversionData(@RequestBody CanvasConversionDataQO qo) {
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(qo);
        setPageParam(jsonObject, null, null);
        //1画布维度2导购维度3会员维度
        jsonObject.put("dataType", 1);
        Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), "data_mkt_canvas_haoban_list");
        List<CanvasConversionDataVO> list = DataApiUtils.parsePageDataList(res, CanvasConversionDataVO.class);
        return RestResponse.successResult(list.size() > 0 ? list.get(0) : new CanvasConversionDataVO());
    }

    /**
     * 活动已完成的会员
     * @param qo
     * @return
     */
    @RequestMapping("get-canvas-conversion-member")
    public RestResponse<Page<CanvasConversionMemberVO>> getCanvasConversionMember(@RequestBody CanvasConversionMemberQO qo) {
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(qo);
        setPageParam(jsonObject, qo.getPageNum(), qo.getPageSize());
        //1导购2会员
        jsonObject.put("dataType", 2);
        Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), "data_mkt_canvas_haoban_clerk");

        Page<CanvasConversionMemberVO> page = DataApiUtils.getPageData(res, CanvasConversionMemberVO.class);
        List<CanvasConversionMemberVO> list = page.getResult();
        esMemberInfoUtils.setMemberInfo(qo.getEnterpriseId(), list, Stream.of("memberName","memberNick","photoUrl","clerkCode", "clerkName")
                .collect(Collectors.toSet()));
        return RestResponse.successResult(page);
    }

    /**
     * 活动导购详情
     * @param qo
     * @return
     */
    @RequestMapping("get-canvas-conversion-clerk")
    public RestResponse<Page<CanvasConversionMemberVO>> getCanvasConversionClerk(@RequestBody CanvasConversionClerkQO qo) {
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(qo);
        setPageParam(jsonObject, qo.getPageNum(), qo.getPageSize());
        //1导购2会员
        jsonObject.put("dataType", 1);
        Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), "data_mkt_canvas_haoban_clerk");

        Page<CanvasConversionMemberVO> page = DataApiUtils.getPageData(res, CanvasConversionMemberVO.class);
        List<CanvasConversionMemberVO> list = page.getResult();
        esMemberInfoUtils.setMemberInfo(qo.getEnterpriseId(), list, Stream.of("clerkCode", "clerkName")
                .collect(Collectors.toSet()));
        return RestResponse.successResult(page);
    }

    private void setPageParam(JSONObject jsonObject, Integer pageNum, Integer pageSize) {
        jsonObject.put("pageNum", pageNum == null ? 1 : pageNum);
        jsonObject.put("pageSize", pageSize == null ? 10 : pageSize);
    }

}
