package com.ruoyi.info.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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.springframework.stereotype.Service;
import com.ruoyi.info.order.domain.bo.OrderWorkerChangeRecordBo;
import com.ruoyi.info.order.domain.vo.OrderWorkerChangeRecordVo;
import com.ruoyi.info.order.domain.OrderWorkerChangeRecord;
import com.ruoyi.info.order.mapper.OrderWorkerChangeRecordMapper;
import com.ruoyi.info.order.service.IOrderWorkerChangeRecordService;
import com.ruoyi.info.order.exception.OrderWorkerChangeRecordExceptionEnum;

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

/**
 * 订单更换分配护工记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-29
 */
@RequiredArgsConstructor
@Service
public class OrderWorkerChangeRecordServiceImpl implements IOrderWorkerChangeRecordService {

    private final OrderWorkerChangeRecordMapper baseMapper;

    /**
     * 查询订单更换分配护工记录分页
     *
     * @param bo 订单更换分配护工记录
     * @return 订单更换分配护工记录
     */
    @Override
    public TableDataInfo<OrderWorkerChangeRecordVo> queryPageList(OrderWorkerChangeRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderWorkerChangeRecord> lqw = buildQueryWrapper(bo);
        Page<OrderWorkerChangeRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单更换分配护工记录列表
     *
     * @param bo 订单更换分配护工记录
     * @return 订单更换分配护工记录
     */
    @Override
    public List<OrderWorkerChangeRecordVo> queryList(OrderWorkerChangeRecordBo bo) {
        LambdaQueryWrapper<OrderWorkerChangeRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OrderWorkerChangeRecord> buildQueryWrapper(OrderWorkerChangeRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderWorkerChangeRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, OrderWorkerChangeRecord::getOrderId, bo.getOrderId());
        lqw.eq(bo.getWorkerId() != null, OrderWorkerChangeRecord::getWorkerId, bo.getWorkerId());
        lqw.like(StringUtils.isNotBlank(bo.getWorkerName()), OrderWorkerChangeRecord::getWorkerName, bo.getWorkerName());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkerPhoneNumber()), OrderWorkerChangeRecord::getWorkerPhoneNumber, bo.getWorkerPhoneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), OrderWorkerChangeRecord::getSex, bo.getSex());
        lqw.eq(bo.getOperatingTeacherId() != null, OrderWorkerChangeRecord::getOperatingTeacherId, bo.getOperatingTeacherId());
        lqw.eq(bo.getWorkerStartTime() != null, OrderWorkerChangeRecord::getWorkerStartTime, bo.getWorkerStartTime());
        lqw.eq(bo.getWorkerEndTime() != null, OrderWorkerChangeRecord::getWorkerEndTime, bo.getWorkerEndTime());
        return lqw;
    }

    /**
     * 查询订单更换分配护工记录
     *
     * @param id 订单更换分配护工记录主键
     * @return 订单更换分配护工记录
     */
    @Override
    public OrderWorkerChangeRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 详情订单更换分配护工记录
     *
     * @param id 订单更换分配护工记录主键
     * @return 订单更换分配护工记录
     */
    @Override
    public OrderWorkerChangeRecord loadById(Long id, Boolean tw){
        OrderWorkerChangeRecord info = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(OrderWorkerChangeRecordExceptionEnum.OrderWorkerChangeRecord_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增订单更换分配护工记录
     *
     * @param bo 订单更换分配护工记录
     * @return 结果
     */
    @Override
    public Boolean insertByBo(OrderWorkerChangeRecordBo bo) {
        OrderWorkerChangeRecord add = BeanUtil.toBean(bo, OrderWorkerChangeRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单更换分配护工记录
     *
     * @param bo 订单更换分配护工记录
     * @return 结果
     */
    @Override
    public Boolean updateByBo(OrderWorkerChangeRecordBo bo) {
        OrderWorkerChangeRecord orderWorkerChangeRecord = baseMapper.selectById(bo.getId());
        OrderWorkerChangeRecord update = BeanCopyUtils.copy(bo, orderWorkerChangeRecord);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(OrderWorkerChangeRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单更换分配护工记录
     *
     * @param ids 需要删除的订单更换分配护工记录主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public OrderWorkerChangeRecordVo queryByOrderAndWorker(Long orderId, Long workerId) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<OrderWorkerChangeRecord>()
            .eq(OrderWorkerChangeRecord::getOrderId, orderId)
            .eq(OrderWorkerChangeRecord::getWorkerId, workerId)
            .orderByDesc(OrderWorkerChangeRecord::getCreateTime)
            .last("limit 1")
        );
    }

}
