package com.xinyilu.system.service.testItemCityScoreinfo.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.dto.TestItemCityScoreinfoDTO;
import com.xinyilu.system.domain.vo.TestItemCityScoreinfoVo;
import com.xinyilu.system.mapper.testItemCityScoreinfo.TestItemCityScoreMapper;
import com.xinyilu.system.service.testItemCityScoreinfo.ITestItemCityScoreService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * 全市平均成绩Service业务层处理
 *
 * @author xinyilu
 * @date 2022-08-03
 */
@Service
public class TestItemCityScoreServiceImpl implements ITestItemCityScoreService {
    @Resource
    private TestItemCityScoreMapper testItemCityScoreMapper;

    /**
     * 查询全市平均成绩
     *
     * @param id 全市平均成绩主键
     * @return 全市平均成绩
     */
    @Override
    public TestItemCityScoreinfoVo selectTestItemCityScoreById(Long id) {
        return testItemCityScoreMapper.selectTestItemCityScoreById(id);
    }

    /**
     * 查询全市平均成绩列表
     *
     * @param testItemCityScore 全市平均成绩
     * @return 全市平均成绩
     */
    @Override
    public List<TestItemCityScoreinfoVo> selectTestItemCityScoreList(TestItemCityScoreinfoVo testItemCityScore) {
        return testItemCityScoreMapper.selectTestItemCityScoreList(testItemCityScore);
    }

    /**
     * 新增全市平均成绩
     *
     * @param testItemCityScore 全市平均成绩
     * @return 结果
     */
    @Override
    public int insertTestItemCityScore(TestItemCityScoreinfoDTO testItemCityScore) {
        //查询原有项目全市平均成绩信息
        List<Long> itemCityScoreIdList = testItemCityScoreMapper.getItemCityScoreIds(testItemCityScore.getAreaId(), testItemCityScore.getItemId(), testItemCityScore.getSex());
        if (CollectionUtils.isNotEmpty(itemCityScoreIdList) && itemCityScoreIdList.size() > 0) {
            //删除原有成绩信息
            int deleteCount = testItemCityScoreMapper.deleteTestItemCityScoreByIds(itemCityScoreIdList.toArray(new Long[0]));
            if (deleteCount <= 0) {
                throw new ServiceException("删除测试项目原有全市平均成绩信息失败");
            }
        }
        testItemCityScore.setCreateTime(DateUtils.getNowDate());
        //是否删除
        testItemCityScore.setDelFlag(0);
        return testItemCityScoreMapper.insertTestItemCityScore(testItemCityScore);
    }

    /**
     * 修改全市平均成绩
     *
     * @param testItemCityScore 全市平均成绩
     * @return 结果
     */
    @Override
    public int updateTestItemCityScore(TestItemCityScoreinfoDTO testItemCityScore) {
        testItemCityScore.setUpdateTime(DateUtils.getNowDate());
        //是否删除
        testItemCityScore.setDelFlag(0);
        String itemType = testItemCityScoreMapper.getItemType(testItemCityScore.getItemId());
        if (Constants.ITEM_NUMBER.equals(itemType)) {
            //将item_avg_text字段信息清空
            testItemCityScoreMapper.updateItemTextById(testItemCityScore.getId());
        } else if (Constants.ITEM_TEXT.equals(itemType)) {
            //将item_avg_score字段信息清空
            testItemCityScoreMapper.updateItemScoreById(testItemCityScore.getId());
        }
        return testItemCityScoreMapper.updateTestItemCityScore(testItemCityScore);
    }

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

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

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

    /**
     * 全市平均成绩信息
     *
     * @param areaId 区域id
     * @param itemId 项目id
     * @param sex    性别
     * @return 全市平均成绩信息
     */
    @Override
    public AjaxResult getItemCityScoreInfo(Long areaId, Long itemId, String sex) {
        return AjaxResult.success(testItemCityScoreMapper.getItemCityScoreInfo(areaId, itemId, sex));
    }
}
