package jnpf.attendance.service.impl;

import jnpf.attendance.mapper.AttendanceApprovalSettingMapper;
import jnpf.attendance.service.AttendanceApprovalSettingService;
import jnpf.entity.AttendanceApprovalSetting;
import jnpf.enums.attendance.ApprovalPermissionTypeEnum;
import jnpf.enums.attendance.ApprovalSettingTypeEnum;
import jnpf.model.attendance.dto.AttendanceApprovalSettingDto;
import jnpf.util.FtbUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AttendanceApprovalSettingServiceImpl implements AttendanceApprovalSettingService {

    @Resource
    private AttendanceApprovalSettingMapper attendanceApprovalSettingMapper;

    @Override
    public void update(AttendanceApprovalSettingDto attendanceApprovalSettingDto) {
        AttendanceApprovalSetting attendanceApprovalSetting = JsonUtil.getJsonToBean(attendanceApprovalSettingDto, AttendanceApprovalSetting.class);
        if (attendanceApprovalSetting.getId().isEmpty()) {
            attendanceApprovalSetting.setId(FtbUtil.getId());
            attendanceApprovalSettingMapper.insert(attendanceApprovalSetting);
        }else {
            attendanceApprovalSettingMapper.updateById(attendanceApprovalSetting);
        }
    }

    @Override
    public void addTemplateSetting(String groupId) {
        save(groupId, ApprovalSettingTypeEnum.ROUTINE.getCode(), ApprovalPermissionTypeEnum.CURRENT_GROUP.getCode());
        save(groupId, ApprovalSettingTypeEnum.ACTION_RESULT.getCode(), ApprovalPermissionTypeEnum.SUPERIOR.getCode());
        save(groupId, ApprovalSettingTypeEnum.OUT.getCode(), ApprovalPermissionTypeEnum.CURRENT_GROUP.getCode());
        save(groupId, ApprovalSettingTypeEnum.LEAVE.getCode(), ApprovalPermissionTypeEnum.SUPERIOR.getCode());
        save(groupId, ApprovalSettingTypeEnum.OVERTIME.getCode(), ApprovalPermissionTypeEnum.CURRENT_GROUP.getCode());
        save(groupId, ApprovalSettingTypeEnum.GO_OUT.getCode(), ApprovalPermissionTypeEnum.SUPERIOR.getCode());
        save(groupId, ApprovalSettingTypeEnum.BUSINESS_TRIP.getCode(), ApprovalPermissionTypeEnum.SUPERIOR.getCode());
    }

    private void save(String groupId, Integer type, Integer permission) {
        AttendanceApprovalSetting attendanceApprovalSetting = new AttendanceApprovalSetting();
        attendanceApprovalSetting.setId(RandomUtil.uuId());
        attendanceApprovalSetting.setType(type);
        attendanceApprovalSetting.setPermission(permission);
        attendanceApprovalSetting.setGroupId(groupId);
        attendanceApprovalSettingMapper.insert(attendanceApprovalSetting);
    }
}
