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

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.content.api.dto.account.AccountAuthorizeDTO;
import com.gic.content.api.dto.account.AccountAuthorizeResultDTO;
import com.gic.content.api.dto.account.ContentAccountInfoDTO;
import com.gic.content.api.enums.ContentAccountQrCodeSourceType;
import com.gic.content.api.qdto.account.AccountGenerateQrCodeQDTO;
import com.gic.content.api.qdto.account.AccountScanQrCodeResultQDTO;
import com.gic.content.api.qdto.account.ContentAccountQDTO;
import com.gic.content.api.service.ContentAccountApiService;
import com.gic.haoban.manage.web.controller.content.adaptor.ClerkStoreAdaptor;
import com.gic.haoban.manage.web.qo.content.account.AccountGenerateQrCodeQo;
import com.gic.haoban.manage.web.qo.content.account.AccountInfoQo;
import com.gic.haoban.manage.web.qo.content.account.AccountScanResultQo;
import com.gic.haoban.manage.web.vo.content.account.AccountAuthorizeResultVo;
import com.gic.haoban.manage.web.vo.content.account.AccountAuthorizeVo;
import com.gic.haoban.manage.web.vo.content.account.ContentAccountInfoVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 好办-视频号-授权
 * @Author MUSI
 * @Date 2023/10/29 9:33 PM
 * @Description
 * @Version
 **/

@RestController
@RequestMapping(path = "/content/account")
public class ContentAccountController {

    @Autowired
    private ContentAccountApiService contentAccountApiService;
    @Autowired
    private ClerkStoreAdaptor clerkStoreAdaptor;

    /**
     * 查询导购当前账号视频号列表
     * @param accountInfoQo
     * @return
     */
    @RequestMapping(path = "/list")
    private RestResponse<List<ContentAccountInfoVo>> queryAccountList(AccountInfoQo accountInfoQo) {

        ClerkDTO clerkDTO = clerkStoreAdaptor.queryClerkInfo(accountInfoQo.getEnterpriseId(), accountInfoQo.getClerkId());
        if (clerkDTO == null) {
            return RestResponse.successResult(Collections.emptyList());
        }
        ContentAccountQDTO contentAccountQDTO = new ContentAccountQDTO();
        contentAccountQDTO.setEnterpriseId(accountInfoQo.getEnterpriseId());
        contentAccountQDTO.setQueryForClerk(Boolean.TRUE);
        contentAccountQDTO.setUserCodes(Collections.singletonList(clerkDTO.getClerkCode()));
        contentAccountQDTO.setStoreIds(Collections.singletonList(clerkDTO.getStoreId()));
        ServiceResponse<Page<ContentAccountInfoDTO>> serviceResponse =
                contentAccountApiService.queryAccountList(contentAccountQDTO);
        if (!serviceResponse.isSuccess() || CollectionUtils.isEmpty(serviceResponse.getResult().getResult())) {
            return RestResponse.successResult();
        }
        List<ContentAccountInfoVo> contentAccountInfoVos = serviceResponse.getResult()
                .getResult()
                .stream()
                .map(item -> EntityUtil.changeEntityByJSON(ContentAccountInfoVo.class, item))
                .collect(Collectors.toList());
        return RestResponse.successResult(contentAccountInfoVos);
    }


    /**
     * 获取登陆/授权二维码
     * @param accountGenerateQrCodeQo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/generate/qrcode")
    public RestResponse<AccountAuthorizeVo> generateQrCode(AccountGenerateQrCodeQo accountGenerateQrCodeQo) {

        AccountGenerateQrCodeQDTO accountGenerateQrCodeQDTO = new AccountGenerateQrCodeQDTO();
        accountGenerateQrCodeQDTO.setEnterpriseId(accountGenerateQrCodeQo.getEnterpriseId());
        accountGenerateQrCodeQDTO.setSourceType(ContentAccountQrCodeSourceType.HAOBAN.getCode());
        accountGenerateQrCodeQDTO.setKey(accountGenerateQrCodeQo.getKey());
        ServiceResponse<AccountAuthorizeDTO> serviceResponse = contentAccountApiService.generateQrCode(accountGenerateQrCodeQDTO);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure("500", "生成二维码异常");
        }
        return RestResponse.successResult(EntityUtil.changeEntityByJSON(AccountAuthorizeVo.class, serviceResponse.getResult()));
    }

    /**
     * 二维码轮询结果
     * @param accountScanResultQo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/loop/query/scan/qrcode/result")
    public RestResponse<AccountAuthorizeResultVo> queryScanQrCodeResult(AccountScanResultQo accountScanResultQo) {
        AccountScanQrCodeResultQDTO accountScanQrCodeResultQDTO = EntityUtil.changeEntityByJSON(AccountScanQrCodeResultQDTO.class, accountScanResultQo);
        ServiceResponse<AccountAuthorizeResultDTO> serviceResponse =
                contentAccountApiService.queryScanQrcodeResult(accountScanQrCodeResultQDTO);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure("500", "获取结果异常");
        }
        return RestResponse.successResult(EntityUtil.changeEntityByJSON(AccountAuthorizeResultVo.class, serviceResponse.getResult()));
    }
}
