package com.srs.stureg.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.stureg.domain.SrsStuTodo;
import com.srs.stureg.mapper.SrsStuTodoMapper;
import com.srs.stureg.service.ISrsStuTodoService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.stureg.mapper.SrsRegPhotoMapper;
import com.srs.stureg.domain.SrsRegPhoto;
import com.srs.stureg.service.ISrsRegPhotoService;

/**
 * 新生照片Service业务层处理
 *
 * @author srs
 * @date 2024-04-02
 */
@Service
public class SrsRegPhotoServiceImpl extends ServiceImpl<SrsRegPhotoMapper,SrsRegPhoto> implements ISrsRegPhotoService {
    @Autowired
    private SrsRegPhotoMapper srsRegPhotoMapper;
    @Autowired
    public SrsStuTodoMapper _todoMapper;
    @Autowired
    public ISrsStuTodoService _todoService;

    //图片提交保存
    @Override
    public AjaxResult submitOwnPhoto(SrsRegPhoto param){
        try{
            //新生照片保存
            QueryWrapper<SrsRegPhoto> query=new QueryWrapper<>();
            query.eq("ksh",param.getKsh())
                    .last("limit 1");
            SrsRegPhoto photo=srsRegPhotoMapper.selectOne(query);
            if(photo==null){
                photo =param;
            }else{
                photo.setPhoto(param.getPhoto());
            }
            boolean res =this.saveOrUpdate(photo);
            if(!res){
                throw new Exception("保存失败");
            }

            //新生代办状态修改
            QueryWrapper<SrsStuTodo> todoQuery=new QueryWrapper<>();
            todoQuery.eq("ksh",param.getKsh())
                    .eq("task_code","ZPCJ")
                    .last("limit 1");
            SrsStuTodo todo = _todoMapper.selectOne(todoQuery);
            if(todo == null){
                todo = new SrsStuTodo();
                todo.setKsh(param.getKsh());
                todo.setStatus("1");
                todo.setTaskCode("ZPCJ");
            }else{
                todo.setStatus("1");
            }
            boolean res1 = _todoService.saveOrUpdate(todo);
            if(!res1){
                throw new Exception("保存任务状态失败");
            }


            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }



    //获取新生照片
    @Override
    public AjaxResult getRegPhoto(String ksh){
        QueryWrapper<SrsRegPhoto> query =new QueryWrapper<>();
        query.eq("ksh",ksh)
                .last("limit 1");
        SrsRegPhoto res=srsRegPhotoMapper.selectOne(query);
        return AjaxResult.success(res);
    }





    /**
     * 查询新生照片
     *
     * @param id 新生照片主键
     * @return 新生照片
     */
    @Override
    public SrsRegPhoto selectSrsRegPhotoById(Long id) {
        return srsRegPhotoMapper.selectSrsRegPhotoById(id);
    }

    /**
     * 查询新生照片列表
     *
     * @param srsRegPhoto 新生照片
     * @return 新生照片
     */
    @Override
    public List<SrsRegPhoto> selectSrsRegPhotoList(SrsRegPhoto srsRegPhoto) {
        return srsRegPhotoMapper.selectSrsRegPhotoList(srsRegPhoto);
    }

    /**
     * 新增新生照片
     *
     * @param srsRegPhoto 新生照片
     * @return 结果
     */
    @Override
    public int insertSrsRegPhoto(SrsRegPhoto srsRegPhoto) {
                srsRegPhoto.setCreateTime(DateUtils.getNowDate());
            return srsRegPhotoMapper.insertSrsRegPhoto(srsRegPhoto);
    }

    /**
     * 修改新生照片
     *
     * @param srsRegPhoto 新生照片
     * @return 结果
     */
    @Override
    public int updateSrsRegPhoto(SrsRegPhoto srsRegPhoto) {
                srsRegPhoto.setUpdateTime(DateUtils.getNowDate());
        return srsRegPhotoMapper.updateSrsRegPhoto(srsRegPhoto);
    }

    /**
     * 批量删除新生照片
     *
     * @param ids 需要删除的新生照片主键
     * @return 结果
     */
    @Override
    public int deleteSrsRegPhotoByIds(Long[] ids) {
        return srsRegPhotoMapper.deleteSrsRegPhotoByIds(ids);
    }

    /**
     * 删除新生照片信息
     *
     * @param id 新生照片主键
     * @return 结果
     */
    @Override
    public int deleteSrsRegPhotoById(Long id) {
        return srsRegPhotoMapper.deleteSrsRegPhotoById(id);
    }
}
