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

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.web.vo.MemberChatVO;
import com.gic.marketing.api.service.MemberTagMarketingApiService;
import com.gic.member.api.dto.MemberDTO;
import com.gic.member.api.service.MemberService;
import com.gic.search.engine.api.dto.ESResponseQueryCount;
import com.gic.search.engine.api.dto.dynamic.DynamicSearchDTO;
import com.gic.search.engine.api.service.dynamic.ESDataDynamicOperationApiService;

@RestController
@RequestMapping("/member")
public class MemberController {

    private static final String ES_COLUMN_CATEGORY_CODE = "member";
    @Autowired
    private MemberService memberService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private ESDataDynamicOperationApiService esApiService;
    @Autowired
    private MemberTagMarketingApiService memberTagMarketingApiService;

    @RequestMapping("member-info")
    public RestResponse<Object> memberInfo(String memberId) {
        MemberDTO member = this.memberService.getMember(memberId);
        MemberChatVO vo = new MemberChatVO();
        if (null != member) {
            String name = member.getMemberName();
            String nick = member.getThirdNickname();
            String memberName = "";
            if (StringUtils.isNoneEmpty(name, nick)) {
                memberName = name + "-" + nick;
            } else if (StringUtils.isAllBlank(name, nick)) {
                memberName = "--";
            } else if (StringUtils.isNotBlank(name)) {
                memberName = name;
            } else {
                memberName = nick;
            }
            vo.setMemberName(memberName);
            vo.setMemberImage(member.getThirdImgUrl());
            vo.setPhone(member.getPhoneNumber());
        }
        return RestResponse.successResult(vo);
    }

    /**
     * 预估人数
     */
    @RequestMapping("member-count")
    @ResponseBody
    public RestResponse<Object> getMemberCount(String searchParams, @RequestParam(defaultValue="0")int type) {
        if (StringUtils.isBlank(searchParams)) {
            return RestResponse.successResult(0);
        }
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String enterpriseId = loginUser.getEnterpriseId();
        String clerkId = loginUser.getClerkId();
        int count = 0;
        try {
            // 会员分组
            if (type == 1) {
                searchParams = memberTagMarketingApiService.getMemberTagJson(enterpriseId, searchParams, clerkId);
            }
            JSONObject searchJson = new JSONObject();
            if (!StringUtils.isBlank(searchParams)) {
                searchJson = JSON.parseObject(searchParams);
            }
            DynamicSearchDTO search = new DynamicSearchDTO();
            searchJson = this.storeService.parseSelectData(searchJson, enterpriseId, clerkId, new Date());
            search.setSearchJson(searchJson);
            search.setEnterpriseId(enterpriseId);
            search.setColumnCategoryCode(ES_COLUMN_CATEGORY_CODE);
            ESResponseQueryCount esCount = esApiService.queryDataCount(search);
            count = (int) esCount.getRes();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResponse.successResult(count);
    }

}
