package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbMemberScore;
import com.ruoyi.hezhi.domain.TbMemberSign;
import com.ruoyi.hezhi.domain.dto.MemberSignDTO;
import com.ruoyi.hezhi.mapper.TbMemberScoreMapper;
import com.ruoyi.hezhi.mapper.TbMemberSignMapper;
import com.ruoyi.hezhi.service.ITbMemberSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户签到Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-31
 */
@Service
public class TbMemberSignServiceImpl implements ITbMemberSignService
{
    @Autowired
    private TbMemberSignMapper tbMemberSignMapper;
    @Autowired
    private TbMemberScoreMapper tbMemberScoreMapper;

    /**
     * 查询用户签到
     *
     * @param memberSignId 用户签到主键
     * @return 用户签到
     */
    @Override
    public TbMemberSign selectTbMemberSignByMemberSignId(Long memberSignId)
    {
        return tbMemberSignMapper.selectTbMemberSignByMemberSignId(memberSignId);
    }

    /**
     * 查询用户签到列表
     *
     * @param tbMemberSign 用户签到
     * @return 用户签到
     */
    @Override
    public List<TbMemberSign> selectTbMemberSignList(TbMemberSign tbMemberSign)
    {
        return tbMemberSignMapper.selectTbMemberSignList(tbMemberSign);
    }

    /**
     * 新增用户签到
     *
     * @param tbMemberSign 用户签到
     * @return 结果
     */
    @Override
    public int insertTbMemberSign(TbMemberSign tbMemberSign)
    {
        tbMemberSign.setCreateTime(DateUtils.getNowDate());
        return tbMemberSignMapper.insertTbMemberSign(tbMemberSign);
    }

    /**
     * 修改用户签到
     *
     * @param tbMemberSign 用户签到
     * @return 结果
     */
    @Override
    public int updateTbMemberSign(TbMemberSign tbMemberSign)
    {
        tbMemberSign.setUpdateTime(DateUtils.getNowDate());
        return tbMemberSignMapper.updateTbMemberSign(tbMemberSign);
    }

    /**
     * 批量删除用户签到
     *
     * @param memberSignIds 需要删除的用户签到主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberSignByMemberSignIds(Long[] memberSignIds)
    {
        return tbMemberSignMapper.deleteTbMemberSignByMemberSignIds(memberSignIds);
    }

    /**
     * 删除用户签到信息
     *
     * @param memberSignId 用户签到主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberSignByMemberSignId(Long memberSignId)
    {
        return tbMemberSignMapper.deleteTbMemberSignByMemberSignId(memberSignId);
    }

    /**
     * 用户签到弹窗
     *
     * @param params 参数
     * @return 用户签到弹窗
     */
    @Override
    public Map<String, Object> getMemberSignPopup(Map<String, Object> params) {
        return tbMemberSignMapper.getMemberSignPopup(params);
    }

    /**
     * 用户签到
     *
     * @param memberSignDTO
     * @return ok
     */
    @Override
    public AjaxResult memberSignDay(MemberSignDTO memberSignDTO) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("memberUserId", memberSignDTO.getMemberUserId());
        Map<String, Object> memberSignPopup = tbMemberSignMapper.getMemberSignPopup(params);
        if (memberSignPopup.isEmpty() || "1".equals(memberSignPopup.get("isSign").toString())) {
            return AjaxResult.error("不可签到");
        } else {
            BigDecimal signScore = new BigDecimal(memberSignPopup.get("signScore").toString());
            if (signScore.compareTo(new BigDecimal(0)) > 0) {
                // 添加签到记录
                TbMemberSign memberSign = new TbMemberSign();
                memberSign.setMemberUserId(memberSignDTO.getMemberUserId());
                memberSign.setSignScore(signScore);
                memberSign.setSignYear(LocalDate.now().getYear()+"");
                memberSign.setSignMonth(LocalDate.now().getMonthValue()+"");
                memberSign.setSignDay(LocalDate.now().getDayOfMonth()+"");
                memberSign.setSignDate(LocalDate.now().getYear() + "-" + LocalDate.now().getMonthValue() + "-" + LocalDate.now().getDayOfMonth());
                memberSign.setCreateTime(DateUtils.getNowDate());
                tbMemberSignMapper.insertTbMemberSign(memberSign);

                // 添加积分记录
                TbMemberScore memberScore = new TbMemberScore();
                memberScore.setMemberUserId(memberSignDTO.getMemberUserId());
                memberScore.setName("每日签到");
                memberScore.setScore(signScore);
                memberScore.setType(1);
                memberScore.setIncomeExpenses(1);
                memberScore.setCreateTime(DateUtils.getNowDate());
                tbMemberScoreMapper.insertTbMemberScore(memberScore);
            }

        }
        return AjaxResult.success();
    }
}
