package ${packageName}.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.shkj.common.core.domain.Result;
import SwitchStatusEntity;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.shkj.common.enums.IsDelEnum;
import ${packageName}.bo.${ClassName}AddBo;
import ${packageName}.bo.${ClassName}QueryBo;
import ${packageName}.bo.${ClassName}EditBo;
import ${packageName}.domain.${ClassName};
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.vo.${ClassName}Vo;
import ${packageName}.service.I${ClassName}Service;

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

/**
 * ${functionName}Service业务层处理
 *
 * @author ${author}
 * @date ${datetime}
 */
@Slf4j
@Service
public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, ${ClassName}> implements I${ClassName}Service {

    @Override
    public Result<${ClassName}Vo> queryById(${pkColumn.javaType} ${pkColumn.javaField}){
        ${ClassName} db = this.baseMapper.selectById(${pkColumn.javaField});
        ${ClassName}Vo vo = BeanUtil.toBean(db, ${ClassName}Vo.class);
        return Result.ok().data(vo);
    }

    @Override
    public List<${ClassName}Vo> queryList(${ClassName}QueryBo bo) {
        LambdaQueryWrapper<${ClassName}> lqw = Wrappers.lambdaQuery();
#foreach($column in $columns)
#if($column.query)
#set($queryType=$column.queryType)
#set($javaField=$column.javaField)
#set($javaType=$column.javaType)
#set($columnName=$column.columnName)
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set($mpMethod=$column.queryType.toLowerCase())
#if($queryType != 'BETWEEN')
#if($javaType == 'String')
#set($condition='StrUtil.isNotBlank(bo.get'+$AttrName+'())')
#else
#set($condition='bo.get'+$AttrName+'() != null')
#end
        lqw.$mpMethod($condition, ${ClassName}::get$AttrName, bo.get$AttrName());
#else
        Object dataScope = bo.getParams().get("dataScope");
        lqw.apply(dataScope != null, dataScope != null ? dataScope.toString() : null);
        Map<String, Object> params = bo.getParams();
        if (params.get("begin$AttrName") != null && params.get("end$AttrName") != null) {
            lqw.between(${ClassName}::get$AttrName ,params.get("begin$AttrName"), params.get("end$AttrName"));
        }
#end
#end
#end
        lqw.eq(${ClassName}::getIsDel, IsDelEnum.NORMAL.getValue());
        lqw.orderByDesc(${ClassName}::getCreateTime);
        List<${ClassName}Vo> voList = entity2Vo(this.list(lqw));
        return voList;

    }

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

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

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


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


}
