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.common.utils.SecurityUtils;
import com.shkj.wms.domain.SysBatch;
import com.shkj.wms.mapper.SysBatchMapper;
import com.shkj.wms.service.ISysBatchService;
import com.shkj.wms.bo.SysBatchAddBo;
import com.shkj.wms.bo.SysBatchEditBo;
import com.shkj.wms.bo.SysBatchQueryBo;
import com.shkj.wms.vo.SysBatchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 批次Service业务层处理
 *
 * @author lch
 * @date 2022-04-09
 */
@Slf4j
@Service
public class SysBatchServiceImpl extends ServiceImpl<SysBatchMapper, SysBatch> implements ISysBatchService {

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

    @Override
    public List<SysBatchVo> queryList(SysBatchQueryBo bo) {
        LambdaQueryWrapper<SysBatch> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoodId() != null, SysBatch::getGoodId, bo.getGoodId());
        lqw.eq(bo.getConsignorId() != null, SysBatch::getConsignorId, bo.getConsignorId());
        lqw.eq(bo.getPackageId() != null, SysBatch::getPackageId, bo.getPackageId());
        lqw.eq(StrUtil.isNotBlank(bo.getBatchNo()), SysBatch::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getProductionDate() != null, SysBatch::getProductionDate, bo.getProductionDate());
        lqw.eq(bo.getExpiredDate() != null, SysBatch::getExpiredDate, bo.getExpiredDate());
        lqw.eq(StrUtil.isNotBlank(bo.getFactory()), SysBatch::getFactory, bo.getFactory());
        lqw.eq(StrUtil.isNotBlank(bo.getWaster()), SysBatch::getWaster, bo.getWaster());
        lqw.eq(bo.getBranchId() != null, SysBatch::getBranchId, bo.getBranchId());
        lqw.orderByDesc(SysBatch::getCreateTime);
        List<SysBatchVo> voList = entity2Vo(this.list(lqw));
        return voList;

    }

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

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

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


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> deleteWithValidById(List<Long> ids, Boolean isValid) {
        SysBatch del = new SysBatch();
        LambdaQueryWrapper<SysBatch> wrapper = new LambdaQueryWrapper<SysBatch>().in(SysBatch::getId, ids)
                .eq(SysBatch::getBranchId, SecurityUtils.getCurrentBranchId());
        this.update(del, wrapper);
        return Result.ok();
    }

    @Override
    public Result<SysBatch> getSysBatch(SysBatchAddBo bo) {
        // 商品ID
        //货主ID
        //包装ID
        //批号
        //生产日期
        //有效期
        //厂家
        //生产地
        //仓ID
        LambdaQueryWrapper<SysBatch> wrapper = new LambdaQueryWrapper<SysBatch>().
                eq(SysBatch::getGoodId, bo.getGoodId())
                .eq(SysBatch::getConsignorId, bo.getConsignorId())
                .eq(SysBatch::getPackageId, bo.getPackageId())
                .eq(SysBatch::getBatchNo, bo.getBatchNo())
                .eq(SysBatch::getProductionDate, bo.getProductionDate())
                .eq(SysBatch::getExpiredDate, bo.getExpiredDate())
                .eq(SysBatch::getFactory, bo.getFactory())
                .eq(SysBatch::getWaster, bo.getWaster())
                .eq(SysBatch::getBranchId, bo.getBranchId()).last("limit 1");
        List<SysBatch> list = this.list(wrapper);
        if (list.size() == 0) {
            SysBatch sysBatch = BeanUtil.toBean(bo, SysBatch.class);
            // 重新成时间人员
            sysBatch.setCreateTime(null);
            sysBatch.setUpdateTime(null);
            sysBatch.setCreateBy("");
            sysBatch.setUpdateBy("");
            this.save(sysBatch);
            return Result.ok().data(sysBatch);
        } else {
            return Result.ok().data(list.get(0));
        }
    }

}
