package com.ruoyi.bst.lightingNum.service.impl;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.changeRecord.domain.ChangeRecord;
import com.ruoyi.bst.changeRecord.domain.enums.ChangeRecordBstType;
import com.ruoyi.bst.changeRecord.mapper.ChangeRecordMapper;
import com.ruoyi.bst.changeRecord.service.ChangeRecordService;
import com.ruoyi.bst.lightingNum.domain.dto.GiftLightingNumDTO;
import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.store.domain.StoreVO;
import com.ruoyi.bst.store.service.StoreService;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.domain.vo.LongIntegerVO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.system.user.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bst.lightingNum.mapper.LightingNumMapper;
import com.ruoyi.bst.lightingNum.domain.LightingNum;
import com.ruoyi.bst.lightingNum.domain.LightingNumVO;
import com.ruoyi.bst.lightingNum.domain.LightingNumQuery;
import com.ruoyi.bst.lightingNum.service.LightingNumService;

/**
 * 爆灯次数列表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-26
 */
@Service
public class LightingNumServiceImpl implements LightingNumService
{
    @Autowired
    private LightingNumMapper lightingNumMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private LightingNumService lightingNumService;
    @Autowired
    private ChangeRecordService changeRecordService;
    @Autowired
    private ChangeRecordMapper changeRecordMapper;

    /**
     * 查询爆灯次数列表
     *
     * @param id 爆灯次数列表主键
     * @return 爆灯次数列表
     */
    @Override
    public LightingNumVO selectLightingNumById(Long id, boolean scope)
    {
        if (id == null) {
            return null;
        }
        LightingNumQuery query = new LightingNumQuery();
        query.setId(id);
        query.setScope(scope);
        return this.selectOne(query);
    }

    /**
     * 查询一个
     * @param query
     * @return
     */
    @Override
    public LightingNumVO selectOne(LightingNumQuery query) {
        PageHelper.startPage(1, 1);
        List<LightingNumVO> list = lightingNumMapper.selectLightingNumList(query);
        if(list.isEmpty()) {
            ServiceUtil.assertion(true,"您在当前店铺无爆灯次数");
        }
        return CollectionUtils.firstElement(list);
    }

    /**
     * 查询爆灯次数列表列表
     *
     * @param lightingNum 爆灯次数列表
     * @return 爆灯次数列表
     */
    @Override
    public List<LightingNumVO> selectLightingNumList(LightingNumQuery lightingNum)
    {
        return lightingNumMapper.selectLightingNumList(lightingNum);
    }

    /**
     * 新增爆灯次数列表
     *
     * @param lightingNum 爆灯次数列表
     * @return 结果
     */
    @Override
    public int insertLightingNum(LightingNum lightingNum)
    {
        lightingNum.setCreateTime(DateUtils.getNowDate());
        return lightingNumMapper.insertLightingNum(lightingNum);
    }

    /**
     * 修改爆灯次数列表
     *
     * @param lightingNum 爆灯次数列表
     * @return 结果
     */
    @Override
    public int updateLightingNum(LightingNum lightingNum)
    {
        return lightingNumMapper.updateLightingNum(lightingNum);
    }

    /**
     * 批量删除爆灯次数列表
     *
     * @param ids 需要删除的爆灯次数列表主键
     * @return 结果
     */
    @Override
    public int deleteLightingNumByIds(Long[] ids)
    {
        return lightingNumMapper.deleteLightingNumByIds(ids);
    }

    /**
     * 删除爆灯次数列表信息
     *
     * @param id 爆灯次数列表主键
     * @return 结果
     */
    @Override
    public int deleteLightingNumById(Long id)
    {
        return lightingNumMapper.deleteLightingNumById(id);
    }

    @Override
    public int reduceLightingNumByQuery( LightingNum data,  OrderQuery query) {
        return lightingNumMapper.reduceLightingNum(data,query);
    }

    @Override
    public int giftLightingNum(GiftLightingNumDTO dto) {
        Long storeId = dto.getStoreId();
        Integer number = dto.getNumber();

        // 校验店铺是否存在
        StoreVO store = storeService.selectStoreById(storeId);
        ServiceUtil.assertion(store == null,"当前店铺不存在");

        UserVO user = null;
        // 校验用户是否存在
        if (dto.getPhone() != null) {
            user = userService.selectUserByPhonenumber(dto.getPhone());
            ServiceUtil.assertion(user == null,"当前用户不存在");
        }else {
            user = userService.selectUserById(dto.getUserId());
        }

        // 增加用户爆灯次数并生成记录
        return userService.addLightingNum( user.getUserId(), storeId, number,ChangeRecordBstType.GIFT.getCode(),storeId,"商户赠送爆灯次数"+number);
    }

    @Override
    public List<LongIntegerVO> assembleLightingNum(LightingNumQuery query) {
        return lightingNumMapper.assembleLightingNum(query);
    }
}
