package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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 org.springframework.transaction.annotation.Transactional;
import com.ruoyi.oa.domain.bo.OaSalaryMasterBo;
import com.ruoyi.oa.domain.bo.OaSalaryDetailBo;
import com.ruoyi.oa.domain.vo.OaSalaryMasterVo;
import com.ruoyi.oa.domain.OaSalaryMaster;
import com.ruoyi.oa.domain.OaSalaryDetail;
import com.ruoyi.oa.mapper.OaSalaryMasterMapper;
import com.ruoyi.oa.service.IOaSalaryMasterService;
import com.ruoyi.oa.service.IOaSalaryDetailService;

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

/**
 * 工资发放主Service业务层处理
 *
 * @author Joshi
 * @date 2025-09-08
 */
@RequiredArgsConstructor
@Service
public class OaSalaryMasterServiceImpl implements IOaSalaryMasterService {

    private final OaSalaryMasterMapper baseMapper;
    private final IOaSalaryDetailService salaryDetailService;

    /**
     * 查询工资发放主
     */
    @Override
    public OaSalaryMasterVo queryById(Long masterId){
        return baseMapper.selectVoById(masterId);
    }

    /**
     * 查询工资发放主列表
     */
    @Override
    public TableDataInfo<OaSalaryMasterVo> queryPageList(OaSalaryMasterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaSalaryMaster> lqw = buildQueryWrapper(bo);
        Page<OaSalaryMasterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工资发放主列表
     */
    @Override
    public List<OaSalaryMasterVo> queryList(OaSalaryMasterBo bo) {
        LambdaQueryWrapper<OaSalaryMaster> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaSalaryMaster> buildQueryWrapper(OaSalaryMasterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaSalaryMaster> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUnitName()), OaSalaryMaster::getUnitName, bo.getUnitName());
        lqw.eq(StringUtils.isNotBlank(bo.getSalaryPeriod()), OaSalaryMaster::getSalaryPeriod, bo.getSalaryPeriod());
        lqw.eq(StringUtils.isNotBlank(bo.getGmApproval()), OaSalaryMaster::getGmApproval, bo.getGmApproval());
        lqw.eq(StringUtils.isNotBlank(bo.getFinanceAudit()), OaSalaryMaster::getFinanceAudit, bo.getFinanceAudit());
        lqw.eq(StringUtils.isNotBlank(bo.getCashier()), OaSalaryMaster::getCashier, bo.getCashier());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptManager()), OaSalaryMaster::getDeptManager, bo.getDeptManager());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), OaSalaryMaster::getOperator, bo.getOperator());
        lqw.eq(bo.getSalaryStatus() != null, OaSalaryMaster::getSalaryStatus, bo.getSalaryStatus());
        return lqw;
    }

    /**
     * 新增工资发放主
     */
    @Override
    public Boolean insertByBo(OaSalaryMasterBo bo) {
        OaSalaryMaster add = BeanUtil.toBean(bo, OaSalaryMaster.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMasterId(add.getMasterId());
        }
        return flag;
    }

    /**
     * 修改工资发放主
     */
    @Override
    public Boolean updateByBo(OaSalaryMasterBo bo) {
        OaSalaryMaster update = BeanUtil.toBean(bo, OaSalaryMaster.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 批量插入工资明细
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchInsertDetails(OaSalaryMasterBo bo) {
        // 1. 先插入或更新主表信息
        OaSalaryMaster master;
        if (bo.getMasterId() != null) {
            // 如果有主键，则更新主表
            master = BeanUtil.toBean(bo, OaSalaryMaster.class);
            validEntityBeforeSave(master);
            baseMapper.updateById(master);
        } else {
            // 如果没有主键，则新增主表
            master = BeanUtil.toBean(bo, OaSalaryMaster.class);
            validEntityBeforeSave(master);
            baseMapper.insert(master);
            bo.setMasterId(master.getMasterId());
        }

        // 2. 批量插入明细
        if (CollUtil.isNotEmpty(bo.getSalaryDetailList())) {
            // 设置主表ID
            for (OaSalaryDetailBo detail : bo.getSalaryDetailList()) {
                detail.setMainId(master.getMasterId());
            }
            // 批量插入明细
            return salaryDetailService.insertBatchByBo(bo.getSalaryDetailList());
        }
        
        return true;
    }
}
