package com.fjhx.sale.service.documents.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.sale.entity.documents.dto.DocumentsPdfDto;
import com.fjhx.sale.entity.documents.dto.DocumentsPdfSelectDto;
import com.fjhx.sale.entity.documents.po.DocumentsPdf;
import com.fjhx.sale.entity.documents.po.DocumentsProduct;
import com.fjhx.sale.entity.documents.vo.DocumentsPdfVo;
import com.fjhx.sale.mapper.documents.DocumentsPdfMapper;
import com.fjhx.sale.service.documents.DocumentsPdfService;
import com.fjhx.sale.service.documents.DocumentsProductService;
import com.fjhx.sale.service.documents.DocumentsService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * <p>
 * 单证PDF数据存放表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-26
 */
@Service
public class DocumentsPdfServiceImpl extends ServiceImpl<DocumentsPdfMapper, DocumentsPdf> implements DocumentsPdfService {

    @Autowired
    private DocumentsProductService documentsProductService;

    @Autowired
    private DocumentsService documentsService;

    @Override
    public Page<DocumentsPdfVo> getPage(DocumentsPdfSelectDto dto) {
        IWrapper<DocumentsPdf> wrapper = getWrapper();
        wrapper.orderByDesc("dp", DocumentsPdf::getId);
        Page<DocumentsPdfVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    /**
     * 查询详情
     *
     * @param id 单证ID
     * @return
     */
    @Override
    public DocumentsPdfVo detail(Long id) {
        DocumentsPdf documentsPdf = this.getOne(Wrappers.<DocumentsPdf>query().lambda().eq(DocumentsPdf::getDocumentId, id).eq(DocumentsPdf::getDataType, 1));
        DocumentsPdfVo result = BeanUtil.toBean(documentsPdf, DocumentsPdfVo.class);
        return result;
    }

    /**
     * 报关委托书详情
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> powerDetail(Long id) {
        DocumentsPdf documentsPdf = this.getOne(Wrappers.<DocumentsPdf>query().lambda().eq(DocumentsPdf::getDocumentId, id).eq(DocumentsPdf::getDataType, 1));
        DocumentsPdfVo result = BeanUtil.toBean(documentsPdf, DocumentsPdfVo.class);
        if (ObjectUtil.isEmpty(result)) {
            Map<String, Object> map = documentsService.getContractDataById(id);
            //查询单证数据
            DocumentsProduct documentsProduct = documentsProductService.getMaxMoneyData(id);
            if (MapUtils.isEmpty(map) || ObjectUtil.isEmpty(documentsProduct)) {
                throw new ServiceException("数据异常,请联系管理员");
            }
            map.put("customerCode", documentsProduct.getCustomsCode());
            map.put("productName", StringUtils.equals(documentsProduct.getDescribes(), "无") ? documentsProduct.getSubDescribe() : documentsProduct.getDescribes());
            map.put("productQuantity", documentsProduct.getQuantity());
            return map;
        } else {
            Map<String, Object> map = JSON.parseObject(result.getContent());
            return map;
        }
    }


    @Override
    public void add(DocumentsPdfDto documentsPdfDto) {
        DocumentsPdf documentsPdf = this.getOne(Wrappers.<DocumentsPdf>query().lambda().eq(DocumentsPdf::getDocumentId, documentsPdfDto.getDocumentId()));
        if (ObjectUtil.isEmpty(documentsPdf)) {
            this.save(documentsPdfDto);
        } else {
            this.update(documentsPdfDto, Wrappers.<DocumentsPdf>query().lambda().eq(DocumentsPdf::getDocumentId, documentsPdfDto.getDocumentId()));
        }
    }

    @Override
    public void edit(DocumentsPdfDto documentsPdfDto) {
        this.updateById(documentsPdfDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
