package com.xiaoniu.system.platformsettings.information.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaoniu.common.core.constant.CacheConstants;
import com.xiaoniu.common.core.utils.StringUtils;
import com.xiaoniu.common.redis.service.RedisService;
import com.xiaoniu.system.platformsettings.information.domain.TbPlatformInformation;
import com.xiaoniu.system.platformsettings.information.dto.EditPlatformInformationDTO;
import com.xiaoniu.system.platformsettings.information.mapper.TbPlatformInformationMapper;
import com.xiaoniu.system.platformsettings.information.service.ITbPlatformInformationService;
import com.xiaoniu.system.platformsettings.information.vo.PlatformInformationVO;
import com.xiaoniu.system.platformsettings.information.vo.SignPointsInfoVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * <p>
 * 平台信息设置信息表 服务实现类
 * </p>
 *
 * @author haoxuyang
 * @since 2024-10-10 03:32:25
 */
@Service
public class TbPlatformInformationServiceImpl extends ServiceImpl<TbPlatformInformationMapper, TbPlatformInformation> implements ITbPlatformInformationService {

    @Resource
    private RedisService redisService;

    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public PlatformInformationVO details() {
        PlatformInformationVO platformInformationVO = new PlatformInformationVO();
        TbPlatformInformation tbPlatformInformation = baseMapper.selectOne(new LambdaQueryWrapper<TbPlatformInformation>().last("limit 1"));
        if (StringUtils.isNotNull(tbPlatformInformation)) {
            BeanUtils.copyProperties(tbPlatformInformation, platformInformationVO);
            if (StringUtils.isNotEmpty(tbPlatformInformation.getSignPoints())) {
                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    List<SignPointsInfoVO> signPointsInfoVOList = objectMapper.readValue(tbPlatformInformation.getSignPoints(), new TypeReference<List<SignPointsInfoVO>>() {
                    });
                    platformInformationVO.setSignPointsInfoVOList(signPointsInfoVOList);
                } catch (JsonProcessingException e) {
                    log.error("转换失败{}" + e.getMessage());
                }
            }
        }
        return platformInformationVO;
    }

    @Override
    public void edit(EditPlatformInformationDTO dto) {
        TbPlatformInformation tbPlatformInformation = new TbPlatformInformation();
        BeanUtils.copyProperties(dto, tbPlatformInformation);
        List<SignPointsInfoVO> signPointsInfoVOList = dto.getSignPointsInfoVOList();
        if (!CollectionUtils.isEmpty(signPointsInfoVOList)) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String signPointsString = objectMapper.writeValueAsString(signPointsInfoVOList);
                tbPlatformInformation.setSignPoints(signPointsString);

            } catch (JsonProcessingException e) {
                log.error("转换失败{}" + e.getMessage());
            }
        }
        if (StringUtils.isNull(dto.getId())) {
            baseMapper.insert(tbPlatformInformation);
        } else {
            baseMapper.updateById(tbPlatformInformation);
        }
        //给缓存存储一份，以便后期使用
        threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                setCache(tbPlatformInformation);
            }
        });
    }

    public void setCache(TbPlatformInformation information) {
        String redisKey = CacheConstants.INFORMATION_SETTING_KEY;
        //车友群
        Map<String, Object> carGroupMap = new HashMap<>();
        carGroupMap.put("carGroupQrCode", information.getCarGroupQrCode());
        carGroupMap.put("membershipNotice", information.getMembershipNotice());
        redisService.setCacheObject(redisKey + "carGroup", carGroupMap);
        //客服信息
        Map<String, Object> cusServiceMap = new HashMap<>();
        cusServiceMap.put("vxQrCode", information.getVxQrCode());
        cusServiceMap.put("cusServicePhone", information.getCusServicePhone());
        cusServiceMap.put("cusServiceStartTime", information.getCusServiceStartTime());
        cusServiceMap.put("cusServiceEndTime", information.getCusServiceEndTime());
        redisService.setCacheObject(redisKey + "cusService", cusServiceMap);
    }
}
