package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.domain.AjaxResult;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.SysUser;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.UpdateAppBo;
import org.dromara.system.domain.vo.UpdateAppVo;
import org.dromara.system.domain.UpdateApp;
import org.dromara.system.mapper.UpdateAppMapper;
import org.dromara.system.service.IUpdateAppService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP版本描述Service业务层处理
 *
 * @author boman
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
public class UpdateAppServiceImpl implements IUpdateAppService {

    private final UpdateAppMapper baseMapper;

    /**
     * 查询APP版本描述
     */
    @Override
    public UpdateAppVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询APP版本描述列表
     */
    @Override
    public TableDataInfo<UpdateAppVo> queryPageList(UpdateAppBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UpdateApp> lqw = buildQueryWrapper(bo);
        Page<UpdateAppVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP版本描述列表
     */
    @Override
    public List<UpdateAppVo> queryList(UpdateAppBo bo) {
        LambdaQueryWrapper<UpdateApp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UpdateApp> buildQueryWrapper(UpdateAppBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UpdateApp> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), UpdateApp::getModel, bo.getModel());
        lqw.like(StringUtils.isNotBlank(bo.getName()), UpdateApp::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), UpdateApp::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), UpdateApp::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), UpdateApp::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDel()), UpdateApp::getIsDel, bo.getIsDel());
        return lqw;
    }

    /**
     * 新增APP版本描述
     */
    @Override
    public Boolean insertByBo(UpdateAppBo bo) {

        //将之前的数据全改为失效
        baseMapper.update(null,
            new LambdaUpdateWrapper<UpdateApp>()
                .set(UpdateApp::getIsDel, "0")
                .eq(UpdateApp::getModel, bo.getModel()));

        UpdateApp add = MapstructUtils.convert(bo, UpdateApp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP版本描述
     */
    @Override
    public Boolean updateByBo(UpdateAppBo bo) {
        UpdateApp update = MapstructUtils.convert(bo, UpdateApp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UpdateApp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP版本描述
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public AjaxResult getInfoNew(UpdateAppBo bo) {
        bo.setIsDel("Y");
        LambdaQueryWrapper<UpdateApp> lqw = buildQueryWrapper(bo);
        List<UpdateAppVo> appList = baseMapper.selectVoList(lqw);
        if(appList != null && appList.size()>0){
            return AjaxResult.success(appList.get(0));
        }
        return AjaxResult.success(new UpdateAppVo());
    }
}
