package com.fjhx.mes.service.border.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.item.entity.product.po.ProductClassify;
import com.fjhx.item.entity.product.po.ProductInfo;
import com.fjhx.item.service.product.ProductClassifyService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.mes.entity.border.dto.BorderOfLineDto;
import com.fjhx.mes.entity.border.dto.BorderOfLineSelectDto;
import com.fjhx.mes.entity.border.po.BorderOfLine;
import com.fjhx.mes.entity.border.po.BorderOfLineJournal;
import com.fjhx.mes.entity.border.vo.BorderOfLineVo;
import com.fjhx.mes.mapper.border.BorderOfLineMapper;
import com.fjhx.mes.service.border.BorderOfLineJournalService;
import com.fjhx.mes.service.border.BorderOfLineService;
import com.fjhx.wms.entity.stock.po.StockWait;
import com.fjhx.wms.entity.stock.po.StockWaitDetails;
import com.fjhx.wms.service.stock.StockWaitDetailsService;
import com.fjhx.wms.service.stock.StockWaitService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 线边管理 服务实现类
 * </p>
 *
 * @author
 * @since 2023-03-31
 */
@Service
public class BorderOfLineServiceImpl extends ServiceImpl<BorderOfLineMapper, BorderOfLine> implements BorderOfLineService {
    @Autowired
    BorderOfLineJournalService borderOfLineJournalService;
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    ProductClassifyService productClassifyService;
    @Autowired
    StockWaitService stockWaitService;
    @Autowired
    StockWaitDetailsService stockWaitDetailsService;

    @Override
    public Page<BorderOfLineVo> getPage(BorderOfLineSelectDto dto) {
        IWrapper<BorderOfLine> wrapper = getWrapper();
        wrapper.orderByDesc("bol", BorderOfLine::getId);
        wrapper.ne(BorderOfLine::getQuantity, 0);
        Page<BorderOfLineVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<BorderOfLineVo> records = page.getRecords();

        if (records.size() == 0) {
            return page;
        }

        //赋值产品名称
        productInfoService.attributeAssign(records, BorderOfLine::getProductId, (item, productInfo) -> {
            item.setProductName(productInfo.getName());
            item.setClassifyId(productInfo.getProductClassifyId());
        });

        //赋值产品分类
        productClassifyService.attributeAssign(records,BorderOfLineVo::getClassifyId,(item,classify)->{
            item.setProductTypeName(classify.getName());
        });

        return page;
    }

    @Override
    public BorderOfLineVo detail(BorderOfLineDto dto) {
        BorderOfLine borderOfLine = getOne(q -> q.eq(BorderOfLine::getId, dto.getId()).or().eq(BorderOfLine::getProductId, dto.getProductId()));
        BorderOfLineVo result = BeanUtil.toBean(borderOfLine, BorderOfLineVo.class);
        if (ObjectUtil.isNotEmpty(result)) {
            ProductInfo productInfo = productInfoService.getById(result.getProductId());
            if (ObjectUtil.isNotEmpty(productInfo)) {
                result.setProductName(productInfo.getName());
                ProductClassify productClassify = productClassifyService.getById(productInfo.getProductClassifyId());
                if (ObjectUtil.isNotEmpty(productClassify)) {
                    result.setProductTypeName(productClassify.getName());
                }
            }
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BorderOfLineDto borderOfLineDto) {
        BorderOfLine borderOfLine = getOne(q -> q.eq(BorderOfLine::getProductId, borderOfLineDto.getProductId()));
        if (ObjectUtil.isNotEmpty(borderOfLine)) {
            borderOfLine.setQuantity(borderOfLine.getQuantity().add(borderOfLineDto.getQuantity()));
        } else {
            borderOfLine = BeanUtil.toBean(borderOfLineDto, BorderOfLine.class);
        }
        saveOrUpdate(borderOfLine);
        //创建流水记录
        BorderOfLineJournal borderOfLineJournal = new BorderOfLineJournal();
        borderOfLineJournal.setBorderOfLineId(borderOfLine.getId());
        borderOfLineJournal.setQuantity(borderOfLineDto.getQuantity());
        borderOfLineJournal.setType(5);
        borderOfLineJournalService.save(borderOfLineJournal);
    }

    @Override
    public void edit(BorderOfLineDto borderOfLineDto) {
        BorderOfLine borderOfLine = getById(borderOfLineDto.getId());
        borderOfLine.setQuantity(borderOfLine.getQuantity().subtract(borderOfLineDto.getQuantity()));
        updateById(borderOfLine);
        //创建流水记录
        BorderOfLineJournal borderOfLineJournal = new BorderOfLineJournal();
        borderOfLineJournal.setBorderOfLineId(borderOfLine.getId());
        borderOfLineJournal.setQuantity(borderOfLineDto.getQuantity());
        borderOfLineJournal.setType(borderOfLineDto.getType());
        borderOfLineJournalService.save(borderOfLineJournal);
        //如果类型是"回仓"创建待入库数据
        if (borderOfLineDto.getType() == 2) {
            StockWait stockWait = new StockWait();
            stockWait.setType(1);
//            stockWait.setBusinessType(StockWaitType.BORDER_OF_LINE_IN.getDetailType());
            stockWait.setBusinessId(borderOfLineJournal.getId());
            stockWait.setStatus(0);
            stockWaitService.save(stockWait);
            StockWaitDetails stockWaitDetails = new StockWaitDetails();
            stockWaitDetails.setStockWaitId(stockWait.getId());
            stockWaitDetails.setProductId(borderOfLine.getProductId());
            stockWaitDetails.setQuantity(borderOfLineDto.getQuantity());
            stockWaitDetails.setReceiptQuantity(BigDecimal.ZERO);
            stockWaitDetailsService.save(stockWaitDetails);
        }
    }

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

}
