package com.ylx.point.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.utils.DateUtils;
import com.ylx.common.utils.SecurityUtils;
import com.ylx.point.domain.PointSignTask;
import com.ylx.point.domain.vo.PointSignTaskVo;
import com.ylx.point.mapper.PointSignTaskMapper;
import com.ylx.point.service.IPointSignTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分签到任务规则Service业务层处理
 *
 * @author wzj
 * @date 2026-03-25
 */
@Service
public class PointSignTaskServiceImpl extends ServiceImpl<PointSignTaskMapper, PointSignTask> implements IPointSignTaskService {
    @Autowired
    private PointSignTaskMapper pointSignTaskMapper;

    /**
     * 新增积分签到任务规则
     *
     * @param pointSignTask 积分签到任务规则
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPointSignTask(PointSignTask pointSignTask) {
        pointSignTask.setCreateTime(DateUtils.getNowDate());
        pointSignTask.setCreateBy(SecurityUtils.getUsername());
        pointSignTask.setUpdateTime(pointSignTask.getCreateTime());
        pointSignTask.setIsDeleted(0);
        return pointSignTaskMapper.insertPointSignTask(pointSignTask);
    }

    @Override
    public PointSignTaskVo selectPointSignTaskByActivityId(Long id) {
        PointSignTaskVo vo = pointSignTaskMapper.getTaskWithRewards(id);
        return ObjectUtil.isNull(vo) ? new PointSignTaskVo() : vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePointSignTask(PointSignTask pointSignTask) {
        pointSignTask.setUpdateTime(DateUtils.getNowDate());
        pointSignTask.setUpdateBy(SecurityUtils.getUsername());
        return pointSignTaskMapper.updatePointSignTask(pointSignTask);

    }
}
