package com.hzs.member.store.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.detail.CuMemberServiceLog;
import com.hzs.common.domain.member.store.CuStore;
import com.hzs.common.domain.member.store.ext.CuStoreExt;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.detail.service.ICuMemberServiceLogService;
import com.hzs.member.handlebusiness.dto.AnalysisBusinessAppealParam;
import com.hzs.member.store.ICuStoreServiceApi;
import com.hzs.member.store.dto.StoreDto;
import com.hzs.member.store.service.ICuStoreService;
import com.hzs.member.store.vo.CuStoreVO;
import com.hzs.system.base.IAreaServiceApi;
import com.hzs.system.base.ICountryServiceApi;
import com.hzs.system.base.dto.CountryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: hzs_java_cloud_africa
 * @BelongsPackage: com.hzs.member.store.provider
 * @Author: yh
 * @CreateTime: 2023-10-20  16:30
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@DubboService
public class ICuStoreServiceProvider implements ICuStoreServiceApi {

    @Autowired
    private ICuStoreService cuStoreService;
    @Autowired
    private ICuMemberServiceLogService memberServiceLogService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    @DubboReference
    private IAreaServiceApi areaServiceApi;
    @DubboReference
    private ICountryServiceApi iCountryServiceApi;

    /**
     * 查询用户开通店铺
     *
     * @param pkMember
     * @return
     */
    @Override
    public R<StoreDto> getMemberStore(Long pkMember) {
        CuStore store = cuStoreService.getOne(new QueryWrapper<CuStore>().eq("PK_MEMBER", pkMember));
        StoreDto storeDto = BeanUtil.copyProperties(store, StoreDto.class);
        return R.ok(storeDto);
    }

    @Override
    public R<StoreDto> getMemberStore(Long pkMember, Integer pkCountry) {
        CuStore store = cuStoreService.getOne(new QueryWrapper<CuStore>().eq("PK_MEMBER", pkMember).eq(SystemFieldConstants.PK_COUNTRY, pkCountry));
        if (null == store) {
            return R.ok(null);
        }
        return R.ok(BeanUtil.copyProperties(store, StoreDto.class));
    }

    /**
     * @description: 根据业务编号查询会员店铺信息
     * @author: zhang jing
     * @date: 2024/4/3 15:25
     * @param: [analysisBusinessAppealParam]
     * @return: com.hzs.common.core.domain.R<com.hzs.member.store.dto.StoreDto>
     **/
    @Override
    public R<StoreDto> getCuStoreByBusinessNo(AnalysisBusinessAppealParam analysisBusinessAppealParam) {
        QueryWrapper<CuMemberServiceLog> queryWrapper = new QueryWrapper();
        queryWrapper.eq("BUSINESS_NO", analysisBusinessAppealParam.getBusinessNo());
        List<CuMemberServiceLog> msList = memberServiceLogService.list(queryWrapper);
        StoreDto storeDto = new StoreDto();
        if (CollectionUtil.isNotEmpty(msList)) {
            Map<Integer, String> transactionMap1 = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_STORE_LEVEL);
            //查询店铺信息
            storeDto = JSONObject.parseObject(msList.get(0).getNewValue(), StoreDto.class);
            Map<Integer, String> areaDate = areaServiceApi.getAreaMap(storeDto.getPkCountry()).getData();
            CuStoreVO param = new CuStoreVO();
            param.setPkStoreLevel(storeDto.getPkStoreLevel());
            param.setPkCountry(storeDto.getPkCountry());
            param = cuStoreService.queryStoreLevelById(param);
            storeDto.setPkTransactionVal(transactionMap1.get(param.getPkTransaction()));
            String address = "";
            if (StringUtils.isNotNull(storeDto.getProvince())) {
                storeDto.setProvinceName(areaDate.get(storeDto.getProvince()));
                address += storeDto.getProvinceName() + " ";
            }
            if (StringUtils.isNotNull(storeDto.getCity())) {
                storeDto.setCityName(areaDate.get(storeDto.getCity()));
                address += storeDto.getCityName() + " ";
            }
            if (StringUtils.isNotNull(storeDto.getCounty())) {
                storeDto.setCountyName(areaDate.get(storeDto.getCounty()));
                address += storeDto.getCountyName() + " ";
            }
            if (StringUtils.isNotNull(storeDto.getBindingProvince())) {
                storeDto.setBindingProvinceName(areaDate.get(storeDto.getBindingProvince()));
            }
            address += storeDto.getAddress();
            storeDto.setAddress(address);
            if (StringUtils.isNotNull(storeDto.getBindingCountry())) {
                R<CountryDTO> country = iCountryServiceApi.getCountry(storeDto.getBindingCountry());
                storeDto.setBindingCountryName(country.getData().getShortName());
            }
        }
        return R.ok(storeDto);
    }

    @Override
    public R<List<StoreDto>> queryAllMemberStore() {
        List<CuStoreExt> cuStoreExtList = cuStoreService.queryAllMemberStore();
        return R.ok(BeanUtil.copyToList(cuStoreExtList, StoreDto.class));
    }

}
