package cn.iocoder.yudao.module.attendance.service.employeesetting;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.attendance.controller.admin.employeesetting.vo.AttendanceEmployeeSettingBatchSaveReqVO;
import cn.iocoder.yudao.module.attendance.controller.admin.employeesetting.vo.AttendanceEmployeeSettingPageReqVO;
import cn.iocoder.yudao.module.attendance.controller.admin.employeesetting.vo.AttendanceEmployeeSettingRespVO;
import cn.iocoder.yudao.module.attendance.controller.admin.employeesetting.vo.AttendanceEmployeeSettingSaveReqVO;
import cn.iocoder.yudao.module.attendance.controller.admin.info.vo.AttendanceDailyInfoRespVO;
import cn.iocoder.yudao.module.attendance.dal.dataobject.employeesetting.AttendanceEmployeeSettingDO;
import cn.iocoder.yudao.module.attendance.dal.mysql.employeesetting.AttendanceEmployeeSettingMapper;
import cn.iocoder.yudao.module.employee.dal.dataobject.info.EmployeeInfoDO;
import cn.iocoder.yudao.module.employee.service.info.EmployeeInfoService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.attendance.enums.ErrorCodeConstants.ATTENDANCE_EMPLOYEE_SETTING_NOT_EXISTS;
import static cn.iocoder.yudao.module.attendance.enums.ErrorCodeConstants.ATTENDANCE_SCHEDULING_MANAGE_NOT_EXISTS;
import static cn.iocoder.yudao.module.employee.enums.ErrorCodeConstants.EMPLOYEE_INFO_NOT_EXISTS;

/**
 * 考勤员工设置 Service 实现类
 *
 * @author zhaopq
 */
@Service
@Validated
public class AttendanceEmployeeSettingServiceImpl implements AttendanceEmployeeSettingService {

    @Resource
    private AttendanceEmployeeSettingMapper employeeSettingMapper;
    @Resource
    private EmployeeInfoService employeeInfoService;

    @Override
    public Long createEmployeeSetting(AttendanceEmployeeSettingSaveReqVO createReqVO) {
        // 插入
        AttendanceEmployeeSettingDO employeeSetting = BeanUtils.toBean(createReqVO, AttendanceEmployeeSettingDO.class);
        employeeSettingMapper.insert(employeeSetting);
        // 返回
        return employeeSetting.getId();
    }

    @Override
    public void updateEmployeeSetting(AttendanceEmployeeSettingSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeSettingExists(updateReqVO.getId());
        // 更新
        AttendanceEmployeeSettingDO updateObj = BeanUtils.toBean(updateReqVO, AttendanceEmployeeSettingDO.class);
        employeeSettingMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmployeeSetting(Long id) {
        // 校验存在
        validateEmployeeSettingExists(id);
        // 删除
        employeeSettingMapper.deleteById(id);
    }

    private void validateEmployeeSettingExists(Long id) {
        if (employeeSettingMapper.selectById(id) == null) {
            throw exception(ATTENDANCE_EMPLOYEE_SETTING_NOT_EXISTS);
        }
    }

    @Override
    public AttendanceEmployeeSettingDO getEmployeeSetting(Long id) {
        return employeeSettingMapper.selectById(id);
    }

    @Override
    public PageResult<AttendanceEmployeeSettingDO> getEmployeeSettingPage(AttendanceEmployeeSettingPageReqVO pageReqVO) {
        return employeeSettingMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<AttendanceEmployeeSettingRespVO> getEmployeeInfoSettingPage(AttendanceEmployeeSettingPageReqVO pageReqVO) {
        // 获取用户信息和租户ID
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        Long tenantId = user != null && user.getTenantId() != null ? user.getTenantId() : 0L;
        pageReqVO.setTenantId(tenantId);
        int currentPage = pageReqVO.getPageNo();
        if (currentPage > 0) {
            pageReqVO.setPageNo(currentPage - 1);
        }
        List<AttendanceEmployeeSettingRespVO> list = employeeSettingMapper.getEmployeeInfoSettingPage(pageReqVO);
        return new PageResult<>(list, (long) list.size());
    }

    @Override
    public AttendanceEmployeeSettingRespVO getByEmployeeId(Long employeeId) {
        return employeeSettingMapper.getByEmployeeId(employeeId);
    }

    @Override
    public void batchUpdateEmployeeSetting(AttendanceEmployeeSettingBatchSaveReqVO updateReqVO) {
        List<Long> employeeIds = updateReqVO.getEmployeeIds();
        Long schedulingId = updateReqVO.getSchedulingId();
        if (schedulingId == null) {
            throw exception(ATTENDANCE_SCHEDULING_MANAGE_NOT_EXISTS);
        }
        for (Long employeeId : employeeIds) {
            // 校验存在
            boolean exists = validateEmployeeSettingExistsByEmployeeId(employeeId);
            if (exists) {
                // 更新
                AttendanceEmployeeSettingRespVO respVO = this.getByEmployeeId(employeeId);
                if (respVO == null) {
                    throw exception(ATTENDANCE_EMPLOYEE_SETTING_NOT_EXISTS);
                }
                AttendanceEmployeeSettingDO updateObj = BeanUtils.toBean(respVO, AttendanceEmployeeSettingDO.class);
                updateObj.setSchedulingId(schedulingId);
                employeeSettingMapper.updateById(updateObj);
            } else {
                // 先查询员工信息
                EmployeeInfoDO employeeInfoDO = employeeInfoService.getInfo(employeeId);
                if (employeeInfoDO == null) {
                    throw exception(EMPLOYEE_INFO_NOT_EXISTS);
                }
                // 新增
                AttendanceEmployeeSettingDO employeeSetting = new AttendanceEmployeeSettingDO();
                // 设置非空字段
                if (employeeInfoDO.getName() != null) {
                    employeeSetting.setName(employeeInfoDO.getName());
                }
                if (employeeInfoDO.getDeptId() != null) {
                    employeeSetting.setDeptId(employeeInfoDO.getDeptId());
                }
                employeeSetting.setSchedulingId(schedulingId);
                employeeSetting.setEmployeeId(employeeId);

                if (employeeInfoDO.getEmail() != null) {
                    employeeSetting.setEmail(employeeInfoDO.getEmail());
                }
                if (employeeInfoDO.getPhone() != null) {
                    employeeSetting.setPhoneNumber(employeeInfoDO.getPhone());
                }

                if (employeeInfoDO.getTenantId() != null) {
                    employeeSetting.setTenantId(employeeInfoDO.getTenantId());
                }

                employeeSettingMapper.insert(employeeSetting);
            }
        }
    }

    private boolean validateEmployeeSettingExistsByEmployeeId(Long employeeId) {
        return employeeSettingMapper.getByEmployeeId(employeeId) != null;
    }


}