package org.dromara.work.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 org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
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.work.domain.TpOrderComment;
import org.dromara.work.domain.TpOrderRecord;
import org.dromara.work.domain.bo.TpOrderCommentBo;
import org.dromara.work.domain.vo.TpOrderCommentVo;
import org.dromara.work.mapper.TpOrderCommentMapper;
import org.dromara.work.mapper.TpOrderRecordMapper;
import org.dromara.work.service.ITpOrderCommentService;
import org.springframework.stereotype.Service;

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

/**
 * 订单评论Service业务层处理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@RequiredArgsConstructor
@Service
public class TpOrderCommentServiceImpl implements ITpOrderCommentService {

    private final TpOrderCommentMapper baseMapper;

    private final TpOrderRecordMapper recordMapper;

    /**
     * 查询订单评论
     *
     * @param id 主键
     * @return 订单评论
     */
    @Override
    public TpOrderCommentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询订单评论列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单评论分页列表
     */
    @Override
    public TableDataInfo<TpOrderCommentVo> queryPageList(TpOrderCommentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TpOrderComment> lqw = buildQueryWrapper(bo);
        Page<TpOrderCommentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单评论列表
     *
     * @param bo 查询条件
     * @return 订单评论列表
     */
    @Override
    public List<TpOrderCommentVo> queryList(TpOrderCommentBo bo) {
        LambdaQueryWrapper<TpOrderComment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TpOrderComment> buildQueryWrapper(TpOrderCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TpOrderComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOid() != null, TpOrderComment::getOid, bo.getOid());
        lqw.eq(bo.getUid() != null, TpOrderComment::getUid, bo.getUid());
        lqw.eq(bo.getGrade() != null, TpOrderComment::getGrade, bo.getGrade());
        lqw.eq(bo.getSpeed() != null, TpOrderComment::getSpeed, bo.getSpeed());
        lqw.eq(bo.getQuality() != null, TpOrderComment::getQuality, bo.getQuality());
        lqw.eq(bo.getServe() != null, TpOrderComment::getServe, bo.getServe());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), TpOrderComment::getContent, bo.getContent());
        lqw.eq(bo.getAddTime() != null, TpOrderComment::getAddTime, bo.getAddTime());
        lqw.eq(bo.getType() != null, TpOrderComment::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增订单评论
     *
     * @param bo 订单评论
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TpOrderCommentBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getIdentity() != 3){
            throw new ServiceException("只有客服才可以评论");
        }
        bo.setUid(loginUser.getUserId());
        bo.setType(2L);
        bo.setAddTime(new Date());
        TpOrderComment add = MapstructUtils.convert(bo, TpOrderComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            saveOrderRecord(add.getOid(),"客服评论","评论内容:"+add.getContent());
        }
        return flag;
    }

    /**
     * 修改订单评论
     *
     * @param bo 订单评论
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TpOrderCommentBo bo) {
        TpOrderComment update = MapstructUtils.convert(bo, TpOrderComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除订单评论信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    public Boolean saveOrderRecord(Long orderId, String type,String content) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        TpOrderRecord record = new TpOrderRecord();
        record.setOid(orderId);
        record.setType(type);
        record.setContent(content);
        record.setAddUser(loginUser.getUserId());
        record.setAddTime(new Date());
        record.setState(1);
        return recordMapper.insert(record) > 0;
    }
}
