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.ContentAccountBizTypeEnum;
import com.gic.content.api.enums.ContentAccountQrCodeSourceType;
import com.gic.content.api.enums.ContentAccountStatusEnum;
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.api.dto.StaffClerkInfoDTO;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
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) {


        // 查询成员
        List<StaffClerkRelationDTO> staffClerkRelationDTOS = clerkStoreAdaptor.queryStaffClerkRelationInfo(accountInfoQo.getEnterpriseId(), accountInfoQo.getStaffId());
        if (CollectionUtils.isEmpty(staffClerkRelationDTOS)) {
            return RestResponse.successResult();
        }
        List<String> clerkIds = staffClerkRelationDTOS
                .stream()
                .map(StaffClerkRelationDTO::getClerkId)
                .distinct()
                .collect(Collectors.toList());
        List<String> storeIds = new ArrayList<>();
        List<String> clerkCodes = new ArrayList<>();
        List<ClerkDTO> clerkDTOS = clerkStoreAdaptor.queryClerkInfos(clerkIds);
        if (CollectionUtils.isEmpty(clerkDTOS)) {
            return RestResponse.successResult();
        }
        for (ClerkDTO clerkDTO : clerkDTOS) {
            clerkCodes.add(clerkDTO.getClerkCode());
            if (new Integer(1).equals(clerkDTO.getClerkType())) {
                storeIds.add(clerkDTO.getStoreId());
            }
        }
        ContentAccountQDTO contentAccountQDTO = new ContentAccountQDTO();
        contentAccountQDTO.setEnterpriseId(accountInfoQo.getEnterpriseId());
        contentAccountQDTO.setQueryForClerk(Boolean.TRUE);
        contentAccountQDTO.setUserCodes(clerkCodes);
        contentAccountQDTO.setStoreIds(storeIds);
        // 视频号账号暂时下架了
        contentAccountQDTO.setBizType(ContentAccountBizTypeEnum.XHS.getCode());
        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()
                .filter(item -> !Objects.equals(item.getStatus(), ContentAccountStatusEnum.UNBOUND.getCode()))
                .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.setId(accountGenerateQrCodeQo.getId());
        accountGenerateQrCodeQDTO.setEnterpriseId(accountGenerateQrCodeQo.getEnterpriseId());
        accountGenerateQrCodeQDTO.setSourceType(ContentAccountQrCodeSourceType.HAOBAN.getCode());
        accountGenerateQrCodeQDTO.setKey(accountGenerateQrCodeQo.getKey());
        accountGenerateQrCodeQDTO.setAccountId(accountGenerateQrCodeQo.getAccountId());
        accountGenerateQrCodeQDTO.setBizType(accountGenerateQrCodeQo.getBizType());
        ServiceResponse<AccountAuthorizeDTO> serviceResponse = contentAccountApiService.generateQrCode(accountGenerateQrCodeQDTO);
        if (!serviceResponse.isSuccess() || serviceResponse.getResult() == null) {
            return RestResponse.failure(serviceResponse.getCode(),serviceResponse.getMessage());
        }
        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", "获取结果异常");
        }
        AccountAuthorizeResultVo result = EntityUtil.changeEntityByJSON(AccountAuthorizeResultVo.class, serviceResponse.getResult());
        result.setStatus(convertStatus(serviceResponse.getResult().getStatus()));
        return RestResponse.successResult(result);
    }

    /**
     * 好办状态和GIC后台不一致，此处状态做转换 原因未知
     * @param status
     * @return
     */
    public Integer convertStatus(Integer status) {
        if (Objects.equals(status, 1)) {
            return 0;
        }
        return status;
    }
}
