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 com.ruoyi.oa.domain.bo.OaSalaryDetailBo;
import com.ruoyi.oa.domain.vo.OaSalaryDetailVo;
import com.ruoyi.oa.domain.OaSalaryDetail;
import com.ruoyi.oa.mapper.OaSalaryDetailMapper;
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 OaSalaryDetailServiceImpl implements IOaSalaryDetailService {

    private final OaSalaryDetailMapper baseMapper;

    /**
     * 查询工资发放明细
     */
    @Override
    public OaSalaryDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

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

    /**
     * 查询工资发放明细列表
     */
    @Override
    public List<OaSalaryDetailVo> queryList(OaSalaryDetailBo bo) {
        LambdaQueryWrapper<OaSalaryDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaSalaryDetail> buildQueryWrapper(OaSalaryDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaSalaryDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMainId() != null, OaSalaryDetail::getMainId, bo.getMainId());
        lqw.eq(bo.getSerialNumber() != null, OaSalaryDetail::getSerialNumber, bo.getSerialNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getDept()), OaSalaryDetail::getDept, bo.getDept());
        lqw.like(StringUtils.isNotBlank(bo.getName()), OaSalaryDetail::getName, bo.getName());
        lqw.eq(bo.getBasicSalary() != null, OaSalaryDetail::getBasicSalary, bo.getBasicSalary());
        lqw.eq(bo.getPostSalary() != null, OaSalaryDetail::getPostSalary, bo.getPostSalary());
        lqw.eq(bo.getMealAllowance() != null, OaSalaryDetail::getMealAllowance, bo.getMealAllowance());
        lqw.eq(bo.getHousingAllowance() != null, OaSalaryDetail::getHousingAllowance, bo.getHousingAllowance());
        lqw.eq(bo.getBusAllowance() != null, OaSalaryDetail::getBusAllowance, bo.getBusAllowance());
        lqw.eq(bo.getBusinessDaysOther() != null, OaSalaryDetail::getBusinessDaysOther, bo.getBusinessDaysOther());
        lqw.eq(bo.getBusinessAllowance() != null, OaSalaryDetail::getBusinessAllowance, bo.getBusinessAllowance());
        lqw.eq(bo.getSocialSecurityAllowance() != null, OaSalaryDetail::getSocialSecurityAllowance, bo.getSocialSecurityAllowance());
        lqw.eq(bo.getOvertimeHours() != null, OaSalaryDetail::getOvertimeHours, bo.getOvertimeHours());
        lqw.eq(bo.getOvertimeRate() != null, OaSalaryDetail::getOvertimeRate, bo.getOvertimeRate());
        lqw.eq(bo.getOvertimeTotal() != null, OaSalaryDetail::getOvertimeTotal, bo.getOvertimeTotal());
        lqw.eq(bo.getBusinessDays() != null, OaSalaryDetail::getBusinessDays, bo.getBusinessDays());
        lqw.eq(bo.getBonuses() != null, OaSalaryDetail::getBonuses, bo.getBonuses());
        lqw.eq(bo.getLeaveDeduction() != null, OaSalaryDetail::getLeaveDeduction, bo.getLeaveDeduction());
        lqw.eq(bo.getOtherDeduction() != null, OaSalaryDetail::getOtherDeduction, bo.getOtherDeduction());
        lqw.eq(bo.getGrossSalary() != null, OaSalaryDetail::getGrossSalary, bo.getGrossSalary());
        lqw.eq(bo.getPersonalPension() != null, OaSalaryDetail::getPersonalPension, bo.getPersonalPension());
        lqw.eq(bo.getPersonalMedical() != null, OaSalaryDetail::getPersonalMedical, bo.getPersonalMedical());
        lqw.eq(bo.getPersonalUnemployment() != null, OaSalaryDetail::getPersonalUnemployment, bo.getPersonalUnemployment());
        lqw.eq(bo.getPersonalBigMedical() != null, OaSalaryDetail::getPersonalBigMedical, bo.getPersonalBigMedical());
        lqw.eq(bo.getPersonalHousingFund() != null, OaSalaryDetail::getPersonalHousingFund, bo.getPersonalHousingFund());
        lqw.eq(bo.getPersonalTax() != null, OaSalaryDetail::getPersonalTax, bo.getPersonalTax());
        lqw.eq(bo.getBonusesDeducted() != null, OaSalaryDetail::getBonusesDeducted, bo.getBonusesDeducted());
        lqw.eq(bo.getNetSalary() != null, OaSalaryDetail::getNetSalary, bo.getNetSalary());
        lqw.eq(bo.getEnterprisePension() != null, OaSalaryDetail::getEnterprisePension, bo.getEnterprisePension());
        lqw.eq(bo.getEnterpriseMedical() != null, OaSalaryDetail::getEnterpriseMedical, bo.getEnterpriseMedical());
        lqw.eq(bo.getEnterpriseInjury() != null, OaSalaryDetail::getEnterpriseInjury, bo.getEnterpriseInjury());
        lqw.eq(bo.getEnterpriseUnemployment() != null, OaSalaryDetail::getEnterpriseUnemployment, bo.getEnterpriseUnemployment());
        lqw.eq(bo.getEnterpriseMaternity() != null, OaSalaryDetail::getEnterpriseMaternity, bo.getEnterpriseMaternity());
        lqw.eq(bo.getEnterpriseHousingFund() != null, OaSalaryDetail::getEnterpriseHousingFund, bo.getEnterpriseHousingFund());
        lqw.eq(bo.getEnterpriseBigMedical() != null, OaSalaryDetail::getEnterpriseBigMedical, bo.getEnterpriseBigMedical());
        lqw.eq(bo.getUnitTotalExpense() != null, OaSalaryDetail::getUnitTotalExpense, bo.getUnitTotalExpense());
        return lqw;
    }

    /**
     * 新增工资发放明细
     */
    @Override
    public Boolean insertByBo(OaSalaryDetailBo bo) {
        OaSalaryDetail add = BeanUtil.toBean(bo, OaSalaryDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

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

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

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

    /**
     * 批量插入工资发放明细
     */
    @Override
    public Boolean insertBatchByBo(List<OaSalaryDetailBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return true;
        }
        List<OaSalaryDetail> detailList = BeanUtil.copyToList(boList, OaSalaryDetail.class);
        // 数据校验
        for (OaSalaryDetail detail : detailList) {
            validEntityBeforeSave(detail);
        }
        return baseMapper.insertBatch(detailList);
    }
}
