package com.hchyun.test.service.impl;

import java.util.List;

import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.utils.DateUtils;
import com.hchyun.common.utils.SecurityUtils;
import com.hchyun.common.utils.ServerResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hchyun.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hchyun.test.entity.Results;
import com.hchyun.test.dao.StuDao;
import com.hchyun.test.entity.Stu;
import com.hchyun.test.service.StuService;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 学生Service业务层处理
 * 
 * @author hchyun
 * @date 2021-01-23
 */
@Service
public class StuServiceImpl implements StuService {
    private Logger logger = LoggerFactory.getLogger(StuServiceImpl.class);

    @Autowired
    private StuDao stuDao;

    /**
     * 查询学生
     * 
     * @param id 学生ID
     * @return 学生
     */
    @Override
    public ServerResult<Stu> selectStuById(Long id) {
        try {
            Stu stu = stuDao.selectStuById(id);
            if (stu != null){
                return new ServerResult<Stu>(true,stu);
            }else {
                return new ServerResult<Stu>(false, ReturnConstants.RESULT_EMPTY);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<Stu>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 查询学生列表
     * 
     * @param stu 学生
     * @return 学生
     */
    @Override
    public ServerResult<List<Stu>> selectStuList(Stu stu) {
        try {
            List<Stu> stuList = stuDao.selectStuList(stu);
            if (stuList.size()>0){
                return new ServerResult<List<Stu>>(true,stuList);
            }else {
                return new ServerResult<List<Stu>>(false,ReturnConstants.RESULT_EMPTY);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<List<Stu>>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 新增学生
     * 
     * @param stu 学生
     * @return 结果
     */
    @Transactional
    @Override
    public ServerResult<Integer> insertStu(Stu stu) {
        try {
            stu.setCreateTime(DateUtils.getNowDate());
            stu.setCreateBy(SecurityUtils.getUserId());
            int renewal = stuDao.insertStu(stu);
            if (insertResults(stu)){
                return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
            }
            if (renewal >0){
                return new ServerResult<Integer>(true,renewal);
            }else {
                return new ServerResult<Integer>(false,ReturnConstants.SYS_FAILL);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 修改学生
     * 
     * @param stu 学生
     * @return 结果
     */
    @Transactional
    @Override
    public ServerResult<Integer> updateStu(Stu stu) {
        try {
            stu.setUpdateTime(DateUtils.getNowDate());
            stu.setUpdateBy(SecurityUtils.getUserId());
            stuDao.deleteResultsBySId(stu.getId());
            if (insertResults(stu)){
                return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
            }
            Integer renewal = stuDao.updateStu(stu);
            if (renewal >0){
                return new ServerResult<Integer>(true,renewal);
            }else {
                return new ServerResult<Integer>(false,ReturnConstants.SYS_FAILL);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 批量删除学生
     * 
     * @param ids 需要删除的学生ID
     * @return 结果
     */
    @Transactional
    @Override
    public ServerResult<Integer> deleteStuByIds(Long[] ids) {
        try {
            //批量删除子表数据
            stuDao.deleteResultsBySIds(ids);
            Integer renewal = stuDao.deleteStuByIds(ids);
            if (renewal >0){
                return new ServerResult<Integer>(true,renewal);
            }else {
                return new ServerResult<Integer>(false,ReturnConstants.SYS_FAILL);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 删除学生信息
     * 
     * @param id 学生ID
     * @return 结果
     */
    @Override
    public ServerResult<Integer> deleteStuById(Long id) {
        try {
        //删除子表数据
        stuDao.deleteResultsBySId(id);
            Integer renewal = stuDao.deleteStuById(id);
            if (renewal >0){
                return new ServerResult<Integer>(true,renewal);
            }else {
                return new ServerResult<Integer>(false,ReturnConstants.SYS_FAILL);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 新增成绩信息
     * 
     * @param stu 学生对象
     */
    public boolean insertResults(Stu stu) {
        List<Results> resultsList = stu.getResultsList();
        Long id = stu.getId();
        if (StringUtils.isNotNull(resultsList)) {
            List<Results> list = new ArrayList<Results>();
            for (Results results : resultsList){
                results.setsId(id);
                list.add(results);
            }
            if (list.size() > 0) {
                Integer renewal = stuDao.batchResults(list);
                if (renewal>0){
                    return false;
                }
            }
        }
        return true;
    }
}
