package com.srs.dormitory.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.core.page.TableDataInfo;
import com.srs.dormitory.domain.DmsApprovalProcess;
import com.srs.dormitory.domain.DmsForeignApplication;
import com.srs.dormitory.domain.Dto.AddForeignApplicationDto;
import com.srs.dormitory.domain.Dto.ForeignApplicationListDto;
import com.srs.dormitory.mapper.DmsApprovalProcessMapper;
import com.srs.dormitory.mapper.DmsForeignApplicationMapper;
import com.srs.dormitory.service.IDmsApprovalProcessService;
import com.srs.dormitory.service.IDmsForeignApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.srs.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 外来人员住宿申请表 服务实现类
 * </p>
 *
 * @author szz
 * @since 2023年08月04日
 */
@Service
public class IDmsForeignApplicationServiceImpl extends ServiceImpl<DmsForeignApplicationMapper, DmsForeignApplication> implements IDmsForeignApplicationService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private DmsApprovalProcessMapper dmsApprovalProcessMapper;
    @Autowired
    private IDmsApprovalProcessService dmsApprovalProcessService;

    /**
     * 提交外来人员住宿申请表
     * @param applicationDto 请求参数
     * @return
     */
    @Override
    public AjaxResult addForeignApplication(AddForeignApplicationDto applicationDto) {
        SysUser campusUser = sysUserService.selectUserById(applicationDto.getCampusContactId());
        if (campusUser==null) return AjaxResult.error("校内联系人错误");
        if (applicationDto.getStartDate().isBefore(LocalDateTime.now())||applicationDto.getStartDate().isAfter(applicationDto.getEndDate())){
            return AjaxResult.error("时间填写错误");
        }
        DmsForeignApplication application = new DmsForeignApplication();
        BeanUtil.copyProperties(applicationDto,application);
        boolean success = save(application);
        if (!success) return AjaxResult.error();
        return AjaxResult.success();
    }
    /**
     * 外来人员住宿申请表详情
     * @param id 请求参数
     * @return
     */
    @Override
    public AjaxResult getForeignApplicationInfo(Long id) {
//        Map<String,Object> info = baseMapper.getForeignApplicationInfo(id);
//        if (info == null) return AjaxResult.error("id错误");
//        List<Map<String, Object>> approvalList = dmsApprovalProcessMapper.selectApprovalList(id,6);
//        info.put("approvalList",approvalList);
//        return AjaxResult.success(info);
        return null;
    }

    /**
     * 查看外来人员住宿申请请求列表
     * @param dto 请求参数
     * @return
     */
    @Override
    public List<Map<String,Object>> getForeignApplicationList(ForeignApplicationListDto dto) {
        return baseMapper.getForeignApplicationList(dto);
    }

    /**
     * 批量删除外来人员住宿申请表
     * @param ids 外来人员住宿申请表Id列表
     * @return
     */
    @Transactional
    @Override
    public AjaxResult batchDeleteForeignApplication(List<Long> ids) {
//        boolean removeApproval = dmsApprovalProcessService.lambdaUpdate()
//                .eq(DmsApprovalProcess::getApplicationType, 6)
//                .in(DmsApprovalProcess::getApplicationId, ids)
//                .remove();
//        if (!removeApproval) throw new RuntimeException("删除审核流程失败");
//        boolean removeApplication = removeBatchByIds(ids);
//        if (!removeApplication) throw new RuntimeException("删除申请表失败");
        return AjaxResult.success();
    }
}
