package cn.iocoder.yudao.module.cash.service.refundinfo;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.cash.controller.admin.refundinfo.vo.RefundInfoPageReqVO;
import cn.iocoder.yudao.module.cash.controller.admin.refundinfo.vo.RefundInfoRespVO;
import cn.iocoder.yudao.module.cash.controller.admin.refundinfo.vo.RefundInfoSaveReqVO;
import cn.iocoder.yudao.module.cash.dal.dataobject.refundinfo.RefundInfoDO;
import cn.iocoder.yudao.module.cash.dal.mysql.refundinfo.RefundInfoMapper;
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 java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cash.enums.ErrorCodeConstants.REFUND_INFO_NOT_EXISTS;

/**
 * 回款信息 Service 实现类
 *
 * @author dp
 */
@Service
@Validated
public class RefundInfoServiceImpl implements RefundInfoService {

    @Resource
    private RefundInfoMapper refundInfoMapper;

    @Resource
    private EmployeeApi employeeApi;

    @Resource
    private FileApi fileApi;

    @Override
    public Long createRefundInfo(RefundInfoSaveReqVO createReqVO) {
        // 获取当前登录用户ID
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        // 根据用户ID获取当前登录员工信息
        EmployeeRespDTO loginEmployee = employeeApi.getEmployeeByUserId(loginUserId);

        // 将创建请求对象转换为回款信息对象
        RefundInfoDO refundInfo = BeanUtils.toBean(createReqVO, RefundInfoDO.class);
        // 创建一个日期时间格式化器，用于格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 获取当前时间并格式化，然后拼接上随机UUID作为回款信息的唯一标识
        String format = formatter.format(LocalDateTime.now());
        refundInfo.setRefundInfoUuid(format + IdUtil.fastSimpleUUID());
        // 设置回款信息的状态为"0"，表示已完成
        refundInfo.setStatus("0");
        // 设置回款信息的创建者为当前登录员工的ID
        refundInfo.setCreator(String.valueOf(loginEmployee.getId()));
        // 插入回款信息到数据库
        refundInfoMapper.insert(refundInfo);
        // 附件
        fileApi.updateFileBiz(createReqVO.getFileIdList(), refundInfo.getRefundInfoUuid());
        // 返回创建的回款信息的ID
        return refundInfo.getId();
    }

    @Override
    public void updateRefundInfo(RefundInfoSaveReqVO updateReqVO) {
        // 校验存在
        RefundInfoDO refundInfoDO = validateRefundInfoExists(updateReqVO.getId());
        // 更新
        RefundInfoDO updateObj = BeanUtils.toBean(updateReqVO, RefundInfoDO.class);
        // 附件
        fileApi.updateFileBiz(updateReqVO.getFileIdList(), refundInfoDO.getRefundInfoUuid());
        refundInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteRefundInfo(Long id) {
        // 校验存在
        validateRefundInfoExists(id);
        // 删除
        refundInfoMapper.deleteById(id);
    }

    private RefundInfoDO validateRefundInfoExists(Long id) {
        RefundInfoDO refundInfoDO = refundInfoMapper.selectById(id);
        if (refundInfoDO == null) {
            throw exception(REFUND_INFO_NOT_EXISTS);
        }
        return refundInfoDO;
    }

    @Override
    public RefundInfoDO getRefundInfo(Long id) {
        return refundInfoMapper.selectById(id);
    }

    @Override
    public PageResult<RefundInfoRespVO> getRefundInfoPage(RefundInfoPageReqVO pageReqVO) {
        pageReqVO.setPageNo((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize());

        long pageCount = -1;
        if (!PageParam.PAGE_SIZE_NONE.equals(pageReqVO.getPageSize())) {
            pageCount = refundInfoMapper.selectPageCount(pageReqVO);
        }
        List<RefundInfoRespVO> pageList = refundInfoMapper.selectPageList(pageReqVO);

        return new PageResult<>(pageList, pageCount);
    }

}