package com.fs.his.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.his.config.CouponConfig;
import com.fs.his.config.IntegralConfig;
import com.fs.his.domain.*;
import com.fs.his.enums.FsUserIntegralLogTypeEnum;
import com.fs.his.mapper.*;
import com.fs.his.param.FsUserAddIntegralTemplateParam;
import com.fs.his.service.IFsUserIntegralLogsService;
import com.fs.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.service.IFsUserNewTaskService;
import org.springframework.transaction.annotation.Transactional;

import static com.fs.his.service.impl.FsCouponServiceImpl.genCode;

/**
 * 用户新手任务Service业务层处理
 *
 * @author fs
 * @date 2024-08-27
 */
@Service
public class FsUserNewTaskServiceImpl implements IFsUserNewTaskService
{
    @Autowired
    private FsUserNewTaskMapper fsUserNewTaskMapper;
    @Autowired
    private FsUserMapper fsUserMapper;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private FsCouponMapper fsCouponMapper;
    @Autowired
    private FsUserCouponMapper fsUserCouponMapper;
    @Autowired
    private FsUserIntegralLogsMapper fsUserIntegralLogsMapper;
    @Autowired
    private IFsUserIntegralLogsService integralLogsService;
    /**
     * 查询用户新手任务
     *
     * @param taskId 用户新手任务主键
     * @return 用户新手任务
     */
    @Override
    public FsUserNewTask selectFsUserNewTaskByTaskId(Long taskId)
    {
        return fsUserNewTaskMapper.selectFsUserNewTaskByTaskId(taskId);
    }

    /**
     * 查询用户新手任务列表
     *
     * @param fsUserNewTask 用户新手任务
     * @return 用户新手任务
     */
    @Override
    public List<FsUserNewTask> selectFsUserNewTaskList(FsUserNewTask fsUserNewTask)
    {
        return fsUserNewTaskMapper.selectFsUserNewTaskList(fsUserNewTask);
    }

    /**
     * 新增用户新手任务
     *
     * @param fsUserNewTask 用户新手任务
     * @return 结果
     */
    @Override
    public int insertFsUserNewTask(FsUserNewTask fsUserNewTask)
    {
        fsUserNewTask.setCreateTime(DateUtils.getNowDate());
        return fsUserNewTaskMapper.insertFsUserNewTask(fsUserNewTask);
    }

    /**
     * 修改用户新手任务
     *
     * @param fsUserNewTask 用户新手任务
     * @return 结果
     */
    @Override
    public int updateFsUserNewTask(FsUserNewTask fsUserNewTask)
    {
        return fsUserNewTaskMapper.updateFsUserNewTask(fsUserNewTask);
    }

    /**
     * 批量删除用户新手任务
     *
     * @param taskIds 需要删除的用户新手任务主键
     * @return 结果
     */
    @Override
    public int deleteFsUserNewTaskByTaskIds(Long[] taskIds)
    {
        return fsUserNewTaskMapper.deleteFsUserNewTaskByTaskIds(taskIds);
    }

    /**
     * 删除用户新手任务信息
     *
     * @param taskId 用户新手任务主键
     * @return 结果
     */
    @Override
    public int deleteFsUserNewTaskByTaskId(Long taskId)
    {
        return fsUserNewTaskMapper.deleteFsUserNewTaskByTaskId(taskId);
    }

    @Override
    public FsUserNewTask selectFsUserNewTaskByUserId(Long userId) {
        return fsUserNewTaskMapper.selectFsUserNewTaskByUserId(userId);
    }

    @Override
    @Transactional
    public int performTaskOne(Long userId) {
        int isFirstLogin = 0;
        FsUserNewTask newTask = fsUserNewTaskMapper.selectFsUserNewTaskByUserId(userId);
        if (newTask==null){
            newTask = new FsUserNewTask();
            newTask.setUserId(userId);
            newTask.setCreateTime(new Date());//app首次登录或注册时间
            newTask.setTaskOne(1);//直接完成第一任务
            fsUserNewTaskMapper.insertFsUserNewTask(newTask);

            // 完成手机号绑定注册
            FsUserAddIntegralTemplateParam param = new FsUserAddIntegralTemplateParam();
            param.setUserId(userId);
            param.setLogType(FsUserIntegralLogTypeEnum.TYPE_20.getValue());
            param.setBusinessId(userId.toString());
            integralLogsService.addIntegralTemplate(param);

            isFirstLogin = 1;
        }
        return isFirstLogin;
    }


    @Override
    @Transactional
    public void performTaskTwo(Long userId) {
        FsUserNewTask newTask = fsUserNewTaskMapper.selectFsUserNewTaskByUserId(userId);
        if (newTask!=null&&newTask.getTaskOne()==1&&newTask.getTaskTwo()==0){
            long currentTimeMillis = System.currentTimeMillis();
            long createTimeMillis = newTask.getCreateTime().getTime();
            long difference = currentTimeMillis - createTimeMillis;

            // 转换为天数
            long daysDifference = difference / (1000 * 60 * 60 * 24);

            if (daysDifference <= 3) {
                newTask.setTaskTwo(1);
                fsUserNewTaskMapper.updateFsUserNewTask(newTask);
                // newTask 的 createTime 是三天内
                String json =configService.selectConfigByKey("his.coupon");
                CouponConfig config = JSONUtil.toBean(json, CouponConfig.class);
                FsCoupon coupon=fsCouponMapper.selectFsCouponByCouponId(config.getUserTaskCoupon());
//                if(fsUserCouponMapper.checkReceive(userId,coupon.getCouponId())>0){
//                    return ;
//                }
                FsUserCoupon userCoupon=new FsUserCoupon();
                userCoupon.setUserId(userId);
                userCoupon.setStatus(0);
                userCoupon.setCouponId(config.getUserTaskCoupon());
                userCoupon.setCouponCode(genCode());
                userCoupon.setCreateTime(new Date());
                if(fsUserCouponMapper.insertFsUserCoupon(userCoupon)>0){
                    //更新剩余数量
                    fsCouponMapper.updateRemainCount(coupon.getCouponId());
                }
            }
        }
    }


    @Override
    @Transactional
    public void performTaskThree(Long userId,Long integral,Long orderId) {
        FsUserNewTask newTask = fsUserNewTaskMapper.selectFsUserNewTaskByUserId(userId);
        if (newTask!=null&&newTask.getTaskOne()==1&&newTask.getTaskTwo()==1&&newTask.getTaskThree()==0){
            long currentTimeMillis = System.currentTimeMillis();
            long createTimeMillis = newTask.getCreateTime().getTime();
            long difference = currentTimeMillis - createTimeMillis;

            // 转换为天数
            long daysDifference = difference / (1000 * 60 * 60 * 24);

            if (daysDifference <= 7) {
                newTask.setTaskThree(1);
                fsUserNewTaskMapper.updateFsUserNewTask(newTask);
                String json =configService.selectConfigByKey("his.integral");
                IntegralConfig config= JSONUtil.toBean(json,IntegralConfig.class);
                FsUser userMap=new FsUser();
                userMap.setUserId(userId);
                userMap.setIntegral(integral+config.getIntegralNewTask());
                fsUserMapper.updateFsUser(userMap);
                FsUserIntegralLogs integralLogs = new FsUserIntegralLogs();
                integralLogs.setIntegral(config.getIntegralNewTask().longValue());
                integralLogs.setUserId(userId);
                integralLogs.setBalance(userMap.getIntegral());
                integralLogs.setLogType(15);
                integralLogs.setBusinessId(orderId.toString());
                integralLogs.setCreateTime(new Date());
                fsUserIntegralLogsMapper.insertFsUserIntegralLogs(integralLogs);
            }
        }
    }
}
