package cn.newfeifan.mall.module.sale.service.merchant;

import cn.newfeifan.mall.framework.common.util.json.JsonUtils;
import cn.newfeifan.mall.module.sale.controller.app.shop.vo.ShopRespVO;
import cn.newfeifan.mall.module.sale.dal.dataobject.merchant.MerchantDO;
import cn.newfeifan.mall.module.sale.dal.mysql.merchant.MerchantMapper;
import cn.newfeifan.mall.module.sale.service.shop.ShopService;
import cn.newfeifan.mall.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
import cn.newfeifan.mall.module.system.dal.dataobject.user.AdminUserDO;
import cn.newfeifan.mall.module.system.dal.mysql.user.AdminUserMapper;
import cn.newfeifan.mall.module.system.mq.message.login.LoginSendMessage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import cn.newfeifan.mall.module.sale.dal.redis.ShopMessage;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 商户 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class MerchantServiceImpl implements MerchantService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private AdminUserMapper userMapper;
    @Resource
    private ShopService shopService;
    @Resource
    private MerchantMapper merchantMapper;
    @Override
    public void doSendMail(LoginSendMessage message) {

        // 查询用户的商户信息,并且缓存到redis中
        OAuth2AccessTokenDO oAuth2AccessTokenDO = message.getOAuth2AccessTokenDO();
        ShopMessage shopMessage = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get("shop:" + message.getOAuth2AccessTokenDO().getUserId()), ShopMessage.class);
        if (shopMessage != null) {
            // 刷新时间
            stringRedisTemplate.opsForValue().set("shop:" + oAuth2AccessTokenDO.getUserId(), JsonUtils.toJsonString(shopMessage), 3600 * 24, TimeUnit.SECONDS);
        } else {
            // 查询数据库
            AdminUserDO adminUserDO = userMapper.selectById(message.getOAuth2AccessTokenDO().getUserId());
            if (adminUserDO.getShopId() != null) {
                ShopMessage addRedis = new ShopMessage().setShopId(adminUserDO.getShopId()).setMerId(adminUserDO.getMerchantId());
                stringRedisTemplate.opsForValue().set("shop:" + oAuth2AccessTokenDO.getUserId(), JsonUtils.toJsonString(addRedis), 3600 * 24, TimeUnit.SECONDS);
            } else {
                // 获取随机店铺
                List<ShopRespVO> merchantAllShop = shopService.getMerchantAllShop(adminUserDO.getMerchantId());
                oAuth2AccessTokenDO.setMerchantId(adminUserDO.getMerchantId());
                Long shopId = null;
                if (merchantAllShop.size() > 0) {
                    shopId = merchantAllShop.get(0).getId();
                    oAuth2AccessTokenDO.setShopId(shopId);
                }
                //存储redis
                ShopMessage addRedis = new ShopMessage().setShopId(shopId).setMerId(adminUserDO.getMerchantId());
                stringRedisTemplate.opsForValue().set("shop:" + oAuth2AccessTokenDO.getUserId(), JsonUtils.toJsonString(addRedis), 3600 * 24, TimeUnit.SECONDS);
            }
        }
    }

    @Override
    public MerchantDO getMerchantByMerchantId(Long merchantId) {
        return merchantMapper.selectOne(new LambdaQueryWrapper<MerchantDO>().eq(MerchantDO::getId, merchantId));
    }

}