package com.srs.stureg.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.stureg.domain.SrsStuReg;
import com.srs.stureg.domain.SrsStuTodo;
import com.srs.stureg.domain.VO.SrsStuTodoVo;
import com.srs.stureg.mapper.SrsStuRegMapper;
import com.srs.stureg.mapper.SrsStuTodoMapper;
import com.srs.stureg.service.ISrsStuTodoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import static com.srs.common.utils.DateUtils.getNowDate;
import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 学生待办表Service业务层处理
 *
 * @author srs
 * @date 2023-08-18
 */
@Service
public class SrsStuTodoServiceImpl extends ServiceImpl<SrsStuTodoMapper, SrsStuTodo> implements ISrsStuTodoService
{
    @Autowired
    private SrsStuTodoMapper srsStuTodoMapper;

    @Autowired
    public SrsStuRegMapper _regMapper;

    @Autowired
    public PlatformTransactionManager transactionManager;

    @Override
    public AjaxResult getOwnTaskStatusByCode(SrsStuTodo param){
        QueryWrapper<SrsStuTodo> query = new QueryWrapper<>();
        query.eq("ksh",param.ksh)
                .eq("task_code",param.taskCode)
                .last("limit 1");
        SrsStuTodo res = srsStuTodoMapper.selectOne(query);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult confirmTask(String ksh,String taskCode){
        TransactionDefinition def = new DefaultTransactionDefinition();
        TransactionStatus status = transactionManager.getTransaction(def);
        try{

            if(Objects.equals(taskCode, "ZXXS")){
                QueryWrapper<SrsStuReg> regQuery = new QueryWrapper<>();
                regQuery.eq("ksh",ksh);
                SrsStuReg stu = _regMapper.selectOne(regQuery);
                if(stu == null){
                    throw new Exception("考生号不在新生记录中");
                }
                stu.setRegStatus("3");
                int res = _regMapper.updateById(stu);
                if(res == 0){
                    throw new Exception("更新学生注册状态失败");
                }
            }

            QueryWrapper<SrsStuTodo> searchQuery = new QueryWrapper<>();
            searchQuery.eq("ksh",ksh)
                    .eq("task_code",taskCode)
                    .last("limit 1");
            SrsStuTodo find = srsStuTodoMapper.selectOne(searchQuery);
            if(find == null){
                find = new SrsStuTodo();
                find.setKsh(ksh);
                find.setStatus("1");
                find.setTaskCode(taskCode);
            }else{
                find.setStatus("1");
            }
            find.setUpdateTime(getNowDate());
            boolean res = this.saveOrUpdate(find);
            if(!res){
                throw new Exception("保存失败");
            }

            transactionManager.commit(status);
            return AjaxResult.success();
        }catch(Exception ex){
            transactionManager.rollback(status);
            return AjaxResult.error(ex.getMessage());
        }
    }



    /**
     * 查询学生待办表
     *
     * @param id 学生待办表主键
     * @return 学生待办表
     */
    @Override
    public SrsStuTodo selectSrsStuTodoById(Long id)
    {
        return srsStuTodoMapper.selectSrsStuTodoById(id);
    }

    /**
     * 查询学生待办表列表
     *
     * @param srsStuTodo 学生待办表
     * @return 学生待办表
     */
    @Override
    public List<SrsStuTodo> selectSrsStuTodoList(SrsStuTodo srsStuTodo)
    {
        return srsStuTodoMapper.selectSrsStuTodoList(srsStuTodo);
    }

    public List<SrsStuTodoVo> selectSrsStuTodoListJoin(SrsStuTodo srsStuTodo){
        return srsStuTodoMapper.selectSrsStuTodoListJoin(srsStuTodo);
    }

    /**
     * 新增学生待办表
     *
     * @param srsStuTodo 学生待办表
     * @return 结果
     */
    @Override
    public int insertSrsStuTodo(SrsStuTodo srsStuTodo)
    {
        srsStuTodo.setCreateTime(getNowDate());
        return srsStuTodoMapper.insertSrsStuTodo(srsStuTodo);
    }

    /**
     * 修改学生待办表
     *
     * @param srsStuTodo 学生待办表
     * @return 结果
     */
    @Override
    public int updateSrsStuTodo(SrsStuTodo srsStuTodo)
    {
        return srsStuTodoMapper.updateSrsStuTodo(srsStuTodo);
    }

    /**
     * 修改状态
     */
    @Override
    public int updateSrsStuTodoStastus(SrsStuTodo srsStuTodo){
        QueryWrapper<SrsStuTodo> query = new QueryWrapper<>();
        query.eq("ksh",srsStuTodo.ksh)
                .eq("task_code",srsStuTodo.taskCode)
                .last("limit 1");
        SrsStuTodo todo = srsStuTodoMapper.selectOne(query);
        if(todo == null){
            todo = srsStuTodo;
        }else{
            todo.setStatus("1");
        }
        boolean res = this.saveOrUpdate(todo);
        if(res){
            return 1;
        }
        return 0;
    }


    /**
     * 批量删除学生待办表
     *
     * @param ids 需要删除的学生待办表主键
     * @return 结果
     */
    @Override
    public int deleteSrsStuTodoByIds(Long[] ids)
    {
        return srsStuTodoMapper.deleteSrsStuTodoByIds(ids);
    }

    /**
     * 删除学生待办表信息
     *
     * @param id 学生待办表主键
     * @return 结果
     */
    @Override
    public int deleteSrsStuTodoById(Long id)
    {
        return srsStuTodoMapper.deleteSrsStuTodoById(id);
    }
}
