package com.fs.hisStore.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.hisStore.domain.FsUserScrm;
import com.fs.hisStore.domain.FsUserIntegralLogsScrm;
import com.fs.hisStore.mapper.FsUserIntegralLogsScrmMapper;
import com.fs.hisStore.mapper.FsUserScrmMapper;
import com.fs.hisStore.param.FsUserIntegralLogsListUParam;
import com.fs.hisStore.param.FsUserIntegralLogsParam;
import com.fs.hisStore.service.IFsUserIntegralLogsScrmService;
import com.fs.hisStore.vo.FsUserIntegralLogsListUVO;
import com.fs.hisStore.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 FsUserIntegralLogsScrmServiceImpl implements IFsUserIntegralLogsScrmService
{
    @Autowired
    private FsUserIntegralLogsScrmMapper fsUserIntegralLogsMapper;
    @Autowired
    FsUserScrmMapper fsUserMapper;
    /**
     * 查询积分记录
     *
     * @param id 积分记录主键
     * @return 积分记录
     */
    @Override
    public FsUserIntegralLogsScrm selectFsUserIntegralLogsById(Long id)
    {
        return fsUserIntegralLogsMapper.selectFsUserIntegralLogsById(id);
    }

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

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

    /**
     * 修改积分记录
     *
     * @param fsUserIntegralLogs 积分记录
     * @return 结果
     */
    @Override
    public int updateFsUserIntegralLogs(FsUserIntegralLogsScrm 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) {

            FsUserScrm fsUser = fsUserMapper.selectFsUserByUserId(id);
            if(fsUser != null &&fsUser.getIntegral()== 0){
                List<FsUserIntegralLogsScrm> log =  fsUserIntegralLogsMapper.selectFsUserIntegralByStatus0(id);
                for (FsUserIntegralLogsScrm fsUserIntegralLogs : log) {
                    Long integral = fsUser.getIntegral();
                    BigDecimal integralLog = fsUserIntegralLogs.getIntegral();
                    Long balance = integral-integralLog.longValue();
                    if (balance< 0) {
                        balance = 0L;
                    }
                    fsUser.setIntegral(balance);
                    fsUserMapper.updateFsUser(fsUser);
                    fsUserIntegralLogs.setStatus(1);
                    fsUserIntegralLogsMapper.updateFsUserIntegralLogs(fsUserIntegralLogs);
                    FsUserIntegralLogsScrm logs = new FsUserIntegralLogsScrm();
                    logs.setIntegral(integralLog.negate());
                    logs.setUserId(id);
                    logs.setBalance(BigDecimal.valueOf(balance));
                    logs.setLogType(7);
                    logs.setStatus(1);
                    logs.setCreateTime(new Date());
                    fsUserIntegralLogsMapper.insertFsUserIntegralLogs(logs);
                }


            }

        }
    }
}
