package com.ruoyi.scientific.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.scientific.domain.ProjectHandbook;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.scientific.domain.bo.DepartmentBo;
import com.ruoyi.scientific.domain.vo.DepartmentVo;
import com.ruoyi.scientific.domain.Department;
import com.ruoyi.scientific.mapper.DepartmentMapper;
import com.ruoyi.scientific.service.IDepartmentService;

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

/**
 * departmentService业务层处理
 *
 * @author zqjia
 * @date 2024-08-13
 */
@RequiredArgsConstructor
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    private final DepartmentMapper baseMapper;

    /**
     * 查询department
     */
    @Override
    public DepartmentVo queryById(Long departmentId){
        return baseMapper.selectVoById(departmentId);
    }

    /**
     * 查询department列表
     */
    @Override
    public TableDataInfo<DepartmentVo> queryPageList(DepartmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Department> lqw = buildQueryWrapper(bo);
        Page<DepartmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询department列表
     */
    @Override
    public List<DepartmentVo> queryList(DepartmentBo bo) {
        LambdaQueryWrapper<Department> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Department> buildQueryWrapper(DepartmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Department> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDepartmentId() != null, Department::getDepartmentId, bo.getDepartmentId());
        lqw.like(StringUtils.isNotBlank(bo.getDepartmentName()), Department::getDepartmentName, bo.getDepartmentName());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentAddress()), Department::getDepartmentAddress, bo.getDepartmentAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentCategory()), Department::getDepartmentCategory, bo.getDepartmentCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentPerson()), Department::getDepartmentPerson, bo.getDepartmentPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentPhone()), Department::getDepartmentPhone, bo.getDepartmentPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentEmail()), Department::getDepartmentEmail, bo.getDepartmentEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentFile()), Department::getDepartmentFile, bo.getDepartmentFile());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentNote()), Department::getDepartmentNote, bo.getDepartmentNote());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentProcId()), Department::getDepartmentProcId, bo.getDepartmentProcId());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartmentStatus()), Department::getDepartmentStatus, bo.getDepartmentStatus());
        return lqw;
    }

    /**
     * 新增department
     */
    @Override
    public Boolean insertByBo(DepartmentBo bo) {
        Department add = BeanUtil.toBean(bo, Department.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDepartmentId(add.getDepartmentId());
        }
        return flag;
    }

    /**
     * 修改department
     */
    @Override
    public Boolean updateByBo(DepartmentBo bo) {
        Department update = BeanUtil.toBean(bo, Department.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    public Boolean updateDepartmentInfoById(Long departmentId, String status){
        // 获取项目的详细信息
        Department update  = baseMapper.selectById(departmentId);
        if (update == null) {
            return false;
        }
        // 更新状态信息
        update.setDepartmentStatus(status);
        // 保存信息
        validEntityBeforeSave(update);
        // 返回结果取决于是否更新成功
        return baseMapper.updateById(update) > 0;
    }
}
