package com.ruoyi.clock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.clock.exception.ClockProductRecordExceptionEnum;
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.clock.domain.bo.ClockProductRecordBo;
import com.ruoyi.clock.domain.vo.ClockProductRecordVo;
import com.ruoyi.clock.domain.ClockProductRecord;
import com.ruoyi.clock.mapper.ClockProductRecordMapper;
import com.ruoyi.clock.service.IClockProductRecordService;

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

/**
 * 打卡记录关联产品信息Service业务层处理
 *
 * @author pengc
 * @date 2026-03-04
 */
@RequiredArgsConstructor
@Service
public class ClockProductRecordServiceImpl implements IClockProductRecordService {

    private final ClockProductRecordMapper baseMapper;

    /**
     * 查询打卡记录关联产品信息分页
     *
     * @param bo 打卡记录关联产品信息
     * @return 打卡记录关联产品信息
     */
    @Override
    public TableDataInfo<ClockProductRecordVo> queryPageList(ClockProductRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClockProductRecord> lqw = buildQueryWrapper(bo);
        Page<ClockProductRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询打卡记录关联产品信息列表
     *
     * @param bo 打卡记录关联产品信息
     * @return 打卡记录关联产品信息
     */
    @Override
    public List<ClockProductRecordVo> queryList(ClockProductRecordBo bo) {
        LambdaQueryWrapper<ClockProductRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClockProductRecord> buildQueryWrapper(ClockProductRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClockProductRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRecordId() != null, ClockProductRecord::getRecordId, bo.getRecordId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), ClockProductRecord::getProductName, bo.getProductName());
        lqw.eq(bo.getAmount() != null, ClockProductRecord::getAmount, bo.getAmount());
        return lqw;
    }

    /**
     * 查询打卡记录关联产品信息
     *
     * @param recordDetailId 打卡记录关联产品信息主键
     * @return 打卡记录关联产品信息
     */
    @Override
    public ClockProductRecordVo queryById(Long recordDetailId){
        return baseMapper.selectVoById(recordDetailId);
    }

    /**
     * 详情打卡记录关联产品信息
     *
     * @param recordDetailId 打卡记录关联产品信息主键
     * @return 打卡记录关联产品信息
     */
    @Override
    public ClockProductRecord loadById(Long recordDetailId, Boolean tw){
        ClockProductRecord info = this.baseMapper.selectById(recordDetailId);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(ClockProductRecordExceptionEnum.ClockProductRecord_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增打卡记录关联产品信息
     *
     * @param bo 打卡记录关联产品信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(ClockProductRecordBo bo) {
        ClockProductRecord add = BeanUtil.toBean(bo, ClockProductRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordDetailId(add.getRecordDetailId());
        }
        return flag;
    }

    /**
     * 修改打卡记录关联产品信息
     *
     * @param bo 打卡记录关联产品信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(ClockProductRecordBo bo) {
        ClockProductRecord clockProductRecord = baseMapper.selectById(bo.getRecordDetailId());
        ClockProductRecord update = BeanCopyUtils.copy(bo, clockProductRecord);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除打卡记录关联产品信息
     *
     * @param ids 需要删除的打卡记录关联产品信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
