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

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.extension.service.impl.ServiceImpl;

import top.imwork.commons.core.constants.Constants;
import top.imwork.commons.core.utils.StringUtils;
import top.imwork.window.silos.dao.ContentsDao;
import top.imwork.window.silos.entity.Contents;
import top.imwork.window.silos.pojo.bo.ContentsBO;
import top.imwork.window.silos.pojo.bo.ContentsResultBO;
import top.imwork.window.silos.pojo.dto.ContentsDTO;
import top.imwork.window.silos.pojo.dto.ContentsListDTO;
import top.imwork.window.silos.service.book.IContentsService;
import top.imwork.window.silos.convert.ContentsConvert;

import java.util.Date;
import java.util.List;

@Service("contentsService")
public class ContentsServiceImpl extends ServiceImpl<ContentsDao,Contents> implements IContentsService {
    @Resource
    private ContentsDao contentsDao;

    /**
     * 根据id获取信息
     * @param id 用户id
     * @return ContentsBO 响应信息
     */
    @Override
    public ContentsBO info(Long id) {
        Contents contents = contentsDao.info(id);
        return ContentsConvert.contentsDoToBo(contents);
    }

    /**
     * 新增/更新信息
     * @param contentsDTO 信息
     * @return contentsBO 更新后信息
     */
    @Override
    public ContentsBO save(ContentsDTO contentsDTO) {
        Contents contents = ContentsConvert.contentsDtoToDo(contentsDTO);
        if (ObjectUtils.isEmpty(contents.getId())) {
            contents.setDelFlag(Constants.DEL_FLAG_N);
            contents.setCreateTime(new Date());
            contentsDao.insert(contents);
        } else {
            contents.setUpdateTime(new Date());
            contentsDao.updateById(contents);
        }
        return ContentsConvert.contentsDoToBo(contents);
    }

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

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

    @Override
    public ContentsResultBO queryPage(ContentsListDTO contentsListDTO) {
        QueryWrapper<Contents> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(contentsListDTO.getContentsName())) {
            queryWrapper.like("chapter_title", contentsListDTO.getContentsName()).or()
                    .like("book_info_id", contentsListDTO.getContentsName()).or()
                    .like("chapter_number", contentsListDTO.getContentsName());
        }
        if (!StringUtils.isEmpty(contentsListDTO.getBookInfoId())) {
            queryWrapper.like("book_info_id", contentsListDTO.getBookInfoId());
        }
        if (!StringUtils.isEmpty(contentsListDTO.getContentType())) {
            queryWrapper.like("content_type", contentsListDTO.getContentType());
        }
        if (!StringUtils.isEmpty(contentsListDTO.getVersion())) {
            queryWrapper.like("version", contentsListDTO.getVersion());
        }
        if (!StringUtils.isEmpty(contentsListDTO.getIsPublic())) {
            queryWrapper.like("is_public", contentsListDTO.getIsPublic());
        }
        if (!StringUtils.isEmpty(contentsListDTO.getBookInfoId())) {
            queryWrapper.like("book_info_id", contentsListDTO.getBookInfoId());
        }

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

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

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

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


        PageHelper.startPage(contentsListDTO.getPageNo(), contentsListDTO.getPageRows());
        List<Contents> list = contentsDao.selectList(queryWrapper);
        PageInfo<Contents> pageInfo = new PageInfo<>(list);

        ContentsResultBO resultBO = new ContentsResultBO();
        resultBO.setPageNo(contentsListDTO.getPageNo());
        resultBO.setPageRows(contentsListDTO.getPageRows());
        resultBO.setPageCount(pageInfo.getPages());
        resultBO.setTotalRows((int) pageInfo.getTotal());
        resultBO.setDataList(ContentsConvert.contentsListToContentsBOList(list));
        resultBO.setParams(contentsListDTO);

        return resultBO;
    }
}