package com.ms.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.doudian.open.core.AccessToken;
import com.ms.api.dto.dsapi.response.CheckDsAppAuthExpireResponseDTO;
import com.ms.api.dto.dsapi.response.CommonResponseDTO;
import com.ms.api.dto.dsapi.response.GetPlatformAuthUrlResponseDTO;
import com.ms.biz.dto.openspi.request.QuerySupplyPlatformAuthParam;
import com.ms.biz.dto.openspi.response.QuerySupplyPlatformAuthData;
import com.ms.biz.service.DsApiService;
import com.ms.biz.service.ShopService;
import com.ms.biz.bo.ShopBO;
import com.ms.biz.tool.CommonTool;
import com.ms.biz.tool.SecurityTool;
import com.ms.dal.entity.Shop;
import com.ms.dal.entity.ShopTo1688DsMember;
import com.ms.dal.mapper.ShopMapper;
import com.ms.dal.mapper.ShopTo1688DsMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopTo1688DsMemberMapper shopTo1688DsMemberMapper;

    @Autowired
    private DsApiService dsApiService;

    @Override
    public Shop getDetailById(Long id) {
        return shopMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return shopMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Shop record) {
        return shopMapper.insertSelective(record);
    }

    @Override
    public int insertSelective(Shop record) {
        return shopMapper.insertSelective(record);
    }

    @Override
    public Shop selectByPrimaryKey(Long id) {
        return shopMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Shop record) {
        return shopMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Shop record) {
        return shopMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Shop> getShopByIds(List<String> shopIds) {
        if (ObjectUtil.isEmpty(shopIds)) {
            return new ArrayList<Shop>();
        }
        List<Shop> shopList = shopMapper.getShopByIds(shopIds);
        if (ObjectUtil.isEmpty(shopList)) {
            return new ArrayList<Shop>();
        }
        return shopList;
    }

    @Override
    public List<Shop> getExpiresTokenShops(String gmtExpireStart,String gmtExpireEnd) {
        return shopMapper.getExpiresTokenShops(gmtExpireStart,gmtExpireEnd);
    }

    private AccessToken getShopAccessTokenByShop(Shop shop) {
        return null;
    }

    @Override
    public ShopBO getShopInfo(Long shopId) {
        System.out.println(shopId);
        Shop shop = getDetailById(shopId);
        if (shop == null) {
            throw new RuntimeException("店铺不存在");
        }
        ShopBO shopBO = CommonTool.modelMapper(shop, ShopBO.class);
        shopBO.setShopAccessToken(getShopAccessTokenByShop(shop));
        return shopBO;
    }

    @Override
    public ShopBO getShopInfoByShop(Shop shop) {
        ShopBO shopBO = CommonTool.modelMapper(shop, ShopBO.class);
        shopBO.setShopAccessToken(getShopAccessTokenByShop(shop));
        return shopBO;
    }

    public void querySupplyPlatformAuth(QuerySupplyPlatformAuthParam param, QuerySupplyPlatformAuthData data) {
        Long shopId = param.getShopId();
        if (shopId == null || shopId == 0) {
            throw new RuntimeException("店铺ID错误");
        }
        if (param.getScene() == 1) {
            data.setAuthorized(true);
            data.setAuthUrl("");
            return;
        }
        ShopBO shop = new ShopBO();
        String shopName = SecurityTool.MD5(shopId.toString());
        assert shopName != null;
        shopName = shopName.substring(16);
        shop.setShopName("店铺-" + shopName);
        shop.setShopId(shopId);
        ShopTo1688DsMember shopTo1688DsMember = shopTo1688DsMemberMapper.getDetailByShopId(shopId);
        boolean authorized = false;
        if (shopTo1688DsMember != null && shopTo1688DsMember.getGmtExpire() != null) {
            Date gmtExpire = CommonTool.parseDate(shopTo1688DsMember.getGmtExpire());
            if ((new Date()).before(gmtExpire)) {
                authorized = true;
            }
        }

        CheckDsAppAuthExpireResponseDTO responseDTO = dsApiService.checkDsAppAuthExpire(shopId, "", "");
        CommonResponseDTO res = responseDTO.getUserIdAndCheckRetMap().get(shopId);
        if (!res.isSuccess()) {
            authorized = false;
        }

        GetPlatformAuthUrlResponseDTO response = dsApiService.getPlatformAuthUrl(shop);
        if (response.isSuccess()) {
            data.setAuthorized(authorized);
            data.setAuthUrl(response.getRedirectUrl());
        } else {
            throw new RuntimeException(response.getReason());
        }
    }
}




