package com.hzs.retail.member.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.hzs.common.core.utils.sign.Base64Util;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.retail.member.service.IMemberRetailService;
import com.hzs.retail.member.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 新零售首页控制器
 */
@RestController
@RequestMapping("/api/retail-member")
@Slf4j
public class RetailMemberController extends BaseController {

    @Autowired
    private ICuMemberService iCuMemberService;
    @Autowired
    private IMemberRetailService iMemberRetailService;

    /**
     * 获取登录会员信息
     *
     * @return
     */
    @GetMapping("/get-info")
    public AjaxResult getInfo() {
        RetailMemberInfoVO retailMemberInfoVO = iMemberRetailService.getMemberInfo(SecurityUtils.getUserId());
        if (null == retailMemberInfoVO) {
            return AjaxResult.error();
        }
        return AjaxResult.success(retailMemberInfoVO);
    }

    @GetMapping("/get-shared-image")
    public AjaxResult getSharedImage() {
        JSONObject result = new JSONObject();
        String url = iCuMemberService.getSharePosterImage();
        result.set("url", url);
        result.set("base64", Base64Util.toUrlBase64(url));
        return AjaxResult.success(result);
    }

    /**
     * 会员推荐列表
     *
     * @return
     */
    @GetMapping("/recommend-list")
    public TableDataInfo recommendList() {
        // 返回数据
        List<RecommendListVO> resultList = new ArrayList<>();

        startPage();
        List<CuMemberExt> cuMemberExtList = iCuMemberService.listRecommend(SecurityUtils.getUserId());
        if (CollectionUtil.isNotEmpty(cuMemberExtList)) {
            for (CuMemberExt cuMemberExt : cuMemberExtList) {
                resultList.add(BeanUtil.copyProperties(cuMemberExt, RecommendListVO.class));
            }
        }

        TableDataInfo tableDataInfo = getDataTable(cuMemberExtList);
        tableDataInfo.setRows(resultList);
        return tableDataInfo;
    }

}
