package org.dromara.elevator.service.impl;

import org.apache.commons.collections4.CollectionUtils;
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.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.elevator.constant.WorkStateEnum;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EExpertConsultBo;
import org.dromara.elevator.domain.vo.EExpertConsultVo;
import org.dromara.elevator.domain.EExpertConsult;
import org.dromara.elevator.mapper.EExpertConsultMapper;
import org.dromara.elevator.service.IEExpertConsultService;

import java.util.*;

/**
 * 专家咨询Service业务层处理
 *
 * @author ym
 * @date 2024-10-29
 */
@RequiredArgsConstructor
@Service
public class EExpertConsultServiceImpl implements IEExpertConsultService {

    private final EExpertConsultMapper baseMapper;

    /**
     * 查询专家咨询
     *
     * @param id 主键
     * @return 专家咨询
     */
    @Override
    public EExpertConsultVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询专家咨询列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 专家咨询分页列表
     */
    @Override
    public TableDataInfo<EExpertConsultVo> queryPageList(EExpertConsultBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EExpertConsult> lqw = buildQueryWrapper(bo);
        Page<EExpertConsultVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的专家咨询列表
     *
     * @param bo 查询条件
     * @return 专家咨询列表
     */
    @Override
    public List<EExpertConsultVo> queryList(EExpertConsultBo bo) {
        LambdaQueryWrapper<EExpertConsult> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EExpertConsult> buildQueryWrapper(EExpertConsultBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EExpertConsult> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getConsultType() != null, EExpertConsult::getConsultType, bo.getConsultType());
        lqw.eq(StringUtils.isNotBlank(bo.getConsultDesc()), EExpertConsult::getConsultDesc, bo.getConsultDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getConsultAttachment()), EExpertConsult::getConsultAttachment, bo.getConsultAttachment());
        lqw.eq(bo.getExpectTime() != null, EExpertConsult::getExpectTime, bo.getExpectTime());
        lqw.like(StringUtils.isNotBlank(bo.getCreateName()), EExpertConsult::getCreateName, bo.getCreateName());
        lqw.eq(bo.getState() != null, EExpertConsult::getState, bo.getState());
        lqw.eq(bo.getExpectMode() != null, EExpertConsult::getExpectMode, bo.getExpectMode());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EExpertConsult::getElevatorNum, bo.getElevatorNum());
        lqw.eq(bo.getReceiveUserId() != null, EExpertConsult::getReceiveUserId, bo.getReceiveUserId());
        lqw.eq(bo.getReceiveTime() != null, EExpertConsult::getReceiveTime, bo.getReceiveTime());
        lqw.like(StringUtils.isNotBlank(bo.getReceiveUserName()), EExpertConsult::getReceiveUserName, bo.getReceiveUserName());
        lqw.eq(bo.getFinishTime() != null, EExpertConsult::getFinishTime, bo.getFinishTime());
        lqw.eq(bo.getAuditState() != null, EExpertConsult::getAuditState, bo.getAuditState());
        lqw.eq(bo.getAuditTime() != null, EExpertConsult::getAuditTime, bo.getAuditTime());
        lqw.in(CollectionUtils.isNotEmpty(bo.getStateList()), EExpertConsult::getState, bo.getStateList());
        lqw.orderByDesc(EExpertConsult::getCreateTime);
        return lqw;
    }

    /**
     * 新增专家咨询
     *
     * @param bo 专家咨询
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EExpertConsultBo bo) {
        EExpertConsult add = MapstructUtils.convert(bo, EExpertConsult.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改专家咨询
     *
     * @param bo 专家咨询
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EExpertConsultBo bo) {
        EExpertConsult update = MapstructUtils.convert(bo, EExpertConsult.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public Boolean receive(EExpertConsultBo bo) {
        EExpertConsult consult = getAndCheck(bo.getId(), WorkStateEnum.NEW);
        consult.setReceiveUserId(LoginHelper.getUserId());
        consult.setReceiveTime(new Date());
        consult.setReceiveUserName(LoginHelper.getUsername());
        consult.setState(WorkStateEnum.RECEIVE.getState());
        return this.baseMapper.updateById(consult) > 0;
    }

    @Override
    public Boolean complete(EExpertConsultBo bo) {
        EExpertConsult consult = getAndCheck(bo.getId(), WorkStateEnum.RECEIVE);
        consult.setSolveDesc(bo.getSolveDesc());
        consult.setSolveAttachment(bo.getSolveAttachment());
        consult.setFinishTime(new Date());
        consult.setState(WorkStateEnum.COMPLETE.getState());
        consult.setSolveUserId(LoginHelper.getUserId());
        consult.setSolveUserName(LoginHelper.getUsername());
        return this.baseMapper.updateById(consult) > 0;
    }

    @Override
    public Boolean audit(EExpertConsultBo bo) {
        EExpertConsult consult = getAndCheck(bo.getId(), WorkStateEnum.COMPLETE);
        consult.setAuditState(1);
        consult.setAuditTime(new Date());
        consult.setAuditRemark(bo.getAuditRemark());
        consult.setAuditAttachment(bo.getAuditAttachment());
        consult.setAuditUserId(LoginHelper.getUserId());
        consult.setAuditUserName(LoginHelper.getUsername());
        return this.baseMapper.updateById(consult) > 0;
    }

    /**
     * 查询并校验
     *
     * @param id          工单id
     * @param beforeState 前置状态
     * @return 咨询工单
     */
    private EExpertConsult getAndCheck(Long id, WorkStateEnum beforeState) {
        EExpertConsult expertConsult = this.baseMapper.selectById(id);
        if (Objects.isNull(expertConsult)) {
            throw new ServiceException("未查询到该工单");
        }
        if (!Objects.equals(expertConsult.getState(), beforeState.getState())) {
            throw new ServiceException("咨询工单状态不正确");
        }
        return expertConsult;
    }
}
