package com.srs.survey.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.survey.domain.SurItinerary;
import com.srs.survey.mapper.SurItineraryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.survey.mapper.SurSurveyMapper;
import com.srs.survey.domain.SurSurvey;
import com.srs.survey.service.ISurSurveyService;

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

/**
 * 学工去向调查发布	Service业务层处理
 *
 * @author srs
 * @date 2025-03-13
 */
@Service
public class SurSurveyServiceImpl extends ServiceImpl<SurSurveyMapper,SurSurvey> implements ISurSurveyService {
    @Autowired
    private SurSurveyMapper surSurveyMapper;
    @Autowired
    private SurItineraryMapper _itineraryMapper;

    @Override
    public AjaxResult listStu(SurSurvey param){
        QueryWrapper<SurSurvey> queryWrapper = new QueryWrapper<>();
        if(param.getSurveyName()!=null && !param.getSurveyName().equals("")){
            queryWrapper.like("survey_name",param.getSurveyName());
        }
        queryWrapper.eq("status",1);
        queryWrapper.orderByDesc("survey_id");
        List<SurSurvey> list = surSurveyMapper.selectList(queryWrapper);
        list.forEach(item->{
            QueryWrapper<SurItinerary> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("stu_no",getUsername());
            queryWrapper1.eq("survey_id",item.getSurveyId());
            List<SurItinerary> list1 = _itineraryMapper.selectList(queryWrapper1);
            if(list1.size()>0){
                item.leaveStatus = list1.get(0).getLeaveStatus();
                item.isSubmit = "是";
                item.homeGps = list1.get(0).getHomeGps();
            }else{
                item.isSubmit = "否";
            }
        });

        return AjaxResult.success(list);
    }

    /**
     * 查询学工去向调查发布
     *
     * @param surveyId 学工去向调查发布	主键
     * @return 学工去向调查发布
     */
    @Override
    public SurSurvey selectSurSurveyBySurveyId(Long surveyId) {
        return surSurveyMapper.selectSurSurveyBySurveyId(surveyId);
    }

    /**
     * 查询学工去向调查发布	列表
     *
     * @param surSurvey 学工去向调查发布
     * @return 学工去向调查发布
     */
    @Override
    public List<SurSurvey> selectSurSurveyList(SurSurvey surSurvey) {
        return surSurveyMapper.selectSurSurveyList(surSurvey);
    }

    /**
     * 新增学工去向调查发布
     *
     * @param surSurvey 学工去向调查发布
     * @return 结果
     */
    @Override
    public int insertSurSurvey(SurSurvey surSurvey) {
                surSurvey.setCreateTime(DateUtils.getNowDate());
            return surSurveyMapper.insertSurSurvey(surSurvey);
    }

    /**
     * 修改学工去向调查发布
     *
     * @param surSurvey 学工去向调查发布
     * @return 结果
     */
    @Override
    public int updateSurSurvey(SurSurvey surSurvey) {
        return surSurveyMapper.updateSurSurvey(surSurvey);
    }

    /**
     * 批量删除学工去向调查发布
     *
     * @param surveyIds 需要删除的学工去向调查发布	主键
     * @return 结果
     */
    @Override
    public int deleteSurSurveyBySurveyIds(Long[] surveyIds) {
        return surSurveyMapper.deleteSurSurveyBySurveyIds(surveyIds);
    }

    /**
     * 删除学工去向调查发布	信息
     *
     * @param surveyId 学工去向调查发布	主键
     * @return 结果
     */
    @Override
    public int deleteSurSurveyBySurveyId(Long surveyId) {
        return surSurveyMapper.deleteSurSurveyBySurveyId(surveyId);
    }
}
