package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.dto.StaffManageDTO;
import org.dromara.common.core.utils.DateUtils;
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.domain.staffTrainTime.StaffTrainTime;
import org.dromara.domain.staffTrainTime.bo.StaffTrainTimeBo;
import org.dromara.domain.staffTrainTime.vo.StaffTrainTimeVo;
import org.dromara.mapper.StaffTrainTimeMapper;
import org.dromara.service.IStaffTrainTimeService;
import org.springframework.stereotype.Service;

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

/**
 * 员工培训时长Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class StaffTrainTimeServiceImpl implements IStaffTrainTimeService {

    private final StaffTrainTimeMapper baseMapper;

    /**
     * 查询员工培训时长
     *
     * @param trainTimeId 主键
     * @return 员工培训时长
     */
    @Override
    public StaffTrainTimeVo queryById(Long trainTimeId) {
        return baseMapper.selectVoById(trainTimeId);
    }

    /**
     * 分页查询员工培训时长列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 员工培训时长分页列表
     */
    @Override
    public TableDataInfo<StaffTrainTimeVo> queryPageList(StaffTrainTimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StaffTrainTime> lqw = buildQueryWrapper(bo);
        pageQuery.setOrderByColumn("staff_time");
        IPage<StaffTrainTimeVo> staffTrainTimeVoIPage = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(staffTrainTimeVoIPage);
    }

    /**
     * 查询符合条件的员工培训时长列表
     *
     * @param bo 查询条件
     * @return 员工培训时长列表
     */
    @Override
    public List<StaffTrainTimeVo> queryList(StaffTrainTimeBo bo) {
        LambdaQueryWrapper<StaffTrainTime> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StaffTrainTime> buildQueryWrapper(StaffTrainTimeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StaffTrainTime> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(StaffTrainTime::getTrainTimeId);
        lqw.eq(bo.getStaffId() != null, StaffTrainTime::getStaffId, bo.getStaffId());
        lqw.like(StringUtils.isNotBlank(bo.getStaffName()), StaffTrainTime::getStaffName, bo.getStaffName());
        lqw.eq(bo.getUserId() != null, StaffTrainTime::getUserId, bo.getUserId());
        lqw.eq(bo.getManualId() != null, StaffTrainTime::getManualId, bo.getManualId());
        lqw.eq(StringUtils.isNotBlank(bo.getStaffTime()), StaffTrainTime::getStaffTime, bo.getStaffTime());
        return lqw;
    }

    /**
     * 新增员工培训时长
     *
     * @param bo 员工培训时长
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(StaffTrainTimeBo bo) {
        boolean flag = true;
        StaffTrainTime add = MapstructUtils.convert(bo, StaffTrainTime.class);
        validEntityBeforeSave(add);
        Long userId = LoginHelper.getUserId();
        add.setUserId(userId);
        StaffTrainTime staffTrainTime = baseMapper.selectOne(new LambdaQueryWrapper<StaffTrainTime>().eq(StaffTrainTime::getUserId, userId));
        if (staffTrainTime != null) {
            int staffTime = staffTrainTime.getStaffTime();
            //更新阅读时长
            staffTrainTime.setStaffTime(staffTime + staffTrainTime.getStaffTime());
            Long trainTimeId = staffTrainTime.getTrainTimeId();
            staffTrainTime.setTrainTimeId(trainTimeId);
            flag = baseMapper.updateById(staffTrainTime) > 0;
            return flag;
        }

        StaffManageDTO staffManage = LoginHelper.getLoginUser().getStaffManage();
        if (staffManage != null) {
            staffTrainTime.setStaffId(staffManage.getStaffId());
            staffTrainTime.setStaffName(staffManage.getStaffName());
            staffTrainTime.setCreateTime(DateUtils.getNowDate());
            flag = baseMapper.insert(add) > 0;
        }
        if (flag) {
            bo.setTrainTimeId(add.getTrainTimeId());
        }
        return flag;
    }

    /**
     * 修改员工培训时长
     *
     * @param bo 员工培训时长
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(StaffTrainTimeBo bo) {
        StaffTrainTime update = MapstructUtils.convert(bo, StaffTrainTime.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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