package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import shop.alien.entity.result.R;
import shop.alien.entity.second.SecondUserCredit;
import shop.alien.entity.second.SecondUserCreditRecord;
import shop.alien.entity.store.LifeUserCreditScoreInfo;
import shop.alien.entity.store.StoreUser;
import shop.alien.mapper.LifeUserCreditScoreInfoMapper;
import shop.alien.mapper.second.SecondUserCreditMapper;
import shop.alien.mapper.second.SecondUserCreditRecordMapper;
import shop.alien.store.service.LifeUserCreditScoreInfoService;
import shop.alien.util.common.JwtUtil;

import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class LifeUserCreditScoreInfoServiceImpl extends ServiceImpl<LifeUserCreditScoreInfoMapper, LifeUserCreditScoreInfo> implements LifeUserCreditScoreInfoService {

    private final SecondUserCreditRecordMapper secondUserCreditRecordMapper;
    private final LifeUserCreditScoreInfoMapper lifeUserCreditScoreInfoMapper;
    private final SecondUserCreditMapper secondUserCreditMapper;

    @Override
    public R<String> add(LifeUserCreditScoreInfo lifeUserCreditScoreInfo) {
        log.info("LifeUserCreditScoreInfoServiceImpl.add, param={}", lifeUserCreditScoreInfo);
        Integer userId = Objects.requireNonNull(JwtUtil.getCurrentUserInfo()).getInteger("userId");

        LambdaQueryWrapper<LifeUserCreditScoreInfo> queryWrapperInfo = new LambdaQueryWrapper<>();
        queryWrapperInfo.eq(LifeUserCreditScoreInfo::getUserId, userId);
        LifeUserCreditScoreInfo existingInfo = lifeUserCreditScoreInfoMapper.selectOne(queryWrapperInfo);
        if (null == existingInfo) {
            lifeUserCreditScoreInfo.setUserId(userId);
            this.save(lifeUserCreditScoreInfo);
        } else {
            lifeUserCreditScoreInfo.setId(existingInfo.getId());
            this.updateById(lifeUserCreditScoreInfo);
        }

        LambdaQueryWrapper<SecondUserCreditRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SecondUserCreditRecord::getUserId, userId);
        queryWrapper.eq(SecondUserCreditRecord::getPointsType, 9);
        SecondUserCreditRecord existingPoints = secondUserCreditRecordMapper.selectOne(queryWrapper);
        if (null == existingPoints) {
            SecondUserCreditRecord record = new SecondUserCreditRecord();
            record.setUserId(userId);
            record.setPoints(8);



            // 更新总分
            SecondUserCreditRecord recordByUserId = new SecondUserCreditRecord();
            recordByUserId.setUserId(userId);
            QueryWrapper<SecondUserCreditRecord> queryWrapperByUserId = new QueryWrapper<>();
            queryWrapperByUserId.eq("userId", userId);
            SecondUserCredit secondUserCredit = secondUserCreditMapper.selectOne(new LambdaQueryWrapper<SecondUserCredit>().eq(SecondUserCredit::getUserId, userId));
            if (null != secondUserCredit) {
                secondUserCredit.setUserPoints(secondUserCredit.getUserPoints() + 8);
                record.setCurrentScoreCount(secondUserCredit.getUserPoints());
                secondUserCreditMapper.updateById(secondUserCredit);

                SecondUserCreditRecord recordUp = new SecondUserCreditRecord();
                recordUp.setUserId(secondUserCredit.getUserId());
                recordUp.setPoints(8);
                recordUp.setCurrentScoreCount(secondUserCredit.getUserPoints());
                recordUp.setPointsType(9);
                secondUserCreditRecordMapper.updateById(record);
            }
            record.setPointsType(9);
            secondUserCreditRecordMapper.insert(record);

        }

        return R.success("新增成功");
    }

    @Override
    public R<String> deleteById(Integer id) {
        log.info("LifeUserCreditScoreInfoServiceImpl.deleteById, id={}", id);
        boolean result = this.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @Override
    public R<String> update(LifeUserCreditScoreInfo lifeUserCreditScoreInfo) {
        log.info("LifeUserCreditScoreInfoServiceImpl.update, param={}", lifeUserCreditScoreInfo);
        boolean result = this.updateById(lifeUserCreditScoreInfo);
        if (result) {
            return R.success("更新成功");
        }
        return R.fail("更新失败");
    }

    @Override
    public R<LifeUserCreditScoreInfo> getInfoById(Integer id) {
        log.info("LifeUserCreditScoreInfoServiceImpl.getInfoById, id={}", id);
        LifeUserCreditScoreInfo result = this.getById(id);
        return R.data(result);
    }

    @Override
    public R<LifeUserCreditScoreInfo> getByUserId(Integer userId) {
        log.info("LifeUserCreditScoreInfoServiceImpl.getByUserId, userId={}", userId);
        LambdaQueryWrapper<LifeUserCreditScoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LifeUserCreditScoreInfo::getUserId, userId);
        LifeUserCreditScoreInfo result = this.getOne(queryWrapper);
        return R.data(result);
    }

    @Override
    public R<IPage<LifeUserCreditScoreInfo>> list(Integer pageNum, Integer pageSize, Integer userId) {
        log.info("LifeUserCreditScoreInfoServiceImpl.list, pageNum={}, pageSize={}, userId={}", pageNum, pageSize, userId);
        Page<LifeUserCreditScoreInfo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LifeUserCreditScoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq(LifeUserCreditScoreInfo::getUserId, userId);
        }
        queryWrapper.orderByDesc(LifeUserCreditScoreInfo::getCreatedTime);
        IPage<LifeUserCreditScoreInfo> result = this.page(page, queryWrapper);
        return R.data(result);
    }
}
