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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.common.constant.SourceConstant;
import com.fjhx.item.entity.product.vo.ProductInfoVo;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.purchase.entity.sales.dto.SalesReturnDetailDto;
import com.fjhx.purchase.entity.sales.dto.SalesReturnDetailSelectDto;
import com.fjhx.purchase.entity.sales.po.SalesReturnDetail;
import com.fjhx.purchase.entity.sales.vo.SalesReturnDetailVo;
import com.fjhx.purchase.mapper.sales.SalesReturnDetailMapper;
import com.fjhx.purchase.service.sales.SalesReturnDetailService;
import com.fjhx.supply.entity.supplier.po.SupplierInfo;
import com.fjhx.supply.service.supplier.SupplierInfoService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.system.utils.UserUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 到货明细单 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-11
 */
//@DS(SourceConstant.PURCHASE)
@Service
public class SalesReturnDetailServiceImpl extends ServiceImpl<SalesReturnDetailMapper, SalesReturnDetail> implements SalesReturnDetailService {

    @Autowired
    private SupplierInfoService supplierInfoService;

    @Autowired
    private ProductInfoService productInfoService;


    @Override
    public Page<SalesReturnDetailVo> getPage(SalesReturnDetailSelectDto dto) {
        IWrapper<SalesReturnDetail> wrapper = getWrapper();
        wrapper.orderByDesc("t1", SalesReturnDetail::getCreateTime);
        if(StringUtils.isNotEmpty(dto.getStatus())){
            wrapper.eq("t1",SalesReturnDetail::getStatus,dto.getStatus());
        }
        Page<SalesReturnDetailVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<SalesReturnDetailVo> list = page.getRecords();
        if(CollectionUtils.isNotEmpty(list)){
            List<Long> supplyIds = list.stream().map(SalesReturnDetailVo::getSupplyId).collect(Collectors.toList());
            List<SupplierInfo> supplierInfoList = supplierInfoService.list(Wrappers.<SupplierInfo>query().lambda().in(SupplierInfo::getId,supplyIds));
            Map<Long,List<SupplierInfo>> supplyMap = supplierInfoList.stream().distinct().collect(Collectors.groupingBy(SupplierInfo::getId));
            if(MapUtils.isNotEmpty(supplyMap)){
                for(SalesReturnDetailVo p:list){
                    List<SupplierInfo> supplys = supplyMap.getOrDefault(p.getSupplyId(),null);
                    p.setSupplyName(supplys==null?null:supplys.get(0).getName());
                }
            }
            //查询产品
            List<Long> productIds = list.stream().map(SalesReturnDetailVo::getBussinessId).collect(Collectors.toList());
            List<ProductInfoVo> productInfoVos = productInfoService.getListByProductIds(productIds);
            Map<Long, List<ProductInfoVo>> productMap = productInfoVos.stream().collect(Collectors.groupingBy(ProductInfoVo::getId));
            if(MapUtils.isNotEmpty(productMap)) {
                for(SalesReturnDetailVo s:list){
                    List<ProductInfoVo> ps = productMap.get(s.getBussinessId());
                    if(CollectionUtils.isNotEmpty(ps)){
                        ProductInfoVo p = ps.get(0);
                        s.setProductCategory(p.getClassifyName());
                        s.setProductCode(p.getCode());
                        s.setProductUnit(p.getUnit());
                        s.setProductType(p.getType());
                        s.setProductName(p.getName());
                        s.setProductSpec(p.getSpec());
                    }
                }
            }
        }
        //赋值操作人
        UserUtil.assignmentNickName(list, SalesReturnDetailVo::getUpdateUser, SalesReturnDetailVo::setOpUserName);
        return page;
    }

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

    @Override
    public void add(SalesReturnDetailDto salesReturnDetailDto) {
        this.save(salesReturnDetailDto);
    }

    @Override
    public void edit(SalesReturnDetailDto salesReturnDetailDto) {
        this.updateById(salesReturnDetailDto);
    }

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

}
