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

import java.util.List;

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.common.utils.codes.GenerateSystemCodeUtil;
import com.xinyilu.system.domain.ItemBatchInfo.ItemBatchInfo;
import com.xinyilu.system.domain.dto.TestScoreBatchDTO;
import com.xinyilu.system.domain.testItem.TestItems;
import com.xinyilu.system.domain.testScoreBatchinfo.TestScoreBatch;
import com.xinyilu.system.mapper.ItemBatchInfo.ItemBatchInfoMapper;
import com.xinyilu.system.mapper.testScoreBatchinfo.TestScoreBatchMapper;
import com.xinyilu.system.service.testScoreBatchinfo.ITestScoreBatchService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 测试成绩批次Service业务层处理
 *
 * @author xinyilu
 * @date 2022-08-02
 */
@Service
public class TestScoreBatchServiceImpl implements ITestScoreBatchService {
    @Resource
    private TestScoreBatchMapper testScoreBatchMapper;
    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;
    @Resource
    private ItemBatchInfoMapper itemBatchInfoMapper;
    /**
     * 查询测试成绩批次
     *
     * @param id 测试成绩批次主键
     * @return 测试成绩批次
     */
    @Override
    public TestScoreBatch selectTestScoreBatchById(Long id) {
        return testScoreBatchMapper.selectTestScoreBatchById(id);
    }

    /**
     * 查询测试成绩批次列表
     *
     * @param testScoreBatch 测试成绩批次
     * @return 测试成绩批次
     */
    @Override
    public List<TestScoreBatch> selectTestScoreBatchList(TestScoreBatch testScoreBatch) {
        return testScoreBatchMapper.selectTestScoreBatchList(testScoreBatch);
    }

    /**
     * 新增测试成绩批次
     *
     * @param testScoreBatch 测试成绩批次
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertTestScoreBatch(TestScoreBatchDTO testScoreBatch) {
        // 判断项目id不能为空
        if (CollectionUtils.isEmpty(testScoreBatch.getItemBatchInfoIdList())){
           return AjaxResult.error("测试项目信息不能为空");
        }
        testScoreBatch.setCreateTime(DateUtils.getNowDate());
        //是否删除
        testScoreBatch.setDelFlag(0);
        // 随机生成批次编号
        testScoreBatch.setBatchCode(Constants.PC_PREFIX + generateSystemCodeUtil.generateSystemCode(Constants.PC_PREFIX));
        // 新增批次信息
        int insertTestScoreBatch = testScoreBatchMapper.insertTestScoreBatch(testScoreBatch);
        if (insertTestScoreBatch <= 0){
            throw new ServiceException("新增批次信息失败");
        }
        // 获取批次id
        Long batchId = testScoreBatch.getId();
        List<ItemBatchInfo> batchInfoList = Lists.newArrayList();
        for (Long itemId:testScoreBatch.getItemBatchInfoIdList()) {
            ItemBatchInfo itemBatchInfo = new ItemBatchInfo();
            itemBatchInfo.setBatchId(batchId);
            itemBatchInfo.setItemId(itemId);
            itemBatchInfo.setDelFlag(0);
            itemBatchInfo.setCreateBy(testScoreBatch.getCreateBy());
            itemBatchInfo.setCreateTime(DateUtils.getNowDate());
            batchInfoList.add(itemBatchInfo);
        }
        if (CollectionUtils.isNotEmpty(batchInfoList)) {
            //插入
            int insertItemBatchInfo = itemBatchInfoMapper.insertItemBatchInfoList(batchInfoList);
            if (insertItemBatchInfo <= 0){
                throw new ServiceException("新增批次项目信息失败");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改测试成绩批次
     *
     * @param testScoreBatch 测试成绩批次
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult updateTestScoreBatch(TestScoreBatchDTO testScoreBatch) {
        // 判断项目id不能为空
        if (CollectionUtils.isEmpty(testScoreBatch.getItemBatchInfoIdList())){
            return AjaxResult.error("测试项目信息不能为空");
        }
        testScoreBatch.setUpdateTime(DateUtils.getNowDate());
        //是否删除
        testScoreBatch.setDelFlag(0);
        // 修改批次信息
        int updateTestScoreBatch = testScoreBatchMapper.updateTestScoreBatch(testScoreBatch);
        if (updateTestScoreBatch <= 0){
            throw new ServiceException("修改批次信息失败");
        }
        //删除原有测试项目信息
        int batchInfoCount = itemBatchInfoMapper.selectItemBatchInfoCount(testScoreBatch.getId());
        if (batchInfoCount > 0) {
            int deleteItemBatchInfoById = itemBatchInfoMapper.deleteItemBatchInfoById(testScoreBatch.getId());
            if (deleteItemBatchInfoById <= 0){
                throw new ServiceException("删除批次项目信息失败");
            }
        }
        // 新增批次项目关系表
        List<ItemBatchInfo> batchInfoList = Lists.newArrayList();
        for (Long itemId:testScoreBatch.getItemBatchInfoIdList()) {
            ItemBatchInfo itemBatchInfo = new ItemBatchInfo();
            itemBatchInfo.setBatchId(testScoreBatch.getId());
            itemBatchInfo.setItemId(itemId);
            itemBatchInfo.setDelFlag(0);
            itemBatchInfo.setCreateBy(testScoreBatch.getCreateBy());
            itemBatchInfo.setCreateTime(DateUtils.getNowDate());
            batchInfoList.add(itemBatchInfo);
        }
        if (CollectionUtils.isNotEmpty(batchInfoList)) {
            //插入
            int insertItemBatchInfo = itemBatchInfoMapper.insertItemBatchInfoList(batchInfoList);
            if (insertItemBatchInfo <= 0){
                throw new ServiceException("修改批次项目信息失败");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除测试成绩批次
     *
     * @param ids 需要删除的测试成绩批次主键
     * @return 结果
     */
    @Override
    public int deleteTestScoreBatchByIds(Long[] ids) {
        return testScoreBatchMapper.deleteTestScoreBatchByIds(ids);
    }

    /**
     * 删除测试成绩批次信息
     *
     * @param id 测试成绩批次主键
     * @return 结果
     */
    @Override
    public int deleteTestScoreBatchById(Long id) {
        return testScoreBatchMapper.deleteTestScoreBatchById(id);
    }

    /**
     * 逻辑批量删除测试成绩批次
     *
     * @param ids 需要删除的测试成绩批次主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult updateTestScoreBatchByIds(Long[] ids) {
        // 逻辑删除批次成绩
        int updateTestScoreBatchByIds = testScoreBatchMapper.updateTestScoreBatchByIds(ids);
        if (updateTestScoreBatchByIds <= 0 ){
            throw new ServiceException("删除批次信息失败");
        }
        // 逻辑删除批次项目信息
        int updateItemBatchInfoByIds = itemBatchInfoMapper.updateItemBatchInfoByIds(ids);
        if (updateItemBatchInfoByIds <= 0 ){
            throw new ServiceException("删除批次项目信息失败");
        }
        return AjaxResult.success();
    }


    /**
     * 查询项目id对应的项目名称
     *
     * @param batchId 项目id
     * @return java.util.List<com.xinyilu.system.domain.testItem.TestItems>
     **/
    @Override
    public List<TestItems> getItemBatchInfoList(Long batchId) {
        return testScoreBatchMapper.getItemBatchInfoList(batchId);
    }

}
