package com.ruoyi.school.course.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.page.RespPage;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.core.api.APIResponse;
import com.ruoyi.core.api.ApiResEnums;
import com.ruoyi.school.course.domain.ScClaTimeAttend;
import com.ruoyi.school.course.domain.req.time.ReqSearchScClaTimeAttend;
import com.ruoyi.school.course.domain.resp.time.RespClaTimeAttend;
import com.ruoyi.school.course.mapper.ScClaTimeAttendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author ：zhangbaoyu
 * @date ：Created in 2020-01-14 17:24
 */
@Service
@Transactional
public class BusinessScClaTimeAttendService {

    @Autowired
    private IScClaTimeAttendService scClaTimeAttendService;

    @Autowired
    private ScClaTimeAttendMapper attendMapper;

    /**
     * 查询
     *
     * @param reqSearchScClaTimeAttend
     * @return
     */
    public RespPage<RespClaTimeAttend> searchList(ReqSearchScClaTimeAttend reqSearchScClaTimeAttend) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        reqSearchScClaTimeAttend.setTenantId(loginUser.getNowTenantId());
        RespPage<RespClaTimeAttend> page = new RespPage(reqSearchScClaTimeAttend.getPageNum(), reqSearchScClaTimeAttend.getPageSize());
        List<RespClaTimeAttend> attendList = attendMapper.selectTimeAttendList(reqSearchScClaTimeAttend, page);
        page.setRows(attendList);
        return page;
    }

    /**
     * 已上课 出席详情
     *
     * @param courseTimeId
     * @return
     */
    public APIResponse hadClaTimeAttendDetail(Long courseTimeId) {
        QueryWrapper<ScClaTimeAttend> qwAttend = new QueryWrapper<>();
        qwAttend.select( "attend_status", "memo");
        qwAttend.eq("course_time_id", courseTimeId);
        List<ScClaTimeAttend> claTimeAttendList = scClaTimeAttendService.list(qwAttend);
        return APIResponse.toAPIResponse(claTimeAttendList);
    }

    /**
     * 详情
     *
     * @param attendId
     * @return
     */
    public APIResponse detailById(Long attendId) {
        if (null == attendId) {
            return APIResponse.toAPIResponse(null);
        }
        ScClaTimeAttend detailInfo = scClaTimeAttendService.getById(attendId);
        return APIResponse.toAPIResponse(detailInfo);
    }

    /**
     * 添加
     *
     * @param scClaTimeAttend
     * @return
     */
    public APIResponse addScClaTimeAttend(ScClaTimeAttend scClaTimeAttend) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        scClaTimeAttend.setCreateUser(loginUser.getUserId());
        boolean addScClaTimeAttend = scClaTimeAttendService.save(scClaTimeAttend);
        if (addScClaTimeAttend) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }

    /**
     * 更新
     *
     * @param scClaTimeAttend
     * @return
     */
    public APIResponse updateScClaTimeAttend(ScClaTimeAttend scClaTimeAttend) {
        if (null == scClaTimeAttend.getAttendId()) {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
        boolean updateScClaTimeAttend = scClaTimeAttendService.updateById(scClaTimeAttend);
        if (updateScClaTimeAttend) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }

    /**
     * 删除
     *
     * @param attendIds
     * @return
     */
    public APIResponse deleteById(Long[] attendIds) {
        if (null == attendIds || attendIds.length == 0) {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
        boolean deleteScClaTimeAttend = scClaTimeAttendService.removeByIds(Arrays.asList(attendIds));
        if (deleteScClaTimeAttend) {
            return APIResponse.toOkResponse();
        } else {
            return APIResponse.toExceptionResponse(ApiResEnums.FAIL_WAIT_A_MINUTE);
        }
    }
}
