package com.shkj.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.shkj.common.core.domain.Result;
import com.shkj.wms.bo.SysStockDetailQrcodeAddBo;
import com.shkj.wms.bo.SysStockDetailQrcodeEditBo;
import com.shkj.wms.bo.SysStockDetailQrcodeQueryBo;
import com.shkj.wms.domain.SysStockDetailQrcode;
import com.shkj.wms.mapper.SysStockDetailQrcodeMapper;
import com.shkj.wms.service.ISysStockDetailQrcodeService;
import com.shkj.wms.vo.SysStockDetailQrcodeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 已建表-库存明细产品信息Service业务层处理
 *
 * @author lch
 * @date 2023-05-07
 */
@Slf4j
@Service
public class SysStockDetailQrcodeServiceImpl extends ServiceImpl<SysStockDetailQrcodeMapper, SysStockDetailQrcode> implements ISysStockDetailQrcodeService {

    @Override
    public Result<SysStockDetailQrcodeVo> queryById(Long id) {
        SysStockDetailQrcode db = this.baseMapper.selectById(id);
        SysStockDetailQrcodeVo vo = BeanUtil.toBean(db, SysStockDetailQrcodeVo.class);
        return Result.ok().data(vo);
    }

    @Override
    public List<SysStockDetailQrcodeVo> queryList(SysStockDetailQrcodeQueryBo bo) {
        LambdaQueryWrapper<SysStockDetailQrcode> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLotDetailId() != null, SysStockDetailQrcode::getLotDetailId, bo.getLotDetailId());
        lqw.eq(StrUtil.isNotBlank(bo.getQrCode()), SysStockDetailQrcode::getQrCode, bo.getQrCode());
        lqw.eq(StrUtil.isNotBlank(bo.getGoodsNo()), SysStockDetailQrcode::getGoodsNo, bo.getGoodsNo());
        List<SysStockDetailQrcodeVo> voList = entity2Vo(this.list(lqw));
        return voList;

    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<SysStockDetailQrcodeVo> entity2Vo(Collection<SysStockDetailQrcode> collection) {
        List<SysStockDetailQrcodeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, SysStockDetailQrcodeVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<SysStockDetailQrcode> page = (Page<SysStockDetailQrcode>) collection;
            Page<SysStockDetailQrcodeVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> insertByAddBo(SysStockDetailQrcodeAddBo bo) {
        SysStockDetailQrcode add = BeanUtil.toBean(bo, SysStockDetailQrcode.class);
        this.save(add);
        return Result.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> updateByEditBo(SysStockDetailQrcodeEditBo bo) {
        SysStockDetailQrcode one = getById(bo.getId());
        if (one == null) {
            return Result.err().msg("不存在");
        }
        SysStockDetailQrcode update = BeanUtil.toBean(bo, SysStockDetailQrcode.class);
        this.updateById(update);
        return Result.ok();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> deleteWithValidById(List<Long> ids, Boolean isValid) {
        LambdaQueryWrapper<SysStockDetailQrcode> wrapper = new LambdaQueryWrapper<SysStockDetailQrcode>().in(SysStockDetailQrcode::getId, ids);
        this.remove(wrapper);
        return Result.ok();
    }

    @Override
    public Result<Boolean> depositGoods(Long detailId, Map<String, String> good) {
        List<SysStockDetailQrcode> detailQrcodeList = new ArrayList<>();
            SysStockDetailQrcode detailQrcode = new SysStockDetailQrcode();
            detailQrcode.setLotDetailId(detailId);
            if (StrUtil.isNotBlank(good.get("goodsNo"))) {
                detailQrcode.setGoodsNo(good.get("goodsNo"));
            }
            if (StrUtil.isNotBlank(good.get("qrCode"))) {
                detailQrcode.setQrCode(good.get("qrCode"));
            }
            detailQrcodeList.add(detailQrcode);
        this.saveBatch(detailQrcodeList);
        return Result.ok();
    }
}
