package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 com.ruoyi.oa.domain.*;
import com.ruoyi.oa.domain.bo.SysOaProjectBo;
import com.ruoyi.oa.domain.vo.*;
import com.ruoyi.oa.mapper.*;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.SysOaCostBo;
import com.ruoyi.oa.service.ISysOaCostService;

import java.util.*;

/**
 * 项目成本分析Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-04
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class SysOaCostServiceImpl implements ISysOaCostService {

    private final SysOaCostMapper baseMapper;

    private final SysUserMapper userMapper;

    private final SysOaClaimMapper claimMapper;

    private final SysOaWarehouseMapper warehouseMapper;

    private final SysOaWarehouseDetailMapper outWarehouseMapper;

    private final SysOaAttendanceMapper attendanceMapper;

    private final SysOaProjectMapper projectMapper;

    /**
     * 查询项目成本分析
     */
    @Override
    public SysOaCostVo queryById(Long costId){
        return baseMapper.selectVoById(costId);
    }

    /**
     * 查询项目成本分析列表
     */
    @Override
    public TableDataInfo<SysOaCostVo> queryPageList(SysOaCostBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOaCost> lqw = buildQueryWrapper(bo);
        Page<SysOaCostVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目成本分析列表
     */
    @Override
    public List<SysOaCostVo> queryList(SysOaCostBo bo) {
        LambdaQueryWrapper<SysOaCost> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOaCost> buildQueryWrapper(SysOaCostBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaCost> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, SysOaCost::getProjectId, bo.getProjectId());
        lqw.eq(bo.getCost() != null, SysOaCost::getCost, bo.getCost());
        lqw.eq(bo.getCostType() != null, SysOaCost::getCostType, bo.getCostType());
        lqw.like(StringUtils.isNotBlank(bo.getRemark()), SysOaCost::getRemark, bo.getRemark());
        lqw.eq(bo.getMaterialId() != null, SysOaCost::getMaterialId, bo.getMaterialId());
        lqw.eq(bo.getUserId() != null, SysOaCost::getUserId, bo.getUserId());
        lqw.eq(bo.getFinanceId() != null, SysOaCost::getFinanceId, bo.getFinanceId());
        lqw.eq(bo.getClaimId() != null, SysOaCost::getClaimId, bo.getClaimId());
        return lqw;
    }

    /**
     * 新增项目成本分析
     */
    @Override
    public Boolean insertByBo(SysOaCostBo bo) {
        SysOaCost add = BeanUtil.toBean(bo, SysOaCost.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCostId(add.getCostId());
        }
        return flag;
    }

    /**
     * 修改项目成本分析
     */
    @Override
    public Boolean updateByBo(SysOaCostBo bo) {
        SysOaCost update = BeanUtil.toBean(bo, SysOaCost.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    }



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



    @Override
    public TableDataInfo<SysOaCostAllVo> calcProjectCostList(SysOaCostBo bo, PageQuery pageQuery) {
        // 查询所有的项目拿到项目之后分别计算他们的各种成本联查四表
        QueryWrapper<SysOaProjectBo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(bo.getProjectName()!=null,"sop.project_name", bo.getProjectName());
        queryWrapper.orderByDesc("sop.create_time");
        Page<SysOaCostAllVo> sysOaCostAllVos =  projectMapper.selectListToCost(pageQuery.build(),queryWrapper);
        // 查询user laborCost 查attendance 拿签到
        return TableDataInfo.build(sysOaCostAllVos);
    }

    /**
     * 项目成本查询详情改进
     */
    @Override
    public SysOaCostRow calcProjectDetail(Long projectId) {
        Map<String, Object> result  = projectMapper.selectProjectDetails(projectId);
        String jsonData = (String) result.get("data");
        return JSON.parseObject(jsonData, SysOaCostRow.class);
    }
}
