package com.fs.store.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.store.domain.FsUser;
import com.fs.store.domain.FsUserIntegralLogs;
import com.fs.store.mapper.FsUserIntegralLogsMapper;
import com.fs.store.mapper.FsUserMapper;
import com.fs.store.param.FsUserIntegralLogsListUParam;
import com.fs.store.param.FsUserIntegralLogsParam;
import com.fs.store.service.IFsUserIntegralLogsService;
import com.fs.store.vo.FsUserIntegralLogsListUVO;
import com.fs.store.vo.FsUserIntegralLogsListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 积分记录Service业务层处理
 *
 * @author fs
 * @date 2023-11-02
 */
@Service
public class FsUserIntegralLogsServiceImpl implements IFsUserIntegralLogsService
{
    @Autowired
    private FsUserIntegralLogsMapper fsUserIntegralLogsMapper;
    @Autowired
    FsUserMapper fsUserMapper;
    /**
     * 查询积分记录
     *
     * @param id 积分记录主键
     * @return 积分记录
     */
    @Override
    public FsUserIntegralLogs selectFsUserIntegralLogsById(Long id)
    {
        return fsUserIntegralLogsMapper.selectFsUserIntegralLogsById(id);
    }

    /**
     * 查询积分记录列表
     *
     * @param fsUserIntegralLogs 积分记录
     * @return 积分记录
     */
    @Override
    public List<FsUserIntegralLogs> selectFsUserIntegralLogsList(FsUserIntegralLogs fsUserIntegralLogs)
    {
        return fsUserIntegralLogsMapper.selectFsUserIntegralLogsList(fsUserIntegralLogs);
    }

    /**
     * 新增积分记录
     *
     * @param fsUserIntegralLogs 积分记录
     * @return 结果
     */
    @Override
    public int insertFsUserIntegralLogs(FsUserIntegralLogs fsUserIntegralLogs)
    {
        fsUserIntegralLogs.setCreateTime(DateUtils.getNowDate());
        return fsUserIntegralLogsMapper.insertFsUserIntegralLogs(fsUserIntegralLogs);
    }

    /**
     * 修改积分记录
     *
     * @param fsUserIntegralLogs 积分记录
     * @return 结果
     */
    @Override
    public int updateFsUserIntegralLogs(FsUserIntegralLogs fsUserIntegralLogs)
    {
        return fsUserIntegralLogsMapper.updateFsUserIntegralLogs(fsUserIntegralLogs);
    }

    /**
     * 批量删除积分记录
     *
     * @param ids 需要删除的积分记录主键
     * @return 结果
     */
    @Override
    public int deleteFsUserIntegralLogsByIds(Long[] ids)
    {
        return fsUserIntegralLogsMapper.deleteFsUserIntegralLogsByIds(ids);
    }

    /**
     * 删除积分记录信息
     *
     * @param id 积分记录主键
     * @return 结果
     */
    @Override
    public int deleteFsUserIntegralLogsById(Long id)
    {
        return fsUserIntegralLogsMapper.deleteFsUserIntegralLogsById(id);
    }

    @Override
    public List<FsUserIntegralLogsListVO> selectFsUserIntegralLogsListVO(FsUserIntegralLogsParam fsUserIntegralLogs) {
        return fsUserIntegralLogsMapper.selectFsUserIntegralLogsListVO(fsUserIntegralLogs);
    }

    @Override
    public List<FsUserIntegralLogsListUVO> selectFsUserIntegralLogsListUVO(FsUserIntegralLogsListUParam param) {
        return fsUserIntegralLogsMapper.selectFsUserIntegralLogsListUVO(param);
    }

    @Override
    public void subFsUserIntegralLogsByOrder5() {
        List<Long> longs = fsUserIntegralLogsMapper.subFsUserIntegralLogsByOrder5();
        for (Long id : longs) {

            FsUser fsUser = fsUserMapper.selectFsUserByUserId(id);
            if(fsUser != null &&fsUser.getIntegral().compareTo(BigDecimal.ZERO) == 0){
                List<FsUserIntegralLogs> log =  fsUserIntegralLogsMapper.selectFsUserIntegralByStatus0(id);
                for (FsUserIntegralLogs fsUserIntegralLogs : log) {
                    BigDecimal integral = fsUser.getIntegral();
                    BigDecimal integralLog = fsUserIntegralLogs.getIntegral();
                    BigDecimal balance = integral.subtract(integralLog);
                    if (balance.compareTo(BigDecimal.ZERO) < 0) {
                        balance = BigDecimal.ZERO;
                    }
                    fsUser.setIntegral(balance);
                    fsUserMapper.updateFsUser(fsUser);
                    fsUserIntegralLogs.setStatus(1);
                    fsUserIntegralLogsMapper.updateFsUserIntegralLogs(fsUserIntegralLogs);
                    FsUserIntegralLogs logs = new FsUserIntegralLogs();
                    logs.setIntegral(integralLog.negate());
                    logs.setUserId(id);
                    logs.setBalance(balance);
                    logs.setLogType(7);
                    logs.setStatus(1);
                    logs.setCreateTime(new Date());
                    fsUserIntegralLogsMapper.insertFsUserIntegralLogs(logs);
                }


            }

        }
    }
}
