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.common.utils.StringUtils;
import com.shkj.wms.domain.SysAppVersion;
import com.shkj.wms.mapper.SysAppVersionMapper;
import com.shkj.wms.service.ISysAppVersionService;
import com.shkj.wms.bo.SysAppVersionAddBo;
import com.shkj.wms.bo.SysAppVersionQueryBo;
import com.shkj.wms.utils.BeanMapperUtils;
import com.shkj.wms.vo.SysAppVersionVo;
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-07-24
 */
@Slf4j
@Service
public class SysAppVersionServiceImpl extends ServiceImpl<SysAppVersionMapper, SysAppVersion> implements ISysAppVersionService {

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

    @Override
    public List<SysAppVersionVo> queryList(SysAppVersionQueryBo bo) {
        LambdaQueryWrapper<SysAppVersion> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getCode()), SysAppVersion::getCode, bo.getCode());
        lqw.orderByDesc(SysAppVersion::getCreateTime);
        List<SysAppVersionVo> voList = entity2Vo(this.list(lqw));
        return voList;

    }

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

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

    @Override
    public Result<SysAppVersionVo> checkAppNeedUpdate(String code) {
        SysAppVersion one = getOne(new LambdaQueryWrapper<SysAppVersion>()
                .eq(SysAppVersion::getBranchId, SecurityUtils.getCurrentBranchId())
                .orderByDesc(SysAppVersion::getCreateTime)
                .last("limit 1"));
        if (one == null) {
            return Result.err().msg("没有版本");
        }

        if (!one.getCode().equals(code) || StringUtils.isBlank(code)) {
            SysAppVersionVo map = BeanMapperUtils.map(one, SysAppVersionVo.class);
            map.setIsUpdate(1);
            return Result.ok().data(map);
        }

        SysAppVersionVo map = BeanMapperUtils.map(one, SysAppVersionVo.class);
        map.setIsUpdate(0);
        return Result.ok().data(map);
    }


}
