package com.ruoyi.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.PaySalaryReportBo;
import com.ruoyi.pay.domain.vo.PaySalaryReportVo;
import com.ruoyi.pay.domain.PaySalaryReport;
import com.ruoyi.pay.mapper.PaySalaryReportMapper;
import com.ruoyi.pay.service.IPaySalaryReportService;

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

/**
 * 工资明细总Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RequiredArgsConstructor
@Service
public class PaySalaryReportServiceImpl implements IPaySalaryReportService {

    private final PaySalaryReportMapper baseMapper;

    /**
     * 查询工资明细总
     */
    @Override
    public PaySalaryReportVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

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

    /**
     * 查询最近一个月的工资
     */
    @Override
    public TableDataInfo<PaySalaryReportVo> queryLastMonthReport(Long tenantId, PageQuery pageQuery) {
        LambdaQueryWrapper<PaySalaryReport> lqw = new LambdaQueryWrapper<>();
        lqw.eq(tenantId != null, PaySalaryReport::getTenantId, tenantId);
        Page<PaySalaryReportVo> result =  baseMapper.queryLastMonthReport(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工资明细总列表
     */
    @Override
    public List<PaySalaryReportVo> queryList(PaySalaryReportBo bo) {
        LambdaQueryWrapper<PaySalaryReport> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PaySalaryReport> buildQueryWrapper(PaySalaryReportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PaySalaryReport> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTenantId() != null, PaySalaryReport::getTenantId, bo.getTenantId());
        lqw.eq(StringUtils.isNotBlank(bo.getYear()), PaySalaryReport::getYear, bo.getYear());
        lqw.eq(StringUtils.isNotBlank(bo.getMonth()), PaySalaryReport::getMonth, bo.getMonth());
        lqw.eq(bo.getDeptId() != null, PaySalaryReport::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), PaySalaryReport::getDeptName, bo.getDeptName());
        lqw.eq(bo.getEmployeeId() != null, PaySalaryReport::getEmployeeId, bo.getEmployeeId());
        lqw.like(StringUtils.isNotBlank(bo.getEmployeeName()), PaySalaryReport::getEmployeeName, bo.getEmployeeName());
        lqw.eq(bo.getBasicSalary() != null, PaySalaryReport::getBasicSalary, bo.getBasicSalary());
        lqw.eq(bo.getActuallyDays() != null, PaySalaryReport::getActuallyDays, bo.getActuallyDays());
        lqw.eq(bo.getActuallyHours() != null, PaySalaryReport::getActuallyHours, bo.getActuallyHours());
        lqw.eq(bo.getDayWorkHours() != null, PaySalaryReport::getDayWorkHours, bo.getDayWorkHours());
        lqw.eq(bo.getWorkOrderHours() != null, PaySalaryReport::getWorkOrderHours, bo.getWorkOrderHours());
        lqw.eq(bo.getOvertimeWorkHours() != null, PaySalaryReport::getOvertimeWorkHours, bo.getOvertimeWorkHours());
        lqw.eq(bo.getRigongHelpHours() != null, PaySalaryReport::getRigongHelpHours, bo.getRigongHelpHours());
        lqw.eq(bo.getDayWorkPay() != null, PaySalaryReport::getDayWorkPay, bo.getDayWorkPay());
        lqw.eq(bo.getWorkOrderPay() != null, PaySalaryReport::getWorkOrderPay, bo.getWorkOrderPay());
        lqw.eq(bo.getOvertime() != null, PaySalaryReport::getOvertime, bo.getOvertime());
        lqw.eq(bo.getBanggongPay() != null, PaySalaryReport::getBanggongPay, bo.getBanggongPay());
        lqw.eq(bo.getZhibanOrLahuo() != null, PaySalaryReport::getZhibanOrLahuo, bo.getZhibanOrLahuo());
        lqw.eq(bo.getHoliday() != null, PaySalaryReport::getHoliday, bo.getHoliday());
        lqw.eq(bo.getPost() != null, PaySalaryReport::getPost, bo.getPost());
        lqw.eq(bo.getPhoneCharge() != null, PaySalaryReport::getPhoneCharge, bo.getPhoneCharge());
        lqw.eq(bo.getContract() != null, PaySalaryReport::getContract, bo.getContract());
        lqw.eq(bo.getFullTime() != null, PaySalaryReport::getFullTime, bo.getFullTime());
        lqw.eq(bo.getDiffOfLastMonth() != null, PaySalaryReport::getDiffOfLastMonth, bo.getDiffOfLastMonth());
        lqw.eq(bo.getShoudPayMoney() != null, PaySalaryReport::getShoudPayMoney, bo.getShoudPayMoney());
        lqw.eq(bo.getLate() != null, PaySalaryReport::getLate, bo.getLate());
        lqw.eq(bo.getInsurance() != null, PaySalaryReport::getInsurance, bo.getInsurance());
        lqw.eq(bo.getActuallyPayMoney() != null, PaySalaryReport::getActuallyPayMoney, bo.getActuallyPayMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getAllButieData()), PaySalaryReport::getAllButieData, bo.getAllButieData());
        lqw.eq(StringUtils.isNotBlank(bo.getNote()), PaySalaryReport::getNote, bo.getNote());
        lqw.eq(bo.getEmpQuery() != null, PaySalaryReport::getEmpQuery, bo.getEmpQuery());
        return lqw;
    }

    /**
     * 新增工资明细总
     */
    @Override
    public Boolean insertByBo(PaySalaryReportBo bo) {
        PaySalaryReport add = BeanUtil.toBean(bo, PaySalaryReport.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改工资明细总
     */
    @Override
    public Boolean updateByBo(PaySalaryReportBo bo) {
        PaySalaryReport update = BeanUtil.toBean(bo, PaySalaryReport.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }
    @Override
    public Boolean updateByBo2(PaySalaryReport entity) {
        PaySalaryReportBo bo = BeanUtil.toBean(entity, PaySalaryReportBo.class);
        UpdateWrapper<PaySalaryReport> upw =new UpdateWrapper<>();
        upw.set(bo.getEmpQuery() != null,"emp_query",bo.getEmpQuery())
            .eq("year",entity.getYear()).eq("month",entity.getMonth());
        return baseMapper.update(entity,upw) > 0;
    }

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

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

}
