package com.trade.service.trade.salesorder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trade.common.core.page.GridPage;
import com.trade.service.TransStruct;
import com.trade.service.trade.salesorder.domain.CustomsDeclarationGoods;
import com.trade.service.trade.salesorder.mapper.CustomsDeclarationGoodsMapper;
import com.trade.service.trade.salesorder.service.ICustomsDeclarationGoodsService;
import com.trade.service.thirdparty.xiaotu.dto.CustomsDeclarationResponse;
import com.trade.service.trade.salesorder.vo.CustomsDeclarationGoodsBelongsVO;
import com.trade.service.trade.salesorder.vo.CustomsDeclarationGoodsDtlVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 报关单商品 服务实现类
 * </p>
 *
 * @author zhangchenm
 * @since 2024-09-20
 */
@Service
public class CustomsDeclarationGoodsServiceImpl extends ServiceImpl<CustomsDeclarationGoodsMapper, CustomsDeclarationGoods> implements ICustomsDeclarationGoodsService {

    @Resource
    private TransStruct transStruct;

    @Override
    public void addCustomsDeclarationGoods(CustomsDeclarationResponse customsDeclarationResponse) {
        CustomsDeclarationGoods customsDeclarationGoods = transStruct
                .transXTResponseToCustomsDeclarationGoods(customsDeclarationResponse);
        this.save(customsDeclarationGoods);
    }

    @Override
    public List<CustomsDeclarationGoodsDtlVO> listWithCustomsDeclarationDtlByOrderId(Long orderId) {
        return this.baseMapper.listWithCustomsDeclarationDtlByOrderId(orderId);
    }

    @Override
    public void updateCustomsDeclarationGoods(CustomsDeclarationResponse customsDeclarationResponse, Long id) {
        CustomsDeclarationGoods customsDeclarationGoods = transStruct
                .transXTResponseToCustomsDeclarationGoods(customsDeclarationResponse);
        customsDeclarationGoods.setId(id);
        this.updateById(customsDeclarationGoods);
    }

    @Override
    public CustomsDeclarationGoods getByCkbgdh(String ckbgdh) {
        return this.lambdaQuery().eq(CustomsDeclarationGoods::getCkbgdh, ckbgdh).one();
    }

    @Override
    public List<CustomsDeclarationGoods> getByBgdh(String bgdhgbh) {
        return this.lambdaQuery().eq(CustomsDeclarationGoods::getBgdhgbh, bgdhgbh).list();
    }

    @Override
    public void updateTsjltbbsDoneByIds(List<Long> ids) {
        this.lambdaUpdate().in(CustomsDeclarationGoods::getId, ids).set(CustomsDeclarationGoods::getTsjltbbs, true)
                .update();
    }

    @Override
    public GridPage<CustomsDeclarationGoodsBelongsVO> getAllNeedPullTaxRebateRecords(int pageNum, int pageSize) {
        Page<CustomsDeclarationGoodsBelongsVO> page = PageHelper.startPage(pageNum, pageSize);
        List<CustomsDeclarationGoodsBelongsVO> rows = this.baseMapper.getAllNeedPullTaxRebateRecords();
        return new GridPage<>(pageNum, pageSize, page.getTotal(), rows);
    }
}
