package com.ruoyi.zhushi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zhushi.entity.NjBalanceManage;
import com.ruoyi.zhushi.entity.NjBalanceManageDetail;
import com.ruoyi.zhushi.entity.NjBalanceManageDetailDTO;
import com.ruoyi.zhushi.mapper.NjBalanceManageDetailMapper;
import com.ruoyi.zhushi.mapper.NjBalanceManageMapper;
import com.ruoyi.zhushi.service.NjBalanceManageDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
@Transactional()
public class NjBalanceManageDetailServiceImpl implements NjBalanceManageDetailService {

    @Autowired
    private NjBalanceManageMapper njBalanceManageMapper;
    @Autowired
    private NjBalanceManageDetailMapper njBalanceManageDetailMapper;
    @Autowired
    private ISysUserService iSysUserService;
    //查询年假申请信息
    @Override
    public TableDataInfo<NjBalanceManageDetailDTO> queryPageList(NjBalanceManageDetailDTO njBalanceManageDetailDTO, PageQuery pageQuery) {
        LambdaQueryWrapper<NjBalanceManageDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(njBalanceManageDetailDTO.getUserName()!= null, NjBalanceManageDetail::getUserName, njBalanceManageDetailDTO.getUserName());
        queryWrapper.like(njBalanceManageDetailDTO.getNickName()!= null, NjBalanceManageDetail::getNickName, njBalanceManageDetailDTO.getNickName());
        IPage<NjBalanceManageDetailDTO> njBalanceManageDTOS = njBalanceManageDetailMapper.selectVoPage(pageQuery.build(), queryWrapper, NjBalanceManageDetailDTO.class);
        return TableDataInfo.build(njBalanceManageDTOS);
    }

    //单条插入
    @Override
    public boolean insert(NjBalanceManageDetailDTO njBalanceManageDetailDTO) {
        NjBalanceManageDetail njBalanceManageDetail = new NjBalanceManageDetail();
        BeanUtils.copyProperties(njBalanceManageDetailDTO,njBalanceManageDetail);
        SysUser sysUser = iSysUserService.selectUserById(njBalanceManageDetailDTO.getUserId());
        njBalanceManageDetail.setUserName(sysUser.getUserName());
        njBalanceManageDetail.setNickName(sysUser.getNickName());
        njBalanceManageDetail.setDeptName(sysUser.getDept().getDeptName());
        njBalanceManageDetail.setIsEnable(true);
        //年假申请明细 插入
        int i = njBalanceManageDetailMapper.insert(njBalanceManageDetail);
        int j = 0;
        //年假余额表 修改已使用和未使用年假
        if (i==1) {
            //查询这个用户
            BigDecimal used_days = new BigDecimal(0);
            QueryWrapper detailQuery =new QueryWrapper();
            detailQuery.select("sum(used_day) used_days");
            detailQuery.eq("user_id",njBalanceManageDetailDTO.getUserId());
            List<Map> detailList= njBalanceManageDetailMapper.selectMaps(detailQuery);
            if (detailList!=null&&detailList.size()>0) {
                Map map =detailList.get(0);
                used_days =  (BigDecimal )map.get("used_days");
            }
            UpdateWrapper<NjBalanceManage> updateWrapper = new UpdateWrapper();
            updateWrapper.set("used_day",used_days);
            updateWrapper.setSql("unused_day = annual_leave - " + used_days);
            updateWrapper.eq("user_id",njBalanceManageDetailDTO.getUserId());
            j = njBalanceManageMapper.update(null,updateWrapper);
        }
        if (i==1&&j==1) {
            return true;
        } else {
            throw new RuntimeException("插入失败！请联系软件部门");
        }

    }

}
