package com.boman.web.core.service.attendance.rules;

import com.alibaba.fastjson.JSONObject;
import com.boman.web.core.service.common.ICommonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.boman.domain.constant.AttendanceRulesConst.ATTENDANCE_RULES_DEPT_ID;
import static com.boman.domain.constant.AttendanceRulesConst.ATTENDANCE_RULES_TABLE_NAME;
import static com.boman.common.core.utils.obj.ObjectUtils.*;

/**
 * @author shiqian
 * @date 2021年06月15日 17:15
 **/
@Service
public class AttendanceRulesServiceImpl implements AttendanceRulesService{

    @Resource
    private ICommonService commonService;

    /**
     * 功能描述: 根据deptId, 查找规则
     *
     * @param deptId deptId
     * @return com.alibaba.fastjson.JSONObject
     */
    @Override
    public JSONObject listByDeptId(Long deptId) {
        JSONObject map = new JSONObject(2);
        map.put(ATTENDANCE_RULES_DEPT_ID, deptId);
        JSONObject result = commonService.getOneByMap(ATTENDANCE_RULES_TABLE_NAME, map);
        if (isEmpty(result)) {
            // 该部门没有单独设置，遵从总公司的上下班时间
            map.clear();
            map.put(ATTENDANCE_RULES_DEPT_ID, null);
            result = commonService.getOneByMap(ATTENDANCE_RULES_TABLE_NAME, map);
            if (isEmpty(result)) {
                throw new IllegalArgumentException("该公司未配置上下班时间");
            }
        }

        return result;
    }

}
