package com.ruoyi.powerdistribution.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.powerdistribution.domain.PdmStopTarge;
import com.ruoyi.powerdistribution.mapper.PdmStopTargeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.powerdistribution.mapper.PdmScoreRankManagerMapper;
import com.ruoyi.powerdistribution.domain.PdmScoreRankManager;
import com.ruoyi.powerdistribution.service.IPdmScoreRankManagerService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 负责人积分排名Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-05
 */
@Service
public class PdmScoreRankManagerServiceImpl extends ServiceImpl<PdmScoreRankManagerMapper, PdmScoreRankManager> implements IPdmScoreRankManagerService
{
    @Autowired
    private PdmScoreRankManagerMapper pdmScoreRankManagerMapper;

    /**
     * 查询负责人积分排名
     *
     * @param id 负责人积分排名主键
     * @return 负责人积分排名
     */
    @Override
    public PdmScoreRankManager selectPdmScoreRankManagerById(Long id)
    {
        return pdmScoreRankManagerMapper.selectPdmScoreRankManagerById(id);
    }

    /**
     * 查询负责人积分排名列表
     *
     * @param pdmScoreRankManager 负责人积分排名
     * @return 负责人积分排名
     */
    @Override
    public List<PdmScoreRankManager> selectPdmScoreRankManagerList(PdmScoreRankManager pdmScoreRankManager)
    {
        return pdmScoreRankManagerMapper.selectPdmScoreRankManagerList(pdmScoreRankManager);
    }

    /**
     * 新增负责人积分排名
     *
     * @param pdmScoreRankManager 负责人积分排名
     * @return 结果
     */
    @Override
    public int insertPdmScoreRankManager(PdmScoreRankManager pdmScoreRankManager)
    {
        pdmScoreRankManager.setCreateTime(DateUtils.getNowDate());
        return pdmScoreRankManagerMapper.insertPdmScoreRankManager(pdmScoreRankManager);
    }

    /**
     * 修改负责人积分排名
     *
     * @param pdmScoreRankManager 负责人积分排名
     * @return 结果
     */
    @Override
    public int updatePdmScoreRankManager(PdmScoreRankManager pdmScoreRankManager)
    {
        pdmScoreRankManager.setUpdateTime(DateUtils.getNowDate());
        return pdmScoreRankManagerMapper.updatePdmScoreRankManager(pdmScoreRankManager);
    }

    /**
     * 批量删除负责人积分排名
     *
     * @param ids 需要删除的负责人积分排名主键
     * @return 结果
     */
    @Override
    public int deletePdmScoreRankManagerByIds(Long[] ids)
    {
        return pdmScoreRankManagerMapper.deletePdmScoreRankManagerByIds(ids);
    }

    /**
     * 删除负责人积分排名信息
     *
     * @param id 负责人积分排名主键
     * @return 结果
     */
    @Override
    public int deletePdmScoreRankManagerById(Long id)
    {
        return pdmScoreRankManagerMapper.deletePdmScoreRankManagerById(id);
    }

    @Transactional
    @Override
    public int saveRank(String startDate,String endDate) {
        if(StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)){
            startDate = DateUtils.getFirstDateLastMonthStr(DateUtils.YYYYMMDD);
            endDate = DateUtils.getLastDateLastMonthStr(DateUtils.YYYYMMDD);
        }
        String reportDate = startDate.substring(0,6);
        this.remove(new LambdaUpdateWrapper<PdmScoreRankManager>().eq(PdmScoreRankManager::getReportDate,reportDate));
        PdmScoreRankManager pdmScoreRankManager = new PdmScoreRankManager();
        pdmScoreRankManager.setStartDate(startDate);
        pdmScoreRankManager.setEndDate(endDate);
        return pdmScoreRankManagerMapper.saveRank(pdmScoreRankManager);
    }

    @Override
    public List<PdmScoreRankManager> getManagerRankYearList(PdmScoreRankManager pdmScoreRankManager) {
        return pdmScoreRankManagerMapper.getManagerRankYearList(pdmScoreRankManager);
    }
}
