/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.sing.active.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.jsonwebtoken.lang.Assert;
import org.springblade.sing.active.entity.ActiveHelpRecord;
import org.springblade.sing.active.entity.ActiveProductRecord;
import org.springblade.sing.active.entity.ProductVote;
import org.springblade.sing.active.service.IActiveProductRecordService;
import org.springblade.sing.active.vo.ActiveHelpRecordVO;
import org.springblade.sing.active.mapper.ActiveHelpRecordMapper;
import org.springblade.sing.active.service.IActiveHelpRecordService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.sing.active.vo.ActiveHelpVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 活动助力记录 服务实现类
 *
 * @author BladeX
 * @since 2021-11-09
 */
@Service
public class ActiveHelpRecordServiceImpl extends BaseServiceImpl<ActiveHelpRecordMapper, ActiveHelpRecord> implements IActiveHelpRecordService {

	@Autowired
	private IActiveProductRecordService activeProductRecordService;

	@Override
	public IPage<ActiveHelpRecordVO> selectActiveHelpRecordPage(IPage<ActiveHelpRecordVO> page, ActiveHelpRecordVO activeHelpRecord) {
		return page.setRecords(baseMapper.selectActiveHelpRecordPage(page, activeHelpRecord));
	}

	@Override
	public List<ActiveHelpRecordVO> selectActiveHelpRecordPage(ActiveHelpRecordVO activeHelpRecord) {
		return baseMapper.selectActiveHelpRecordPage(activeHelpRecord);
	}

	@Override
    public List<ProductVote> productVote(Long productId) {
		return baseMapper.selectVote(productId);
    }

    @Override
	@Transactional
    public boolean recordHide(List<Long> recordIds) {
		Assert.notNull(recordIds,"请选择助力记录");
		List<ActiveHelpRecord> activeHelpRecords = super.listByIds(recordIds);
		Assert.notNull(activeHelpRecords,"此助力记录已失效");

		statusHide(activeHelpRecords);
		return super.updateBatchById(activeHelpRecords);
	}

	@Override
	@Transactional
	public boolean recordShow(List<Long> recordIds) {
		Assert.notNull(recordIds,"请选择助力记录");
		List<ActiveHelpRecord> activeHelpRecords = super.listByIds(recordIds);
		Assert.notNull(activeHelpRecords,"此助力记录已失效");

		statusShow(activeHelpRecords);
		return super.updateBatchById(activeHelpRecords);
	}

	@Override
	@Transactional
	public boolean recordHideByIp(List<String> ips) {
		List<ActiveHelpRecord> activeHelpRecords = new ArrayList<>();
		for (String ip : ips) {
			List<ActiveHelpRecord> list = super.list(new LambdaQueryWrapper<ActiveHelpRecord>().eq(ActiveHelpRecord::getIp, ip));
			statusHide(list);
			activeHelpRecords.addAll(list);
		}

		return super.updateBatchById(activeHelpRecords);
	}

	@Override
	@Transactional
	public boolean recordShowByIp(List<String> ips) {
		List<ActiveHelpRecord> activeHelpRecords = new ArrayList<>();
		for (String ip : ips) {
			List<ActiveHelpRecord> list = super.list(new LambdaQueryWrapper<ActiveHelpRecord>().eq(ActiveHelpRecord::getIp, ip));
			statusShow(list);
			activeHelpRecords.addAll(list);
		}
		return super.updateBatchById(activeHelpRecords);
	}

	//恢复票数
	private void statusShow(List<ActiveHelpRecord> list) {
		for (ActiveHelpRecord activeHelpRecord : list) {
			if (activeHelpRecord.getStatus() == 0){
				activeHelpRecord.setStatus(1);
				//更新作品票数
				ActiveProductRecord productRecord = activeProductRecordService.getById(activeHelpRecord.getProductId());
				productRecord.setVoteCount(productRecord.getVoteCount() + activeHelpRecord.getVoteCount());
				activeProductRecordService.updateById(productRecord);
			}
		}
	}

	//减去票数
	private void statusHide(List<ActiveHelpRecord> list) {
		for (ActiveHelpRecord activeHelpRecord : list) {
			if (activeHelpRecord.getStatus() == 1){
				activeHelpRecord.setStatus(0);
				//更新作品票数
				ActiveProductRecord productRecord = activeProductRecordService.getById(activeHelpRecord.getProductId());
				productRecord.setVoteCount(productRecord.getVoteCount() - activeHelpRecord.getVoteCount());
				activeProductRecordService.updateById(productRecord);
			}
		}
	}


}
