package top.imwork.window.silos.service.book.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import jakarta.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import top.imwork.commons.core.utils.StringUtils;
import top.imwork.window.silos.convert.CategoriesConvert;
import top.imwork.window.silos.dao.CategoriesDao;
import top.imwork.window.silos.entity.Categories;
import top.imwork.window.silos.pojo.bo.CategoriesBO;
import top.imwork.window.silos.pojo.bo.CategoriesResultBO;
import top.imwork.window.silos.pojo.dto.CategoriesDTO;
import top.imwork.window.silos.pojo.dto.CategoriesListDTO;
import top.imwork.window.silos.service.book.ICategoriesService;

import java.util.Date;

@Service("categoriesService")
public class CategoriesServiceImpl extends ServiceImpl<CategoriesDao, Categories> implements ICategoriesService {
    @Resource
    private CategoriesDao categoriesDao;

    /**
     * 根据id获取信息
     * @param id 用户id
     * @return CategoriesBO 响应信息
     */
    @Override
    public CategoriesBO info(Long id) {
        Categories categories = categoriesDao.info(id);
        return CategoriesConvert.categoriesDoToBo(categories);
    }

    /**
     * 新增/更新信息
     * @param categoriesDTO 信息
     * @return categoriesBO 更新后信息
     */
    @Override
    public CategoriesBO save(CategoriesDTO categoriesDTO) {
        Categories categories = CategoriesConvert.categoriesDtoToDo(categoriesDTO);
        if (ObjectUtils.isEmpty(categories.getId())) {
            categories.setDelFlag("N");
            categories.setCreateTime(new Date());
            categoriesDao.insert(categories);
        } else {
            categoriesDao.updateById(categories);
        }
        return CategoriesConvert.categoriesDoToBo(categories);
    }

    /**
     * 根据id删除信息
     * @param id id
     * @return true/false 是否删除成功
     */
    @Override
    public Boolean delete(Long id) {
        return categoriesDao.deleteById(id) == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean deleteByIds(String[] ids) {
        return categoriesDao.deleteByIds(ids) > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public CategoriesResultBO queryPage(CategoriesListDTO categoriesListDTO) {
        QueryWrapper<Categories> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(categoriesListDTO.getCategoriesName())) {
            queryWrapper.like("category_name", categoriesListDTO.getCategoriesName());
        }

        if (!StringUtils.isEmpty(categoriesListDTO.getCreateBeginTime())) {
            queryWrapper.apply("create_time >= '" + categoriesListDTO.getCreateBeginTime() + "'");
        }

        if (!StringUtils.isEmpty(categoriesListDTO.getCreateEndTime())) {
            queryWrapper.apply("create_time <= '" + categoriesListDTO.getCreateEndTime() + "'");
        }

        if (!StringUtils.isEmpty(categoriesListDTO.getUpdateBeginTime())) {
            queryWrapper.apply("update_time >= '" + categoriesListDTO.getUpdateBeginTime() + "'");
        }

        if (!StringUtils.isEmpty(categoriesListDTO.getUpdateEndTime())) {
            queryWrapper.apply("update_time <= '" + categoriesListDTO.getUpdateEndTime() + "'");
        }

        Page<Categories> page = new Page<>(categoriesListDTO.getPageNo(), categoriesListDTO.getPageRows());
        IPage<Categories> iPage = categoriesDao.queryPage(page, queryWrapper);

        CategoriesResultBO resultBO = new CategoriesResultBO();
        resultBO.setPageNo(categoriesListDTO.getPageNo());
        resultBO.setPageRows(categoriesListDTO.getPageRows());
        resultBO.setPageCount((int) iPage.getPages());
        resultBO.setTotalRows((int) iPage.getTotal());
        resultBO.setDataList(CategoriesConvert.categoriesListToCategoriesBOList(iPage.getRecords()));
        resultBO.setParams(categoriesListDTO);

        return resultBO;
    }
}