package jnpf.attendance.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.attendance.mapper.AttendanceLeaveTypeMapper;
import jnpf.attendance.service.AttendanceLeaveRulesService;
import jnpf.attendance.service.AttendanceLeaveTypeService;
import jnpf.entity.attendance.AttendanceLeaveType;
import jnpf.exception.ApproveException;
import jnpf.model.attendance.vo.attendance.AttendanceLeaveRulesVo;
import jnpf.util.StringUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author panpan
 */
@Service
public class AttendanceLeaveTypeServiceImpl extends ServiceImpl<AttendanceLeaveTypeMapper, AttendanceLeaveType> implements AttendanceLeaveTypeService {

    @Resource
    private AttendanceLeaveTypeMapper attendanceLeaveTypeMapper;

    @Resource
    @Lazy
    private AttendanceLeaveRulesService attendanceLeaveRulesService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void typeSaveOrUpdate(AttendanceLeaveType attendanceLeaveType) {
        // 校验名称是否重复,考虑修改时排查传入的id对应的数据
        List<AttendanceLeaveType> attendanceLeaveTypes = attendanceLeaveTypeMapper.selectList(new LambdaQueryWrapper<AttendanceLeaveType>()
                .eq(AttendanceLeaveType::getName, attendanceLeaveType.getName())
                .eq(StringUtil.isNotEmpty(attendanceLeaveType.getId()), AttendanceLeaveType::getId, attendanceLeaveType.getId())
                .eq(AttendanceLeaveType::getDeleteMark, 0));
        if (!attendanceLeaveTypes.isEmpty()) {
            throw new RuntimeException("名称重复");
        }
        attendanceLeaveType.setDeleteMark(0);
        this.saveOrUpdate(attendanceLeaveType);
    }

    @Override
    public void deleteType(String id) {
        AttendanceLeaveType attendanceLeaveType = new AttendanceLeaveType();
        attendanceLeaveType.setId(id);
        attendanceLeaveType.setDeleteMark(1);
        attendanceLeaveType.setDeleteTime(new Date());
        // 删除关联的假期类型
        attendanceLeaveTypeMapper.updateById(attendanceLeaveType);
        attendanceLeaveRulesService.deleteByTypeId(id);

    }

    @Override
    public List<AttendanceLeaveRulesVo> getUserLeaveList() {
        List<AttendanceLeaveType> vo = attendanceLeaveTypeMapper.selectList(new QueryWrapper<AttendanceLeaveType>().lambda()
                .eq(AttendanceLeaveType::getDeleteMark, 0)
                .orderByAsc(AttendanceLeaveType::getCreatorTime));
        return attendanceLeaveRulesService.getUserLeaveList(vo);

    }

    @Override
    public AttendanceLeaveRulesVo getUserLeaveDetail(String id,String userId) throws ApproveException {
        AttendanceLeaveType vo = attendanceLeaveTypeMapper.selectOne(new QueryWrapper<AttendanceLeaveType>().lambda()
                .eq(AttendanceLeaveType::getId, id)
        );
        return attendanceLeaveRulesService.getUserLeaveDetail(vo,userId);
    }

    @Override
    public List<AttendanceLeaveType> getAttendanceLeaveTypes(List<String> holidayIdList) {
        return attendanceLeaveTypeMapper.selectList(new QueryWrapper<AttendanceLeaveType>().lambda()
                .eq(AttendanceLeaveType::getDeleteMark, 0)
                .in(CollUtil.isNotEmpty(holidayIdList), AttendanceLeaveType::getId, holidayIdList)
                .orderByAsc(AttendanceLeaveType::getCreatorTime));
    }

    @Override
    public List<AttendanceLeaveType> getAttendanceLeaveTypesIncludeDeleted(List<String> holidayIdList) {
        return attendanceLeaveTypeMapper.selectList(new QueryWrapper<AttendanceLeaveType>().lambda()
                .in(CollUtil.isNotEmpty(holidayIdList), AttendanceLeaveType::getId, holidayIdList)
                .orderByAsc(AttendanceLeaveType::getCreatorTime));
    }
}
