package com.mortals.xhx.module.attendance.service.impl;

import cn.hutool.core.util.ReflectUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.common.code.AllHolidaysEnum;
import com.mortals.xhx.common.code.SubAddTypeEnum;
import com.mortals.xhx.module.attendance.dao.AttendanceVacationRecordDao;
import com.mortals.xhx.module.attendance.model.AttendanceVacationBalanceEntity;
import com.mortals.xhx.module.attendance.model.AttendanceVacationRecordEntity;
import com.mortals.xhx.module.attendance.model.vo.AllHolidaysVo;
import com.mortals.xhx.module.attendance.service.AttendanceVacationBalanceService;
import com.mortals.xhx.module.attendance.service.AttendanceVacationRecordService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static cn.hutool.core.util.NumberUtil.add;

/**
 * AttendanceVacationRecordService
 * 员工假期记录信息 service实现
 *
 * @author zxfei
 * @date 2023-04-12
 */
@Service("attendanceVacationRecordService")
public class AttendanceVacationRecordServiceImpl extends AbstractCRUDServiceImpl<AttendanceVacationRecordDao, AttendanceVacationRecordEntity, Long> implements AttendanceVacationRecordService {
    @Autowired
    private AttendanceVacationBalanceService attendanceVacationBalanceService;

    @SneakyThrows
    @Override
    protected void saveAfter(AttendanceVacationRecordEntity entity, Context context) throws AppException {
        super.saveAfter(entity, context);

        //todo 反射属性  设置实体属性更新值
        //attendanceVacationBalanceEntity.setAnnualLeaveDays(attendanceVacationBalanceEntity.getAnnualLeaveDays()+);
        Class<AllHolidaysEnum> allHolidaysEnumClass = AllHolidaysEnum.class;
        Object[] objects = allHolidaysEnumClass.getEnumConstants();
        Method getValue = allHolidaysEnumClass.getMethod("getValue");
        Method getDesc = allHolidaysEnumClass.getMethod("getDesc");
        List<AllHolidaysVo> list = new ArrayList<>();
        for (Object object : objects) {
            AllHolidaysVo vo = new AllHolidaysVo();
            vo.setDesc(getValue.invoke(object).toString());
            vo.setType(Integer.valueOf(getDesc.invoke(object).toString()));
            list.add(vo);
        }
        list.forEach(e -> {
            if (e.getType().equals(entity.getType())){
                System.out.println(e.getDesc());
                AttendanceVacationBalanceEntity attendanceVacationBalanceEntity = attendanceVacationBalanceService.get(entity.getStaffId());
                BigDecimal count = (BigDecimal) ReflectUtil.getFieldValue(attendanceVacationBalanceEntity, e.getDesc());
                if (entity.getSubAddType().equals(SubAddTypeEnum.增加.getValue())) {
                    BigDecimal countNew = count.add(new BigDecimal(String.valueOf(entity.getSubOrAddDays())));
                    ReflectUtil.setFieldValue(attendanceVacationBalanceEntity,e.getDesc(),add(countNew));
                    attendanceVacationBalanceService.update(attendanceVacationBalanceEntity);
                }else if (entity.getSubAddType().equals(SubAddTypeEnum.扣除.getValue())){
                    ReflectUtil.setFieldValue(attendanceVacationBalanceEntity,e.getDesc(),count.subtract(new BigDecimal(String.valueOf(entity.getSubOrAddDays()))));
                    System.out.println(attendanceVacationBalanceEntity);
                    attendanceVacationBalanceService.update(attendanceVacationBalanceEntity);
                }else {
                    return;
                }

            }
        });

    }
}