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

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.paymentinfo.vo.PaymentInfoPageReqVO;
import cn.iocoder.yudao.module.cash.controller.admin.paymentinfo.vo.PaymentInfoRespVO;
import cn.iocoder.yudao.module.cash.controller.admin.paymentinfo.vo.PaymentInfoSaveReqVO;
import cn.iocoder.yudao.module.cash.dal.dataobject.paymentinfo.PaymentInfoDO;
import cn.iocoder.yudao.module.cash.dal.mysql.paymentinfo.PaymentInfoMapper;
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.PAYMENT_INFO_NOT_EXISTS;

/**
 * 付款信息 Service 实现类
 *
 * @author dp
 */
@Service
@Validated
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Resource
    private PaymentInfoMapper paymentInfoMapper;

    @Resource
    private EmployeeApi employeeApi;

    @Resource
    private FileApi fileApi;

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

        // 将请求对象转换为付款信息实体对象
        PaymentInfoDO paymentInfo = BeanUtils.toBean(createReqVO, PaymentInfoDO.class);

        // 生成付款编号：当前日期（年月日）+唯一标识符
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String format = formatter.format(LocalDateTime.now());
        paymentInfo.setPaymentInfoUuid(format + IdUtil.fastSimpleUUID());

        // 设置付款信息状态为“已完成”
        paymentInfo.setStatus("0");

        // 设置付款信息的创建人为当前登录员工
        paymentInfo.setCreator(String.valueOf(loginEmployee.getId()));

        // 插入付款信息到数据库
        paymentInfoMapper.insert(paymentInfo);

        // 更新附件业务信息，将附件关联到对应的付款信息
        fileApi.updateFileBiz(createReqVO.getFileIdList(), paymentInfo.getPaymentInfoUuid());

        // 返回创建的付款信息的ID
        return paymentInfo.getId();
    }

    @Override
    public void updatePaymentInfo(PaymentInfoSaveReqVO updateReqVO) {
        // 校验存在
        PaymentInfoDO paymentInfoDO = validatePaymentInfoExists(updateReqVO.getId());
        // 更新
        PaymentInfoDO updateObj = BeanUtils.toBean(updateReqVO, PaymentInfoDO.class);
        // 附件
        fileApi.updateFileBiz(updateReqVO.getFileIdList(), paymentInfoDO.getPaymentInfoUuid());
        paymentInfoMapper.updateById(updateObj);
    }

    @Override
    public void deletePaymentInfo(Long id) {
        // 校验存在
        validatePaymentInfoExists(id);
        // 删除
        paymentInfoMapper.deleteById(id);
    }

    private PaymentInfoDO validatePaymentInfoExists(Long id) {
        PaymentInfoDO paymentInfoDO = paymentInfoMapper.selectById(id);
        if (paymentInfoDO == null) {
            throw exception(PAYMENT_INFO_NOT_EXISTS);
        }
        return paymentInfoDO;
    }

    @Override
    public PaymentInfoDO getPaymentInfo(Long id) {
        return paymentInfoMapper.selectById(id);
    }

    @Override
    public PageResult<PaymentInfoRespVO> getPaymentInfoPage(PaymentInfoPageReqVO pageReqVO) {
        pageReqVO.setPageNo((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize());

        long pageCount = -1;
        if (!PageParam.PAGE_SIZE_NONE.equals(pageReqVO.getPageSize())) {
            pageCount = paymentInfoMapper.selectPageCount(pageReqVO);
        }
        List<PaymentInfoRespVO> pageList =  paymentInfoMapper.selectPageList(pageReqVO);
        return new PageResult<>(pageList, pageCount);
    }

}