package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SchoolRecipe;
import org.dromara.system.domain.bo.SchoolRecipeBo;
import org.dromara.system.domain.vo.SchoolRecipeVo;
import org.dromara.system.mapper.SchoolRecipeMapper;
import org.dromara.system.service.ISchoolRecipeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 学校食谱Service业务层处理
 *
 * @author boman
 * @date 2023-09-22
 */
@RequiredArgsConstructor
@Service
public class SchoolRecipeServiceImpl implements ISchoolRecipeService {

    private final SchoolRecipeMapper baseMapper;

    /**
     * 查询学校食谱
     */
    @Override
    public SchoolRecipeVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询学校食谱列表
     */
    @Override
    public TableDataInfo<SchoolRecipeVo> queryPageList(SchoolRecipeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SchoolRecipe> lqw = buildQueryWrapper(bo);
        Page<SchoolRecipeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学校食谱列表
     */
    @Override
    public List<SchoolRecipeVo> queryList(SchoolRecipeBo bo) {
        LambdaQueryWrapper<SchoolRecipe> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SchoolRecipe> buildQueryWrapper(SchoolRecipeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SchoolRecipe> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSchoolId() != null, SchoolRecipe::getSchoolId, bo.getSchoolId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SchoolRecipe::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateDate()), SchoolRecipe::getCreateDate, bo.getCreateDate());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), SchoolRecipe::getDescription, bo.getDescription());
        lqw.orderByDesc(SchoolRecipe::getCreateTime);
        return lqw;
    }

    /**
     * 新增学校食谱
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(SchoolRecipeBo bo) {
        //查询是否存在当天的当前就餐类型数据
        SchoolRecipeBo b = new SchoolRecipeBo();
        b.setCreateDate(DateUtils.getDate());
        b.setType(bo.getType());
        LambdaQueryWrapper<SchoolRecipe> lqw = buildQueryWrapper(b);
        List<SchoolRecipeVo> voList = baseMapper.selectVoList(lqw);
        if (voList != null && voList.size() > 0) {
            throw new ServiceException("当前就餐类型已添加，请勿重复新增");
        }

        bo.setSchoolId(LoginHelper.getDeptId());
        bo.setCreateDate(DateUtils.getDate());

        SchoolRecipe add = MapstructUtils.convert(bo, SchoolRecipe.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;

        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改学校食谱
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(SchoolRecipeBo bo) {
        SchoolRecipe update = MapstructUtils.convert(bo, SchoolRecipe.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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