package com.fs.his.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.his.param.FsProjectRedDetailStatisticsParam;
import com.fs.his.param.ProjectRedParam;
import com.fs.his.vo.FsProjectRedDetailStatisticsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsProjectRedDetailMapper;
import com.fs.his.domain.FsProjectRedDetail;
import com.fs.his.service.IFsProjectRedDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 理疗浏览记录Service业务层处理
 * 
 * @author fs
 * @date 2026-04-24
 */
@Service
public class FsProjectRedDetailServiceImpl extends ServiceImpl<FsProjectRedDetailMapper, FsProjectRedDetail> implements IFsProjectRedDetailService {


    @Autowired
    private FsProjectRedDetailMapper fsProjectRedDetailMapper;

    @Autowired
    private RedisCache redisCache;
    /**
     * 查询理疗浏览记录
     * 
     * @param id 理疗浏览记录主键
     * @return 理疗浏览记录
     */
    @Override
    public FsProjectRedDetail selectFsProjectRedDetailById(Long id)
    {
        return baseMapper.selectFsProjectRedDetailById(id);
    }

    /**
     * 查询理疗浏览记录列表
     * 
     * @param fsProjectRedDetail 理疗浏览记录
     * @return 理疗浏览记录
     */
    @Override
    public List<FsProjectRedDetail> selectFsProjectRedDetailList(FsProjectRedDetail fsProjectRedDetail)
    {
        return baseMapper.selectFsProjectRedDetailList(fsProjectRedDetail);
    }

    /**
     * 新增理疗浏览记录
     * 
     * @param fsProjectRedDetail 理疗浏览记录
     * @return 结果
     */
    @Override
    public int insertFsProjectRedDetail(FsProjectRedDetail fsProjectRedDetail)
    {
        fsProjectRedDetail.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsProjectRedDetail(fsProjectRedDetail);
    }

    /**
     * 修改理疗浏览记录
     * 
     * @param fsProjectRedDetail 理疗浏览记录
     * @return 结果
     */
    @Override
    public int updateFsProjectRedDetail(FsProjectRedDetail fsProjectRedDetail)
    {
        fsProjectRedDetail.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsProjectRedDetail(fsProjectRedDetail);
    }

    /**
     * 批量删除理疗浏览记录
     * 
     * @param ids 需要删除的理疗浏览记录主键
     * @return 结果
     */
    @Override
    public int deleteFsProjectRedDetailByIds(Long[] ids)
    {
        return baseMapper.deleteFsProjectRedDetailByIds(ids);
    }

    /**
     * 删除理疗浏览记录信息
     * 
     * @param id 理疗浏览记录主键
     * @return 结果
     */
    @Override
    public int deleteFsProjectRedDetailById(Long id)
    {
        return baseMapper.deleteFsProjectRedDetailById(id);
    }

//    @Override
//    public R userRedProject(ProjectRedParam param) {
//        if (param.getProjectId() == null && param.getUserId() == null && param.getCompanyUserId() == null) {
//            return R.error("参数错误");
//        }
//        FsProjectRedDetail detail = fsProjectRedDetailMapper.selectFsProjectRedDetailDataByCompanyUserIdAndProjectId(param);
//        if (detail == null) {
//            detail = new FsProjectRedDetail();
//            detail.setProjectId(param.getProjectId());
//            detail.setUserId(param.getUserId());
//            detail.setCompanyUserId(param.getCompanyUserId());
//            detail.setCreateTime(new Date());
//            detail.setSource("小程序");
//            detail.setReadDuration(param.getReadDuration());
//            int i = fsProjectRedDetailMapper.insertFsProjectRedDetail(detail);
//            if (i > 0) {
//                return R.ok();
//            }
//        } else {
//            FsProjectRedDetail map = new FsProjectRedDetail();
//            map.setId(detail.getId());
//            map.setReadDuration(detail.getReadDuration() + param.getReadDuration());
//            map.setUpdateTime(new Date());
//            int i = fsProjectRedDetailMapper.updateFsProjectRedDetail(map);
//            if (i > 0) {
//                return R.ok();
//            }
//        }
//        return R.error();
//    }
@Override
@Transactional
public R userRedProject(ProjectRedParam param) {
    if (param.getProjectId() == null && param.getUserId() == null && param.getCompanyUserId() == null) {
        return R.error("参数错误");
    }
    FsProjectRedDetail detail = fsProjectRedDetailMapper.selectFsProjectRedDetailDataByCompanyUserIdAndProjectId(param);
    if (detail == null) {
        if (!StringUtils.isEmpty(param.getBaseInfoKey())) {
            FsProjectRedDetail redDetail = redisCache.getCacheObject(param.getBaseInfoKey());
            //存在销售扫码消耗了缓存数据 重新生成用户的阅读记录
            if (redDetail == null) {
                redDetail = new FsProjectRedDetail();
                redDetail.setProjectId(param.getProjectId());
                redDetail.setUserId(param.getUserId());
                redDetail.setCompanyUserId(param.getCompanyUserId());
                redDetail.setCreateTime(DateUtils.getNowDate());
                redDetail.setUpdateTime(DateUtils.getNowDate());
                redDetail.setSendTime(new Date());
            }
            redDetail.setUserId(param.getUserId());
            redDetail.setReadDuration(param.getReadDuration());
            redDetail.setStatus(1);
            redDetail.setUpdateTime(DateUtils.getNowDate());
            redDetail.setSource("小程序");
            int i = fsProjectRedDetailMapper.insertFsProjectRedDetail(redDetail);
            if (i > 0) {
                //删除缓存
                redisCache.deleteObject(param.getBaseInfoKey());
                return R.ok();
            }
        } else {
            return R.error("联系客服重新分享");
        }
    } else {
        FsProjectRedDetail map = new FsProjectRedDetail();
        map.setId(detail.getId());
        map.setReadDuration(detail.getReadDuration() + param.getReadDuration());
        map.setUpdateTime(new Date());
        map.setStatus(1);
        int i = fsProjectRedDetailMapper.updateFsProjectRedDetail(map);
        if (i > 0) {
            return R.ok();
        }
    }
    return R.error();
}

    @Override
    public List<FsProjectRedDetailStatisticsVO> selectDetailStatisticsVOList(FsProjectRedDetailStatisticsParam param) {
        return fsProjectRedDetailMapper.selectDetailStatisticsVOList(param);
    }
}
