package com.fjhx.wms.service.stock.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.ProductInfo;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.wms.entity.stock.dto.StockJournalDetailsDto;
import com.fjhx.wms.entity.stock.dto.StockJournalSelectDto;
import com.fjhx.wms.entity.stock.po.StockJournal;
import com.fjhx.wms.entity.stock.po.StockJournalDetails;
import com.fjhx.wms.entity.stock.vo.StockJournalDetailsVo;
import com.fjhx.wms.mapper.stock.StockJournalDetailsMapper;
import com.fjhx.wms.service.stock.StockJournalDetailsService;
import com.fjhx.wms.service.warehouse.WarehouseService;
import com.ruoyi.common.utils.SecurityUtils;
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.List;
import java.util.Objects;


/**
 * <p>
 * 出入库记录明细 服务实现类
 * </p>
 *
 * @author
 * @since 2023-03-20
 */
@Service
public class StockJournalDetailsServiceImpl extends ServiceImpl<StockJournalDetailsMapper, StockJournalDetails> implements StockJournalDetailsService {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private WarehouseService warehouseService;

    @Override
    public Page<StockJournalDetailsVo> getPage(StockJournalSelectDto dto) {
        IWrapper<StockJournalDetails> wrapper = getWrapper();
        wrapper.eq("sj.op_type", dto.getOpType());
        wrapper.eq("sj.type", dto.getType());
        wrapper.eq("w.id", dto.getWarehouseId());
        wrapper.eq("sj.to_warehouse_id", dto.getToWarehouseId());
        wrapper.eq("sjd.product_id", dto.getProductId());

        //关键字检索
        if (ObjectUtil.isNotEmpty(dto.getKeyword())) {
            List<Long> pIds = productInfoService.listObject(ProductInfo::getId, q -> q
                    .like(ProductInfo::getCustomCode, dto.getKeyword())
                    .or().like(ProductInfo::getName, dto.getKeyword())
            );
            wrapper.and(q -> q
                    .in("sjd.product_id", pIds)
                    .or().like("w.name", dto.getKeyword())
                    .or().like("sj.code", dto.getKeyword())
            );
        }

        //权限过滤：出入库流水-子公司看自己，总公司看全部
        Long companyId = SecurityUtils.getCompanyId();
        if (!Objects.equals(companyId, 100L)) {
            wrapper.eq("sjd.company_id", companyId);
        } else {
            wrapper.eq("sjd.company_id", dto.getCompanyId());
        }

        //是否归还过滤
        Integer isRestitution = dto.getIsRestitution();
        if (ObjectUtil.isNotEmpty(isRestitution)) {
            if (ObjectUtil.equals(isRestitution, 0)) {
                wrapper.isNull("sj.restitution_id");
            } else {
                wrapper.isNotNull("sj.restitution_id");
            }
        }

        //是否寻回过滤
        Integer isSeek = dto.getIsSeek();
        if (ObjectUtil.isNotEmpty(isSeek)) {
            if (ObjectUtil.equals(isSeek, 0)) {
                wrapper.isNull("sj.seek_id");
            } else {
                wrapper.isNotNull("sj.seek_id");
            }
        }

        wrapper.orderByDesc("sj", StockJournal::getCreateTime);

        Page<StockJournalDetailsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<StockJournalDetailsVo> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return page;
        }
        // 赋值产品属性
        productInfoService.attributeAssign(records, StockJournalDetailsVo::getProductId, (item, product) -> {
            item.setProductCode(product.getCustomCode());
            item.setProductUnit(product.getUnit());
            item.setProductType(product.getType());
            item.setProductName(product.getName());
            item.setProductSpec(product.getSpec());
            item.setProductCustomCode(product.getCustomCode());
        });
        //赋值操作人名称
        UserUtil.assignmentNickName(records, StockJournalDetailsVo::getCreateUser, StockJournalDetailsVo::setOpUserName);
        //赋值目标仓库名称
        warehouseService.attributeAssign(records, StockJournalDetailsVo::getToWarehouseId, (item, warehouse) -> {
            item.setToWarehouseName(warehouse.getName());
        });
        return page;
    }

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

    @Override
    public void add(StockJournalDetailsDto stockJournalDetailsDto) {
        this.save(stockJournalDetailsDto);
    }

    @Override
    public void edit(StockJournalDetailsDto stockJournalDetailsDto) {
        this.updateById(stockJournalDetailsDto);
    }

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

}
