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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
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.file.entity.FileInfoVo;
import com.fjhx.file.entity.ObsFile;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.item.entity.product.dto.ProductCustomInfoDto;
import com.fjhx.item.entity.product.dto.ProductInfoDto;
import com.fjhx.item.entity.product.po.ProductBomDetail;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.sale.entity.ext.dto.ExtQuotationProductSelectDto;
import com.fjhx.sale.entity.ext.po.ExtQuotation;
import com.fjhx.sale.entity.ext.po.ExtQuotationProduct;
import com.fjhx.sale.entity.ext.po.ExtQuotationProductBom;
import com.fjhx.sale.entity.ext.po.ExtQuotationProductCustomInfo;
import com.fjhx.sale.entity.ext.vo.ExtQuotationProductBomVo;
import com.fjhx.sale.entity.ext.vo.ExtQuotationProductCustomInfoVo;
import com.fjhx.sale.entity.ext.vo.ExtQuotationProductVo;
import com.fjhx.sale.mapper.ext.ExtQuotationProductMapper;
import com.fjhx.sale.service.ext.ExtQuotationProductBomService;
import com.fjhx.sale.service.ext.ExtQuotationProductCustomInfoService;
import com.fjhx.sale.service.ext.ExtQuotationProductService;
import com.fjhx.sale.service.ext.ExtQuotationService;
import com.ruoyi.common.core.domain.BasePo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 对外报价单产品 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-14
 */
@Service
public class ExtQuotationProductServiceImpl extends ServiceImpl<ExtQuotationProductMapper, ExtQuotationProduct> implements ExtQuotationProductService {

    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ExtQuotationProductBomService extQuotationProductBomService;
    @Autowired
    private ExtQuotationProductCustomInfoService extQuotationProductCustomInfoService;
    @Autowired
    private ExtQuotationProductService extQuotationProductService;
    @Autowired
    private ExtQuotationService extQuotationService;

    @Override
    public Page<ExtQuotationProductVo> waitCreateProductPage(ExtQuotationProductSelectDto dto) {
        IWrapper<ExtQuotationProduct> wrapper = getWrapper();
        //过滤待创建产品数据
        wrapper.eq("eq.create_product_status", 1);
        wrapper.isNull("eqp.product_id");
        Page<ExtQuotationProductVo> page = baseMapper.waitCreateProductPage(dto.getPage(), wrapper);
        List<ExtQuotationProductVo> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return page;
        }
        List<Long> pIds = records.stream().map(ExtQuotationProduct::getId).collect(Collectors.toList());

        //获取BOM信息
        List<ExtQuotationProductBom> bomList = extQuotationProductBomService.list(q -> q.in(ExtQuotationProductBom::getQuotationProductId, pIds));
        List<ExtQuotationProductBomVo> extQuotationProductBomVos = BeanUtil.copyToList(bomList, ExtQuotationProductBomVo.class);
        productInfoService.attributeAssign(extQuotationProductBomVos, ExtQuotationProductBom::getMaterialId, (item, product) -> {
            item.setProductCode(product.getCustomCode());
            item.setProductName(product.getName());
            item.setProductLength(product.getLength());
            item.setProductWidth(product.getWidth());
            item.setProductHeight(product.getHeight());
            item.setProductColor(product.getColor());
        });
        Map<Long, List<ExtQuotationProductBomVo>> eqpbMap = extQuotationProductBomVos.stream().collect(Collectors.groupingBy(ExtQuotationProductBom::getQuotationProductId));

        //获取定制信息
        List<ExtQuotationProductCustomInfo> eqpciList = extQuotationProductCustomInfoService.list(q -> q.in(ExtQuotationProductCustomInfo::getExtQuotationProductId, pIds));
        List<ExtQuotationProductCustomInfoVo> eqpcivList = BeanUtil.copyToList(eqpciList, ExtQuotationProductCustomInfoVo.class);
        List<Long> eqpciIds = eqpcivList.stream().map(ExtQuotationProductCustomInfo::getId).collect(Collectors.toList());
        Map<Long, List<FileInfoVo>> fileMap = ObsFileUtil.getFileMap(eqpciIds);
        for (ExtQuotationProductCustomInfoVo extQuotationProductCustomInfoVo : eqpcivList) {
            extQuotationProductCustomInfoVo.setFileList(fileMap.get(extQuotationProductCustomInfoVo.getId()));
        }
        Map<Long, List<ExtQuotationProductCustomInfoVo>> eqpciMap = eqpcivList.stream().collect(Collectors.groupingBy(ExtQuotationProductCustomInfo::getExtQuotationProductId));

        //赋值原材料名称
        productInfoService.attributeAssign(records, ExtQuotationProduct::getRawMaterialId, (item, product) -> {
            item.setRawMaterialName(product.getName());
            item.setRawMaterialCode(product.getCode());
            item.setRawMaterialColor(product.getColor());
            item.setRawMaterialColorCardCode(product.getColorCardCode());
            item.setRawMaterialLength(product.getLength());
            item.setRawMaterialWidth(product.getWidth());
            item.setRawMaterialHeight(product.getHeight());
            item.setNetWeight(product.getNetWeight());
        });

        //赋值信息
        for (ExtQuotationProductVo record : records) {
            //赋值单号
            //赋值BOM信息
            record.setQuotationProductBomList(eqpbMap.get(record.getId()));
            //赋值定制信息
            record.setQuotationProductCustomInfoList(eqpciMap.get(record.getId()));
        }
        return page;
    }

    @DSTransactional
    @Override
    public void createProduct(ProductInfoDto productInfoDto) {
        productInfoDto.setId(IdWorker.getId());
        List<ProductBomDetail> productBomDetailList = productInfoDto.getProductBomDetailList();
        productBomDetailList.forEach(item -> item.setId(IdWorker.getId()));
        List<ProductCustomInfoDto> productCustomInfoList = productInfoDto.getProductCustomInfoList();
        for (ProductCustomInfoDto productCustomInfoDto : productCustomInfoList) {
            productCustomInfoDto.setId(IdWorker.getId());
            List<ObsFile> fileList = productCustomInfoDto.getFileList();
            ObsFileUtil.copyFileAndSave(fileList, productCustomInfoDto.getId());
        }
        productInfoService.add(productInfoDto);

        Long extQuotationProductId = productInfoDto.getExtQuotationProductId();

        ExtQuotationProduct extQuotationProduct = this.getById(extQuotationProductId);
        Assert.notEmpty(extQuotationProduct, "查询不到对外报价单产品信息");

        this.update(q -> q
                .eq(ExtQuotationProduct::getQuotationProductId, extQuotationProductId)
                .set(ExtQuotationProduct::getProductId, productInfoDto.getId())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
                .set(BasePo::getUpdateTime, new Date())
        );

        extQuotationProductBomService.update(q -> q
                .eq(ExtQuotationProductBom::getQuotationProductId, extQuotationProductId)
                .set(ExtQuotationProductBom::getProductId, productInfoDto.getId())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
                .set(BasePo::getUpdateTime, new Date())
        );

        extQuotationProductCustomInfoService.update(q -> q
                .eq(ExtQuotationProductCustomInfo::getExtQuotationProductId, extQuotationProductId)
                .set(ExtQuotationProductCustomInfo::getProductId, productInfoDto.getId())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
                .set(BasePo::getUpdateTime, new Date())
        );

        //更新状态
        List<ExtQuotationProduct> list = extQuotationProductService.list(q -> q
                .eq(ExtQuotationProduct::getSaleQuotationId, extQuotationProduct.getSaleQuotationId())
                .isNull(ExtQuotationProduct::getProductId)
        );
        if (ObjectUtil.isEmpty(list)) {
            extQuotationService.update(q -> q
                    .eq(ExtQuotation::getId, extQuotationProduct.getSaleQuotationId())
                    .set(ExtQuotation::getCreateProductStatus, 2)
                    .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
                    .set(BasePo::getUpdateTime, new Date())
            );
        }
    }
}
