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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.entity.corporation.po.Corporation;
import com.fjhx.common.service.corporation.CorporationService;
import com.fjhx.common.utils.Assert;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.sale.entity.contract.vo.ContractPdfInfoVo;
import com.fjhx.sale.entity.documents.po.Documents;
import com.fjhx.sale.entity.intermediate.dto.IntermediateContractDto;
import com.fjhx.sale.entity.intermediate.dto.IntermediateContractSelectDto;
import com.fjhx.sale.entity.intermediate.po.IntermediateContract;
import com.fjhx.sale.entity.intermediate.vo.IntermediateContractVo;
import com.fjhx.sale.mapper.intermediate.IntermediateContractMapper;
import com.fjhx.sale.service.contract.ContractService;
import com.fjhx.sale.service.documents.DocumentsService;
import com.fjhx.sale.service.intermediate.IntermediateContractService;
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 java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 中间合同 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-28
 */
@Service
public class IntermediateContractServiceImpl extends ServiceImpl<IntermediateContractMapper, IntermediateContract> implements IntermediateContractService {

    @Autowired
    private DocumentsService documentsService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CorporationService corporationService;

    @Override
    public Page<IntermediateContractVo> getPage(IntermediateContractSelectDto dto) {
        IWrapper<IntermediateContract> wrapper = getWrapper();
        //买方公司过滤
        wrapper.eq(IntermediateContract::getBuyCorporationId, dto.getBuyCorporationId());
        //卖方公司过滤
        wrapper.eq(IntermediateContract::getSellCorporationId, dto.getSellCorporationId());

        wrapper.orderByDesc("ic", IntermediateContract::getId);
        Page<IntermediateContractVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<IntermediateContractVo> records = page.getRecords();
        //赋值原买方公司名称 客户
        customerService.attributeAssign(records, IntermediateContractVo::getOldBuyCorporationId, (item, customer) -> {
            item.setOldBuyCorporationName(customer.getName());
        });
        //赋值原卖方公司名称 多公司
        corporationService.attributeAssign(records, IntermediateContractVo::getOldSellCorporationId, (item, corporation) -> {
            item.setOldSellCorporationName(corporation.getName());
        });
        //赋值中间合同重新赋值过后的买方名称
        corporationService.attributeAssign(records, IntermediateContractVo::getBuyCorporationId, (item, corporation) -> {
            item.setBuyCorporationName(corporation.getName());
        });
        //赋值中间合同重新赋值过后的卖方名称
        corporationService.attributeAssign(records, IntermediateContractVo::getSellCorporationId, (item, corporation) -> {
            item.setSellCorporationName(corporation.getName());
        });
        //赋值最近操作用户
        UserUtil.assignmentNickName(records, IntermediateContractVo::getUpdateUser, IntermediateContractVo::setOpUserName);
        return page;
    }

    @Override
    public IntermediateContractVo detail(Long id) {
        IntermediateContract IntermediateContract = this.getById(id);
        IntermediateContractVo result = BeanUtil.toBean(IntermediateContract, IntermediateContractVo.class);
        return result;
    }

    @Override
    public void edit(IntermediateContractDto intermediateContractDto) {
        intermediateContractDto.setId(null);
        Assert.notEmpty(intermediateContractDto.getDocumentsId(), "单证id不能为空");
        IntermediateContract intermediateContract = this.getOne(q -> q.eq(IntermediateContract::getDocumentsId, intermediateContractDto.getDocumentsId()));
        if (ObjectUtil.isNotEmpty(intermediateContract)) {
            //编辑 赋值id
            intermediateContractDto.setId(intermediateContract.getId());
        }
        this.saveOrUpdate(intermediateContractDto);
    }

    /**
     * 获取中间合同历史联系方式
     */
    @Override
    public List<IntermediateContractVo> contactList(IntermediateContractDto dto) {
        IWrapper<IntermediateContract> wrapper = IWrapper.getWrapper();
        //根据卖方联系人名称/联系方式过滤
        if (ObjectUtil.isNotEmpty(dto.getSellContactName())) {
            wrapper.or().like(IntermediateContract::getSellContactName, dto.getSellContactName());
            wrapper.groupBy("sell_contact_name");
        }
        if (ObjectUtil.isNotEmpty(dto.getSellContactNumber())) {
            wrapper.or().like(IntermediateContract::getSellContactNumber, dto.getSellContactNumber());
            wrapper.groupBy("sell_contact_number");
        }
        //根据买方联系人名称/联系方式过滤
        if (ObjectUtil.isNotEmpty(dto.getBuyContactName())) {
            wrapper.or().like(IntermediateContract::getBuyContactName, dto.getBuyContactName());
            wrapper.groupBy("buy_contact_name");
        }
        if (ObjectUtil.isNotEmpty(dto.getBuyContactNumber())) {
            wrapper.or().like(IntermediateContract::getBuyContactNumber, dto.getBuyContactNumber());
            wrapper.groupBy("buy_contact_number");
        }
        List<IntermediateContractVo> list = baseMapper.contactList(wrapper);
        //赋值买方名称
        corporationService.attributeAssign(list, IntermediateContractVo::getBuyCorporationId, (item, corporation) -> {
            item.setBuyCorporationName(corporation.getName());
        });
        //赋值卖方名称
        corporationService.attributeAssign(list, IntermediateContractVo::getSellCorporationId, (item, corporation) -> {
            item.setSellCorporationName(corporation.getName());
        });
        return list;
    }

    /**
     * 获取打印信息
     *
     * @return
     */
    @Override
    public ContractPdfInfoVo getPrintInfo(IntermediateContractDto dto) {
        Assert.notEmpty(dto.getDocumentsId(), "单证id不能为空");
        Documents documents = documentsService.getById(dto.getDocumentsId());
        Assert.notEmpty(documents, "查询不到单证信息");
        ContractPdfInfoVo contractPdfInfo = contractService.getContractPdfInfo(documents.getContractId());
        //重新赋值买卖方信息为中间合同信息
        //获得多公司信息
        IntermediateContract intermediateContract = getOne(q -> q.eq(IntermediateContract::getDocumentsId, documents.getId()));
        if (ObjectUtil.isNotEmpty(intermediateContract)) {
            List<Long> corporationIds = Arrays.asList(intermediateContract.getBuyCorporationId(), intermediateContract.getSellCorporationId());
            Map<Long, Corporation> corporationMap = corporationService.mapKEntity(Corporation::getId, q -> q.in(Corporation::getId, corporationIds));

            Corporation buyCorporation = corporationMap.get(intermediateContract.getBuyCorporationId());
            //赋值买方信息
            contractPdfInfo.setBuyContactNumber(intermediateContract.getBuyContactName());
            contractPdfInfo.setBuyCorporationName(intermediateContract.getBuyContactNumber());
            if (ObjectUtil.isNotEmpty(buyCorporation)) {
                contractPdfInfo.setBuyContactName(buyCorporation.getNameEn());
                //赋值买方国家省市详细地址信息
                contractPdfInfo.setBuyCountryName(buyCorporation.getCountryEnStr());
                contractPdfInfo.setBuyProvinceName(buyCorporation.getProvinceEnStr());
                contractPdfInfo.setBuyCityName(buyCorporation.getCityEnStr());
                contractPdfInfo.setBuyDetailedAddress(buyCorporation.getAddressEn());
            }

            Corporation sellCorporation = corporationMap.get(intermediateContract.getSellCorporationId());
            //赋值卖方信息
            contractPdfInfo.setSellContactNumber(intermediateContract.getSellContactName());
            contractPdfInfo.setSellCorporationNameEn(intermediateContract.getSellContactNumber());
            if (ObjectUtil.isNotEmpty(sellCorporation)) {
                contractPdfInfo.setSellContactName(sellCorporation.getNameEn());
                //赋值卖方国家省市详细地址信息
                contractPdfInfo.setSellCountryName(sellCorporation.getCountryEnStr());
                contractPdfInfo.setSellProvinceName(sellCorporation.getProvinceEnStr());
                contractPdfInfo.setSellCityName(sellCorporation.getCityEnStr());
                contractPdfInfo.setSellDetailedAddress(sellCorporation.getAddressEn());
            }
        }
        return contractPdfInfo;
    }

}
