package com.ruoyi.base.commonproblem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.base.commonproblem.domain.CommonProblem;
import com.ruoyi.base.commonproblem.domain.bo.CommonProblemBo;
import com.ruoyi.base.commonproblem.domain.vo.CommonProblemVo;
import com.ruoyi.base.commonproblem.exception.CommonProblemExceptionEnum;
import com.ruoyi.base.commonproblem.mapper.CommonProblemMapper;
import com.ruoyi.base.commonproblem.service.ICommonProblemService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 常见问题Service业务层处理
 *
 * @author xiechao
 * @date 2023-12-21
 */
@RequiredArgsConstructor
@Service
public class CommonProblemServiceImpl implements ICommonProblemService {

    private final CommonProblemMapper baseMapper;

    /**
     * 查询常见问题分页
     *
     * @param bo 常见问题
     * @return 常见问题
     */
    @Override
    public TableDataInfo<CommonProblemVo> queryPageList(CommonProblemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CommonProblem> lqw = buildQueryWrapper(bo);
        lqw.select(CommonProblem.class, item->!item.getColumn().equals("reply_content"));
        Page<CommonProblemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<CommonProblem> buildQueryWrapper(CommonProblemBo bo) {
        LambdaQueryWrapper<CommonProblem> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getProblemTitle()), CommonProblem::getProblemTitle, bo.getProblemTitle());
        lqw.like(StrUtil.isNotBlank(bo.getReplyContent()), CommonProblem::getReplyContent, bo.getReplyContent());
        lqw.eq(ObjectUtil.isNotNull(bo.getStatus()), CommonProblem::getStatus, bo.getStatus());
        if(bo.getStartTime() != null) {
            lqw.ge(CommonProblem::getCreateTime, DateUtil.beginOfDay(bo.getStartTime()));
        }
        if(bo.getEndTime() != null) {
            lqw.le(CommonProblem::getCreateTime, DateUtil.endOfDay(bo.getEndTime()));
        }
        lqw.orderByAsc(CommonProblem::getSort);
        lqw.orderByDesc(CommonProblem::getCreateTime);
        return lqw;
    }

    /**
     * 通过id查询常见问题信息
     * @param id
     * @return
     */

    @Override
    public CommonProblemVo getById(Integer id){
         CommonProblem commonProblem= this.loadById(id,true);
        return  BeanCopyUtils.copy(commonProblem, CommonProblemVo.class);
    }

    /**
     * 详情常见问题
     *
     * @param id 常见问题主键
     * @return 常见问题
     */
    @Override
    public CommonProblem loadById(Integer id, Boolean tw){
        CommonProblem info = this.baseMapper.selectOne(
            new LambdaQueryWrapper<CommonProblem>()
                .eq(CommonProblem::getId, id)
        );
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(CommonProblemExceptionEnum.COMMON_PROBLEM_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增常见问题
     *
     * @param bo 常见问题
     * @return 结果
     */
    @Override
    public Boolean insertByBo(CommonProblemBo bo) {
        validEntityBeforeSave(bo);
        CommonProblem add = BeanUtil.toBean(bo, CommonProblem.class);
        return baseMapper.insert(add) > 0;
    }

    /**
     * 修改常见问题
     *
     * @param bo 常见问题
     * @return 结果
     */
    @Override
    public Boolean updateByBo(CommonProblemBo bo) {
        validEntityBeforeSave(bo);
        //验证信息是否存在
        CommonProblem commonProblem= this.loadById(bo.getId(),true);
        commonProblem= BeanCopyUtils.copy(bo, commonProblem);
        return baseMapper.updateById(commonProblem) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CommonProblemBo entity){
        CommonProblem commonProblem = baseMapper.selectOne(new LambdaQueryWrapper<CommonProblem>()
            .eq(CommonProblem::getProblemTitle, entity.getProblemTitle())
            .last("limit 1")
        );

        if(ObjectUtil.isNotNull(commonProblem) && !commonProblem.getId().equals(entity.getId())) {
            throw new ServiceException(CommonProblemExceptionEnum.COMMON_PROBLEM_NAME_REPEAT);
        }
    }

    /**
     * 删除常见问题
     *
     * @param id 需要删除的常见问题主键
     * @return 结果
     */
    @Override
    public Boolean deleteById(Integer id) {
        this.loadById(id,true);
        return baseMapper.deleteById(id)>0;

    }

    @Override
    public boolean setStatus(Integer id) {
        CommonProblem commonProblem = this.loadById(id, true);
        commonProblem.setStatus(!commonProblem.getStatus());
        baseMapper.updateById(commonProblem);
        return true;
    }

}
