package com.ruoyi.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.pay.domain.bo.PayWorkOrderEmployeeBo;
import com.ruoyi.pay.domain.vo.PayWorkOrderEmployeeVo;
import com.ruoyi.pay.domain.PayWorkOrderEmployee;
import com.ruoyi.pay.mapper.PayWorkOrderEmployeeMapper;
import com.ruoyi.pay.service.IPayWorkOrderEmployeeService;

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

/**
 * 工单和员工联系Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PayWorkOrderEmployeeServiceImpl implements IPayWorkOrderEmployeeService {

    private final PayWorkOrderEmployeeMapper baseMapper;

    /**
     * 查询工单和员工联系
     */
    @Override
    public PayWorkOrderEmployeeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工单和员工联系列表
     */
    @Override
    public TableDataInfo<PayWorkOrderEmployeeVo> queryPageList(PayWorkOrderEmployeeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayWorkOrderEmployee> lqw = buildQueryWrapper(bo);
        Page<PayWorkOrderEmployeeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工单和员工联系列表
     */
    @Override
    public List<PayWorkOrderEmployeeVo> queryList(PayWorkOrderEmployeeBo bo) {
        LambdaQueryWrapper<PayWorkOrderEmployee> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayWorkOrderEmployee> buildQueryWrapper(PayWorkOrderEmployeeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayWorkOrderEmployee> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTenantId() != null, PayWorkOrderEmployee::getTenantId, bo.getTenantId());
        lqw.eq(bo.getWorkOrderId() != null, PayWorkOrderEmployee::getWorkOrderId, bo.getWorkOrderId());
        lqw.eq(bo.getEmployeeId() != null, PayWorkOrderEmployee::getEmployeeId, bo.getEmployeeId());
        lqw.eq(bo.getDeptId() != null, PayWorkOrderEmployee::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), PayWorkOrderEmployee::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getEmpName()), PayWorkOrderEmployee::getEmpName, bo.getEmpName());
        lqw.eq(bo.getStartTime() != null, PayWorkOrderEmployee::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, PayWorkOrderEmployee::getEndTime, bo.getEndTime());
        lqw.eq(bo.getValidHours() != null, PayWorkOrderEmployee::getValidHours, bo.getValidHours());
        lqw.eq(bo.getDate() != null, PayWorkOrderEmployee::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getMonth()), PayWorkOrderEmployee::getMonth, bo.getMonth());
        lqw.eq(StringUtils.isNotBlank(bo.getProcessingType()), PayWorkOrderEmployee::getProcessingType, bo.getProcessingType());
        lqw.eq(bo.getProcessingPrice() != null, PayWorkOrderEmployee::getProcessingPrice, bo.getProcessingPrice());
        lqw.eq(bo.getProcessingNum() != null, PayWorkOrderEmployee::getProcessingNum, bo.getProcessingNum());
        lqw.eq(bo.getAmount() != null, PayWorkOrderEmployee::getAmount, bo.getAmount());
        lqw.eq(bo.getHelper() != null, PayWorkOrderEmployee::getHelper, bo.getHelper());
        lqw.eq(bo.getIntime() != null, PayWorkOrderEmployee::getIntime, bo.getIntime());
        return lqw;
    }

    /**
     * 新增工单和员工联系
     */
    @Override
    public Boolean insertByBo(PayWorkOrderEmployeeBo bo) {
        PayWorkOrderEmployee add = BeanUtil.toBean(bo, PayWorkOrderEmployee.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工单和员工联系
     */
    @Override
    public Boolean updateByBo(PayWorkOrderEmployeeBo bo) {
        PayWorkOrderEmployee update = BeanUtil.toBean(bo, PayWorkOrderEmployee.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除工单和员工联系
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
