package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ClewMaterialMapper;
import com.ruoyi.system.domain.ClewMaterial;
import com.ruoyi.system.service.IClewMaterialService;
import com.ruoyi.common.core.text.Convert;

/**
 * 素材Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-07
 */
@Service
public class ClewMaterialServiceImpl implements IClewMaterialService
{
    @Autowired
    private ClewMaterialMapper clewMaterialMapper;

    /**
     * 查询素材
     *
     * @param id 素材主键
     * @return 素材
     */
    @Override
    public ClewMaterial selectClewMaterialById(Long id)
    {
        return clewMaterialMapper.selectClewMaterialById(id);
    }

    /**
     * 查询素材列表
     *
     * @param clewMaterial 素材
     * @return 素材
     */
    @Override
    public List<ClewMaterial> selectClewMaterialList(ClewMaterial clewMaterial)
    {
        return clewMaterialMapper.selectClewMaterialList(clewMaterial);
    }

    /**
     * 新增素材
     *
     * @param clewMaterial 素材
     * @return 结果
     */
    @Override
    public int insertClewMaterial(ClewMaterial clewMaterial)
    {
        clewMaterial.setCreateTime(DateUtils.getNowDate());
        return clewMaterialMapper.insertClewMaterial(clewMaterial);
    }

    /**
     * 修改素材
     *
     * @param clewMaterial 素材
     * @return 结果
     */
    @Override
    public int updateClewMaterial(ClewMaterial clewMaterial)
    {
        clewMaterial.setUpdateTime(DateUtils.getNowDate());
        return clewMaterialMapper.updateClewMaterial(clewMaterial);
    }

    /**
     * 批量删除素材
     *
     * @param ids 需要删除的素材主键
     * @return 结果
     */
    @Override
    public int deleteClewMaterialByIds(String ids)
    {
        return clewMaterialMapper.deleteClewMaterialByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除素材信息
     *
     * @param id 素材主键
     * @return 结果
     */
    @Override
    public int deleteClewMaterialById(Long id)
    {
        return clewMaterialMapper.deleteClewMaterialById(id);
    }

    /**
     * 增加素材点击次数
     *
     * @param id 素材主键
     * @return 结果
     */
    @Override
    public int incrementClickCount(Long id)
    {
        System.out.println("服务层 - 增加素材点击次数，ID: " + id);
        try {
            // 先检查素材是否存在
            ClewMaterial material = clewMaterialMapper.selectClewMaterialById(id);
            if (material == null) {
                System.out.println("服务层 - 素材不存在，ID: " + id);
                return 0;
            }
            
            int result = clewMaterialMapper.incrementClickCount(id);
            System.out.println("服务层 - 更新结果: " + result);
            
            // 更新后查询确认
            if (result > 0) {
                ClewMaterial updatedMaterial = clewMaterialMapper.selectClewMaterialById(id);
                System.out.println("服务层 - 更新后点击次数: " + (updatedMaterial != null ? updatedMaterial.getClickCount() : "null"));
            }
            
            return result;
        } catch (Exception e) {
            System.out.println("服务层 - 更新点击次数异常: " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 增加素材提交次数
     *
     * @param id 素材主键
     * @return 结果
     */
    @Override
    public int incrementSubmitCount(Long id)
    {
        System.out.println("服务层 - 增加素材提交次数，ID: " + id);
        try {
            // 先检查素材是否存在
            ClewMaterial material = clewMaterialMapper.selectClewMaterialById(id);
            if (material == null) {
                System.out.println("服务层 - 素材不存在，ID: " + id);
                return 0;
            }
            
            int result = clewMaterialMapper.incrementSubmitCount(id);
            System.out.println("服务层 - 提交次数更新结果: " + result);
            
            // 更新后查询确认
            if (result > 0) {
                ClewMaterial updatedMaterial = clewMaterialMapper.selectClewMaterialById(id);
                System.out.println("服务层 - 更新后提交次数: " + (updatedMaterial != null ? updatedMaterial.getSubmitCount() : "null"));
                
                // 更新有效率
                int efficiencyResult = clewMaterialMapper.updateEfficiencyRate(id);
                System.out.println("服务层 - 有效率更新结果: " + efficiencyResult);
            }
            
            return result;
        } catch (Exception e) {
            System.out.println("服务层 - 更新提交次数异常: " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 更新素材有效率
     *
     * @param id 素材主键
     * @return 结果
     */
    @Override
    public int updateEfficiencyRate(Long id)
    {
        System.out.println("服务层 - 更新素材有效率，ID: " + id);
        try {
            int result = clewMaterialMapper.updateEfficiencyRate(id);
            System.out.println("服务层 - 有效率更新结果: " + result);
            
            if (result > 0) {
                ClewMaterial updatedMaterial = clewMaterialMapper.selectClewMaterialById(id);
                System.out.println("服务层 - 更新后有效率: " + (updatedMaterial != null ? updatedMaterial.getEfficiencyRate() : "null"));
            }
            
            return result;
        } catch (Exception e) {
            System.out.println("服务层 - 更新有效率异常: " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
    }
}
