package com.shkj.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.shkj.common.utils.SecurityUtils;
import com.shkj.wms.mapper.JimuReportTypeConfigMapper;
import com.shkj.wms.service.IJimuReportTypeConfigService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.shkj.common.core.domain.Result;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.shkj.wms.bo.JimuReportTypeConfigAddBo;
import com.shkj.wms.bo.JimuReportTypeConfigQueryBo;
import com.shkj.wms.bo.JimuReportTypeConfigEditBo;
import com.shkj.wms.domain.JimuReportTypeConfig;
import com.shkj.wms.vo.JimuReportTypeConfigVo;

import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 积木报表关联类型配置Service业务层处理
 *
 * @author lch
 * @date 2022-08-23
 */
@Slf4j
@Service
public class JimuReportTypeConfigServiceImpl extends ServiceImpl<JimuReportTypeConfigMapper, JimuReportTypeConfig> implements IJimuReportTypeConfigService {

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

    @Override
    public List<JimuReportTypeConfigVo> queryList(JimuReportTypeConfigQueryBo bo) {
        LambdaQueryWrapper<JimuReportTypeConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getReportType() != null, JimuReportTypeConfig::getReportType, bo.getReportType());
        lqw.eq(StrUtil.isNotBlank(bo.getExcelConfigId()), JimuReportTypeConfig::getExcelConfigId, bo.getExcelConfigId());
        lqw.eq(StrUtil.isNotBlank(bo.getRemarks()), JimuReportTypeConfig::getRemarks, bo.getRemarks());
        lqw.orderByDesc(JimuReportTypeConfig::getCreateTime);
        List<JimuReportTypeConfigVo> voList = entity2Vo(this.list(lqw));
        return voList;

    }

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

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<Boolean> insertByAddBo(JimuReportTypeConfigAddBo bo) {
        LambdaQueryWrapper<JimuReportTypeConfig> wrapper = new LambdaQueryWrapper<JimuReportTypeConfig>().eq(JimuReportTypeConfig::getBranchId, SecurityUtils.getCurrentBranchId())
                .eq(JimuReportTypeConfig::getReportType, bo.getReportType());

        JimuReportTypeConfig one = getOne(wrapper);
        if (one != null){
            return Result.err().msg("已经存在该表报表类型");
        }
        JimuReportTypeConfig add = BeanUtil.toBean(bo, JimuReportTypeConfig. class);
        add.setBranchId(SecurityUtils.getCurrentBranchId());
        this.save(add);
        return Result.ok();
    }

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


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

    @Override
    public List<JimuReportTypeConfig> getReportTypeByTypeAndBranchId(String reportType, Long currentBranchId) {
        LambdaQueryWrapper<JimuReportTypeConfig> wrapper = new LambdaQueryWrapper<JimuReportTypeConfig>()
                .eq(JimuReportTypeConfig::getReportType, reportType);

        return list(wrapper);
    }


}
