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

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.attendance.api.out.dto.AttendanceOutCreateReqDTO;
import cn.iocoder.yudao.module.attendance.controller.admin.out.vo.AttendanceOutPageReqVO;
import cn.iocoder.yudao.module.attendance.controller.admin.out.vo.AttendanceOutSaveReqVO;
import cn.iocoder.yudao.module.attendance.dal.dataobject.out.AttendanceOutDO;
import cn.iocoder.yudao.module.attendance.dal.mysql.out.AttendanceOutMapper;
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 javax.annotation.Resource;

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

/**
 * 公务外出信息 Service 实现类
 *
 * @author zhaopq
 */
@Service
@Validated
public class AttendanceOutServiceImpl implements AttendanceOutService {

    @Resource
    private AttendanceOutMapper outMapper;
    @Resource
    private EmployeeApi employeeApi;
    @Resource
    private FileApi fileApi;

    @Override
    public Long createOut(AttendanceOutSaveReqVO 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.setOutId(infoId);
        AttendanceOutDO out = BeanUtils.toBean(createReqVO, AttendanceOutDO.class);
        outMapper.insert(out);
        // 保存业务uuid到附件中
        fileApi.updateFileBiz(createReqVO.getFileIdList(), infoId);
        // 返回
        return out.getId();
    }

    @Override
    public Long createOutInfoAfterAudit(AttendanceOutCreateReqDTO outCreateReqDTO) {
        AttendanceOutDO attendanceOutDO = BeanUtils.toBean(outCreateReqDTO, AttendanceOutDO.class);
        outMapper.insert(attendanceOutDO);
        return attendanceOutDO.getId();
    }

    @Override
    public void updateOut(AttendanceOutSaveReqVO updateReqVO) {
        // 校验存在
        validateOutExists(updateReqVO.getId());
        // 保存业务uuid到附件中
        fileApi.updateFileBiz(updateReqVO.getFileIdList(), updateReqVO.getOutId());
        // 公务外出人信息
        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());
        // 更新
        AttendanceOutDO updateObj = BeanUtils.toBean(updateReqVO, AttendanceOutDO.class);
        outMapper.updateById(updateObj);
    }

    @Override
    public void deleteOut(Long id) {
        // 校验存在
        validateOutExists(id);
        // 删除
        outMapper.deleteById(id);
    }

    private void validateOutExists(Long id) {
        if (outMapper.selectById(id) == null) {
            throw exception(ATTENDANCE_OUT_NOT_EXISTS);
        }
    }

    @Override
    public AttendanceOutDO getOut(Long id) {
        return outMapper.selectById(id);
    }

    @Override
    public PageResult<AttendanceOutDO> getOutPage(AttendanceOutPageReqVO pageReqVO) {
        return outMapper.selectPage(pageReqVO);
    }

}