package com.cf.imes.module.system.service.funds.purchase;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.date.LocalDateTimeUtils;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.OrgEarliestPurchaseProductRespVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.PurchaseRecordPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.PurchaseRecordRespVO;
import com.cf.imes.module.system.dal.dataobject.funds.purchase.PurchaseRecordDO;
import com.cf.imes.module.system.dal.mysql.funds.products.ProductsDetailMapper;
import com.cf.imes.module.system.dal.mysql.funds.purchase.PurchaseRecordMapper;
import com.cf.imes.module.system.enums.pay.InvoiceStatusEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 软件购买接口 实现类
 */
@Slf4j
@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Resource
    private PurchaseRecordMapper purchaseRecordMapper;

    @Resource
    private ProductsDetailMapper productsDetailMapper;

    @Override
    public PurchaseRecordDO getRecord(Long id) {
        return purchaseRecordMapper.selectById(id);
    }

    @Override
    public PageResult<PurchaseRecordRespVO> getPurchaseRecord(PurchaseRecordPageReqVO pageReqVO) {
        // 生成起时间0点和止时间12点的区间
        pageReqVO.setCreateTime(LocalDateTimeUtils.generateTimeSection(pageReqVO.getCreateTime()));

        PageResult<PurchaseRecordDO> pageResult = purchaseRecordMapper.selectPage(pageReqVO);
        List<PurchaseRecordDO> resultList = pageResult.getList();

        if (CollUtil.isEmpty(resultList)) {
            return new PageResult<>();
        }
        List<PurchaseRecordRespVO> respVOS = BeanUtils.toBean(resultList, PurchaseRecordRespVO.class);
        return new PageResult<>(respVOS, pageResult.getTotal());
    }

    @Override
    public void createRecord(PurchaseRecordDO purchaseRecordDO) {
        purchaseRecordMapper.insert(purchaseRecordDO);
    }

    @Override
    public void updateRecordInvoicable(Long purchaseRecordId, Long organId) {
        purchaseRecordMapper.update(new LambdaUpdateWrapper<PurchaseRecordDO>()
                .eq(PurchaseRecordDO::getId, purchaseRecordId)
                .eq(PurchaseRecordDO::getOrganId, organId)
                .set(PurchaseRecordDO::getIsInvocing, InvoiceStatusEnum.INVOICABLE.getCode()));
    }

    @Override
    public OrgEarliestPurchaseProductRespVO getOrgEarliestProductDetail(Long organId) {
        // 查询购买记录中还有效的最早一条
        PurchaseRecordDO purchaseRecordDO = purchaseRecordMapper.selectOne(new LambdaQueryWrapper<PurchaseRecordDO>()
                .eq(PurchaseRecordDO::getOrganId, organId)
                .gt(PurchaseRecordDO::getEndTime, LocalDateTime.now())
                .orderByAsc(PurchaseRecordDO::getCreateTime)
                .last("limit 1"));
        if (ObjectUtil.isNotNull(purchaseRecordDO)) {
            return new OrgEarliestPurchaseProductRespVO(purchaseRecordDO, productsDetailMapper.selectById(purchaseRecordDO.getProductDetailsId()));
        }
        return null;
    }

    @Override
    public BigDecimal getInvoicingAmount(Long organId) {
        return purchaseRecordMapper.getInvoicingAmount(organId);
    }
}
