package com.xinyilu.system.service.testItemCountryScoreinfo.impl;

import com.xinyilu.common.constant.Constants;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.exception.ServiceException;
import com.xinyilu.common.utils.DateUtils;
import com.xinyilu.system.domain.testItemCountryScoreinfo.TestItemCountryScore;
import com.xinyilu.system.domain.vo.TestItemCountryScoreVO;
import com.xinyilu.system.mapper.testItemCountryScoreinfo.TestItemCountryScoreMapper;
import com.xinyilu.system.service.testItemCountryScoreinfo.ITestItemCountryScoreService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 全国平均成绩Service业务层处理
 *
 * @author xinyilu
 * @date 2022-08-03
 */
@Service
public class TestItemCountryScoreServiceImpl implements ITestItemCountryScoreService {
    @Resource
    private TestItemCountryScoreMapper testItemCountryScoreMapper;

    /**
     * 查询全国平均成绩
     *
     * @param id 全国平均成绩主键
     * @return 全国平均成绩
     */
    @Override
    public TestItemCountryScore selectTestItemCountryScoreById(Long id) {
        return testItemCountryScoreMapper.selectTestItemCountryScoreById(id);
    }

    /**
     * 查询全国平均成绩列表
     *
     * @param testItemCountryScore 全国平均成绩
     * @return 全国平均成绩
     */
    @Override
    public List<TestItemCountryScoreVO> selectTestItemCountryScoreList(TestItemCountryScoreVO testItemCountryScore) {
        return testItemCountryScoreMapper.selectTestItemCountryScoreList(testItemCountryScore);
    }

    /**
     * 新增全国平均成绩
     *
     * @param testItemCountryScore 全国平均成绩
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTestItemCountryScore(TestItemCountryScore testItemCountryScore) {
        //根据项目id和性别查询全国平均成绩信息
        List<Long> itemCountryScoreIdList = testItemCountryScoreMapper.getItemCountryScoreIds(testItemCountryScore.getItemId(), testItemCountryScore.getSex());
        if (CollectionUtils.isNotEmpty(itemCountryScoreIdList) && itemCountryScoreIdList.size() > 0) {
            //删除原有成绩信息
            int deleteCount = testItemCountryScoreMapper.deleteTestItemCountryScoreByIds(itemCountryScoreIdList.toArray(new Long[0]));
            if (deleteCount <= 0) {
                throw new ServiceException("删除原有项目成绩失败");
            }
        }
        testItemCountryScore.setCreateTime(DateUtils.getNowDate());
        //是否删除
        testItemCountryScore.setDelFlag(0);
        return testItemCountryScoreMapper.insertTestItemCountryScore(testItemCountryScore);
    }

    /**
     * 修改全国平均成绩
     *
     * @param testItemCountryScore 全国平均成绩
     * @return 结果
     */
    @Override
    public int updateTestItemCountryScore(TestItemCountryScore testItemCountryScore) {
        testItemCountryScore.setUpdateTime(DateUtils.getNowDate());
        //是否删除
        testItemCountryScore.setDelFlag(0);
        //根据项目id判断项目类型
        String itemType = testItemCountryScoreMapper.getItemType(testItemCountryScore.getItemId());
        if (Constants.ITEM_NUMBER.equals(itemType)) {
            testItemCountryScoreMapper.updateTestItemCountryTextById(testItemCountryScore.getId());
        } else if (Constants.ITEM_TEXT.equals(itemType)) {
            testItemCountryScoreMapper.updateTestItemCountryScoreById(testItemCountryScore.getId());
        }
        return testItemCountryScoreMapper.updateTestItemCountryScore(testItemCountryScore);
    }

    /**
     * 批量删除全国平均成绩
     *
     * @param ids 需要删除的全国平均成绩主键
     * @return 结果
     */
    @Override
    public int deleteTestItemCountryScoreByIds(Long[] ids) {
        return testItemCountryScoreMapper.deleteTestItemCountryScoreByIds(ids);
    }

    /**
     * 删除全国平均成绩信息
     *
     * @param id 全国平均成绩主键
     * @return 结果
     */
    @Override
    public int deleteTestItemCountryScoreById(Long id) {
        return testItemCountryScoreMapper.deleteTestItemCountryScoreById(id);
    }

    /**
     * 删除全国平均成绩信息
     *
     * @param ids 全国平均成绩主键
     * @return 结果
     */
    @Override
    public int updateTestItemCountryScoreByIds(Long[] ids) {
        return testItemCountryScoreMapper.updateTestItemCountryScoreByIds(ids);
    }

    /**
     * @param itemId 项目id
     * @param sex    学生性别
     * @return 全国平均成绩信息
     */
    @Override
    public AjaxResult getTestItemCountryScoreInfo(Long itemId, String sex) {
        return AjaxResult.success(testItemCountryScoreMapper.getTestItemCountryScore(itemId, sex));
    }
}
