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

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.utils.Assert;
import com.fjhx.purchase.entity.purchase.dto.PurchasePayRecordDetailSelectDto;
import com.fjhx.purchase.entity.purchase.po.Purchase;
import com.fjhx.purchase.entity.purchase.po.PurchasePayRecordDetail;
import com.fjhx.purchase.entity.purchase.vo.PurchasePayRecordDetailVo;
import com.fjhx.purchase.mapper.purchase.PurchasePayRecordDetailMapper;
import com.fjhx.purchase.service.purchase.PurchasePayRecordDetailService;
import com.fjhx.purchase.service.purchase.PurchasePayRecordService;
import com.fjhx.purchase.service.purchase.PurchaseService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 采购付款记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-23
 */
@Service
public class PurchasePayRecordDetailServiceImpl extends ServiceImpl<PurchasePayRecordDetailMapper, PurchasePayRecordDetail> implements PurchasePayRecordDetailService {

    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchasePayRecordService purchasePayRecordService;

    @Override
    public Page<PurchasePayRecordDetailVo> getPage(PurchasePayRecordDetailSelectDto dto) {
        IWrapper<PurchasePayRecordDetail> wrapper = getWrapper();
        wrapper.eq(PurchasePayRecordDetail::getPurchaseId, dto.getPurchaseId());
        wrapper.orderByDesc("pprd", PurchasePayRecordDetail::getId);
        Page<PurchasePayRecordDetailVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        UserUtil.assignmentNickName(page.getRecords(), PurchasePayRecordDetailVo::getCreateUser, PurchasePayRecordDetailVo::setPayUserName);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void empty(PurchasePayRecordDetailSelectDto dto) {
        Assert.notEmpty(dto.getPurchaseId(), "采购id不能为空");
        //修改付款状态未未付款
        Purchase purchase = purchaseService.getById(dto.getPurchaseId());
        purchase.setPayStatus(0);
        String victoriatouristJson = purchase.getVictoriatouristJson();
        JSONObject json = ObjectUtil.isEmpty(victoriatouristJson) ? new JSONObject() : JSONObject.parseObject(victoriatouristJson);
        json.put("paidAmount", BigDecimal.ZERO);
        purchase.setVictoriatouristJson(JSONObject.toJSONString(json,JSONWriter.Feature.WriteLongAsString));
        purchaseService.updateById(purchase);

        //清空付款记录记录信息
        List<PurchasePayRecordDetail> list = this.list(q -> q.eq(PurchasePayRecordDetail::getPurchaseId, dto.getPurchaseId()));
        if (ObjectUtils.isNotEmpty(list)) {
            List<Long> purchasePayRecordIds = list.stream().map(PurchasePayRecordDetail::getPurchasePayRecordId).collect(Collectors.toList());
            purchasePayRecordService.removeBatchByIds(purchasePayRecordIds);
        }
        //清空付款明细信息
        this.remove(q -> q.eq(PurchasePayRecordDetail::getPurchaseId, dto.getPurchaseId()));
    }

}
