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

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.attendance.api.business.dto.AttendanceBusinessCreateReqDTO;
import cn.iocoder.yudao.module.employee.api.EmployeeApi;
import cn.iocoder.yudao.module.employee.api.dto.EmployeeRespDTO;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.attendance.controller.admin.business.vo.*;
import cn.iocoder.yudao.module.attendance.dal.dataobject.business.AttendanceBusinessDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.attendance.dal.mysql.business.AttendanceBusinessMapper;

import javax.annotation.Resource;

import java.time.LocalDate;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.attendance.enums.ErrorCodeConstants.*;

/**
 * 出差信息 Service 实现类
 *
 * @author zhaopq
 */
@Service
@Validated
public class AttendanceBusinessServiceImpl implements AttendanceBusinessService {

    @Resource
    private AttendanceBusinessMapper businessMapper;
    @Resource
    private EmployeeApi employeeApi;
    @Resource
    private FileApi fileApi;

    @Override
    public Long createBusiness(AttendanceBusinessSaveReqVO createReqVO) {
        // 获取用户信息和租户ID
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        Long tenantId = user != null && user.getTenantId() != null ? user.getTenantId() : 0L;
        Long userId = user != null && user.getId() != null ? user.getId() : 0L;
        createReqVO.setTenantId(tenantId);
        // 出差人信息
        EmployeeRespDTO employee = employeeApi.getEmployeeById(createReqVO.getEmployeeId());
        createReqVO.setEmployeeId(employee.getId());
        createReqVO.setEmployeeName(employee.getName());
        createReqVO.setEmployeePhone(employee.getPhone());
        createReqVO.setDeptId(employee.getDeptId());
        createReqVO.setPosition(employee.getPosition());
        // 登录员工信息
        EmployeeRespDTO loginEmployee = employeeApi.getEmployeeByUserId(userId);// 判空检查
        if (loginEmployee != null) {
            createReqVO.setCreatorEmployeeId(loginEmployee.getId());
            createReqVO.setCreatorEmployeeName(loginEmployee.getName());
        }
        // 插入
        String infoId = IdUtil.fastSimpleUUID();
        createReqVO.setBusinessId(infoId);
        AttendanceBusinessDO business = BeanUtils.toBean(createReqVO, AttendanceBusinessDO.class);
        businessMapper.insert(business);
        // 保存业务uuid到附件中
        fileApi.updateFileBiz(createReqVO.getFileIdList(), infoId);
        // 返回
        return business.getId();
    }

    @Override
    public Long createBusinessInfoAfterAudit(AttendanceBusinessCreateReqDTO businessCreateReqDTO) {
        AttendanceBusinessDO attendanceBusinessDO = BeanUtils.toBean(businessCreateReqDTO, AttendanceBusinessDO.class);
        businessMapper.insert(attendanceBusinessDO);
        return attendanceBusinessDO.getId();
    }

    @Override
    public void updateBusiness(AttendanceBusinessSaveReqVO updateReqVO) {
        // 校验存在
        validateBusinessExists(updateReqVO.getId());
        // 保存业务uuid到附件中
        fileApi.updateFileBiz(updateReqVO.getFileIdList(), updateReqVO.getBusinessId());
        // 出差人信息
        EmployeeRespDTO employee = employeeApi.getEmployeeById(updateReqVO.getEmployeeId());
        updateReqVO.setEmployeeId(employee.getId());
        updateReqVO.setEmployeeName(employee.getName());
        updateReqVO.setEmployeePhone(employee.getPhone());
        updateReqVO.setDeptId(employee.getDeptId());
        updateReqVO.setPosition(employee.getPosition());
        // 更新
        AttendanceBusinessDO updateObj = BeanUtils.toBean(updateReqVO, AttendanceBusinessDO.class);
        businessMapper.updateById(updateObj);
    }

    @Override
    public void deleteBusiness(Long id) {
        // 校验存在
        validateBusinessExists(id);
        // 删除
        businessMapper.deleteById(id);
    }

    private void validateBusinessExists(Long id) {
        if (businessMapper.selectById(id) == null) {
            throw exception(ATTENDANCE_BUSINESS_NOT_EXISTS);
        }
    }

    @Override
    public AttendanceBusinessDO getBusiness(Long id) {
        return businessMapper.selectById(id);
    }

    @Override
    public PageResult<AttendanceBusinessDO> getBusinessPage(AttendanceBusinessPageReqVO pageReqVO) {
        return businessMapper.selectPage(pageReqVO);
    }

    @Override
    public AttendanceBusinessDO selectByDateAndEmployeeId(LocalDate date, Long employeeId) {
        return businessMapper.selectByDateAndEmployeeId(date, employeeId);
    }


}