package com.fjhx.purchase.service.purchase.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.utils.Assert;
import com.fjhx.purchase.entity.purchase.PurchaseJson;
import com.fjhx.purchase.entity.purchase.dto.PurchaseRefundRecordDto;
import com.fjhx.purchase.entity.purchase.po.Purchase;
import com.fjhx.purchase.entity.purchase.po.PurchaseRefundRecord;
import com.fjhx.purchase.entity.purchase.vo.PurchaseRefundRecordVo;
import com.fjhx.purchase.mapper.purchase.PurchaseRefundRecordMapper;
import com.fjhx.purchase.service.purchase.PurchaseRefundRecordService;
import com.fjhx.purchase.service.purchase.PurchaseService;
import com.fjhx.supply.entity.supplier.po.SupplierInfo;
import com.fjhx.supply.service.supplier.SupplierInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 采购退款记录 维多利亚 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-28
 */
@Service
public class PurchaseRefundRecordServiceImpl extends ServiceImpl<PurchaseRefundRecordMapper, PurchaseRefundRecord> implements PurchaseRefundRecordService {

    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private SupplierInfoService supplierInfoService;

    @Override
    public void add(PurchaseRefundRecordDto purchaseRefundRecordDto) {
        this.save(purchaseRefundRecordDto);
    }

    @Override
    public PurchaseRefundRecordVo detail(PurchaseRefundRecordDto dto) {
        Assert.notEmpty(dto.getId(), "退款id不能为空");
        PurchaseRefundRecord purchaseRefundRecord = getById(dto.getId());
        PurchaseRefundRecordVo purchaseRefundRecordVo = BeanUtil.copyProperties(purchaseRefundRecord, PurchaseRefundRecordVo.class);
        Purchase purchase = purchaseService.getById(purchaseRefundRecordVo.getPurchaseId());
        PurchaseJson purchaseJson = JSONObject.parseObject(purchase.getVictoriatouristJson(), PurchaseJson.class);
        purchaseRefundRecordVo.setContractCode(purchaseJson.getContractCode());
        SupplierInfo supplierInfo = supplierInfoService.getById(purchase.getSupplyId());
        purchaseRefundRecordVo.setSupplierName(supplierInfo.getName());
        return purchaseRefundRecordVo;
    }

}
