package shop.alien.gateway.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreInfo;
import shop.alien.entity.store.StoreUser;
import shop.alien.entity.store.vo.StoreUserVo;
import shop.alien.gateway.config.BaseRedisService;
import shop.alien.gateway.mapper.StoreInfoGatewayMapper;
import shop.alien.gateway.mapper.StoreUserGatewayMapper;
import shop.alien.gateway.service.StoreUserService;
import shop.alien.util.common.JwtUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 二期-门店用户 服务实现类
 *
 * @author ssk
 * @since 2024-12-11
 */
@Transactional
@Service
@RequiredArgsConstructor
public class StoreUserServiceImpl extends ServiceImpl<StoreUserGatewayMapper, StoreUser> implements StoreUserService {

    @Value("${jwt.expiration-time}")
    private String effectiveTime;

    /**
     * 设定初始化默认密码
     */

    private final StoreInfoGatewayMapper storeInfoMapper;

    private final BaseRedisService baseRedisService;

    /**
     * token
     *
     * @param storeUser
     * @return
     */
    @Override
    public R<StoreUserVo> createToKen(StoreUser storeUser) {
        int effectiveTimeInt = Integer.parseInt(effectiveTime.substring(0, effectiveTime.length() - 1));
        String effectiveTimeUnit = effectiveTime.substring(effectiveTime.length() - 1);
        long effectiveTimeIntLong = 0L;
        switch (effectiveTimeUnit) {
            case "s": {
                effectiveTimeIntLong = effectiveTimeInt * 1000L;
                break;
            }
            case "m": {
                effectiveTimeIntLong = effectiveTimeInt * 60L * 1000L;
                break;
            }
            case "h": {
                effectiveTimeIntLong = effectiveTimeInt * 60L * 60L * 1000L;
                break;
            }
            case "d": {
                effectiveTimeIntLong = effectiveTimeInt * 24L * 60L * 60L * 1000L;
                break;
            }
        }

        StoreUserVo storeUserVo = new StoreUserVo();
        BeanUtils.copyProperties(storeUser, storeUserVo);
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("phone", storeUser.getPhone());
        tokenMap.put("userName", storeUser.getName());
        tokenMap.put("userId", storeUser.getId().toString());
        tokenMap.put("userType", "store");
        storeUserVo.setToken(JwtUtil.createJWT("store_" + storeUser.getPhone(), storeUser.getName(), JSONObject.toJSONString(tokenMap), effectiveTimeIntLong));
        baseRedisService.setString("store_" + storeUser.getPhone(), storeUserVo.getToken());
        StoreInfo storeInfo = storeInfoMapper.selectById(storeUser.getStoreId());
        if (storeInfo != null) {
            storeUserVo.setBusinessSection(storeInfo.getBusinessSection());
            storeUserVo.setBusinessTypesName(storeInfo.getBusinessTypesName());
        }
        return R.data(storeUserVo);
    }

}
