package com.fs.watch.service.impl;

import com.alibaba.fastjson.JSON;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.DeviceFamily;
import com.fs.watch.domain.WatchFamilyUser;
import com.fs.watch.domain.WatchUser;
import com.fs.watch.domain.vo.WatchFamilyUserVo;
import com.fs.watch.mapper.WatchFamilyUserMapper;
import com.fs.watch.mapper.WatchUserMapper;
import com.fs.watch.service.WatchFamilyUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 家人腕表配置Service业务层处理
 *
 * @author fs
 * @date 2024-12-05
 */
@Service
public class WatchFamilyUserServiceImpl implements WatchFamilyUserService
{
    @Autowired
    private WatchFamilyUserMapper watchFamilyUserMapper;
    @Autowired
    private WatchUserMapper watchUserMapper;

    /**
     * 查询家人腕表配置
     *
     * @param id 家人腕表配置主键
     * @return 家人腕表配置
     */
    @Override
    public WatchFamilyUser selectWatchFamilyUserById(Long id)
    {
        return watchFamilyUserMapper.selectWatchFamilyUserById(id);
    }

    /**
     * 查询家人腕表配置列表
     *
     * @param watchFamilyUser 家人腕表配置
     * @return 家人腕表配置
     */
    @Override
    public List<WatchFamilyUser> selectWatchFamilyUserList(WatchFamilyUser watchFamilyUser)
    {
        return watchFamilyUserMapper.selectWatchFamilyUserList(watchFamilyUser);
    }

    /**
     * 新增家人腕表配置
     *
     * @param watchFamilyUser 家人腕表配置
     * @return 结果
     */
    @Override
    public int insertWatchFamilyUser(WatchFamilyUser watchFamilyUser)
    {
        watchFamilyUser.setCreateTime(DateUtils.getNowDate());
        return watchFamilyUserMapper.insertWatchFamilyUser(watchFamilyUser);
    }

    /**
     * 修改家人腕表配置
     *
     * @param watchFamilyUser 家人腕表配置
     * @return 结果
     */
    @Override
    public int updateWatchFamilyUser(WatchFamilyUser watchFamilyUser)
    {
        watchFamilyUser.setUpdateTime(DateUtils.getNowDate());
        return watchFamilyUserMapper.updateWatchFamilyUser(watchFamilyUser);
    }

    /**
     * 批量删除家人腕表配置
     *
     * @param ids 需要删除的家人腕表配置主键
     * @return 结果
     */
    @Override
    public int deleteWatchFamilyUserByIds(Long[] ids)
    {
        return watchFamilyUserMapper.deleteWatchFamilyUserByIds(ids);
    }

    /**
     * 删除家人腕表配置信息
     *
     * @param id 家人腕表配置主键
     * @return 结果
     */
    @Override
    public int deleteWatchFamilyUserById(Long id)
    {
        return watchFamilyUserMapper.deleteWatchFamilyUserById(id);
    }

    @Override
    public WatchFamilyUserVo selectWatchFamilyUserByDeviceId(String deviceId,Long watchUserId) {
        WatchFamilyUser familyUser = watchFamilyUserMapper.selectWatchFamilyUserByDeviceId(deviceId);
        if (familyUser == null){
            return null;
        }
        WatchFamilyUserVo watchFamilyUserVo = new WatchFamilyUserVo();
        BeanUtils.copyProperties(familyUser, watchFamilyUserVo);
        if (watchUserId != null) {
            WatchUser watchUser = watchUserMapper.selectWatchUserById(watchUserId);
            if (watchUser != null) {
                String otherDevice = watchUser.getOtherDevice();
                List<DeviceFamily> deviceFamilies = JSON.parseArray(otherDevice, DeviceFamily.class);
                for (DeviceFamily deviceFamily : deviceFamilies) {
                    if (deviceId.equals(deviceFamily.getDeviceId())) {
                        watchFamilyUserVo.setNickName(deviceFamily.getName());
                        break;
                    }
                }
            }
        }

        return watchFamilyUserVo;
    }

    @Override
    @Transactional
    public int updateWatchFamilyUserByDeviceId(WatchFamilyUserVo watchFamilyUser,Long watchUserId) {
        if (StringUtils.isNotBlank(watchFamilyUser.getNickName())){
            //修改昵称
            WatchUser watchUser = watchUserMapper.selectWatchUserById(watchUserId);
            if (watchUser != null) {
                String otherDevice = watchUser.getOtherDevice();
                List<DeviceFamily> deviceFamilies = JSON.parseArray(otherDevice, DeviceFamily.class);
                for (DeviceFamily deviceFamily : deviceFamilies) {
                    if (watchFamilyUser.getDeviceId().equals(deviceFamily.getDeviceId())) {
                        deviceFamily.setName(watchFamilyUser.getNickName());
                        break;
                    }
                }
                watchUser.setOtherDevice(JSON.toJSONString(deviceFamilies));
                watchUserMapper.updateWatchUser(watchUser);
            }

        }
        watchFamilyUser.setUpdateTime(DateUtils.getNowDate());
        return watchFamilyUserMapper.updateWatchFamilyUserByDeviceId(watchFamilyUser);
    }
}
