package com.yunyan.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunyan.common.core.domain.entity.SysRole;
import com.yunyan.system.domain.SysRoleMenu;
import com.yunyan.system.domain.dto.DeptManageAddDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunyan.system.mapper.DeptManageMapper;
import com.yunyan.system.domain.DeptManage;
import com.yunyan.system.service.IDeptManageService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import com.yunyan.common.core.domain.AjaxResultVo;
import org.springframework.beans.BeanUtils;
import com.yunyan.common.core.domain.model.LoginUser;
import com.yunyan.common.utils.SecurityUtils;
import com.yunyan.common.enums.DataContentType;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import com.yunyan.common.annotation.DataScope;

/**
 * 管理部门与被管理部门的Service业务层处理
 *
 * @author yunyan
 * @date 2024-10-12
 */
@Slf4j
@Service
public class DeptManageServiceImpl extends ServiceImpl<DeptManageMapper, DeptManage> implements IDeptManageService
{
    @Autowired
    private DeptManageMapper deptManageMapper;

    /**
     * 查询管理部门与被管理部门的列表
     *
     * @param deptManage 管理部门与被管理部门的
     * @return 管理部门与被管理部门的
     */    @Override
    public List<DeptManage> selectDeptManageList(DeptManage deptManage)
    {
        return deptManageMapper.selectDeptManageList(deptManage);
    }

    /**
    * 新增
    * @param
    * @return
    */
    @Override
    public AjaxResultVo add(DeptManage dto) {
        DeptManage entity = dto;

        this.save(entity);
        return AjaxResultVo.success();
    }

    /**
     * 编辑
     * @param
     * @return
     */
    @Override
    public AjaxResultVo edit(DeptManage dto) {

        /*DeptManage entity = this.getById(dto.getId());
        if(null == entity) {
            return AjaxResultVo.error("非法操作");
        }

        entity = new DeptManage();
        BeanUtils.copyProperties(dto, entity);

        this.updateById(entity);*/
        return AjaxResultVo.success();
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public AjaxResultVo info(Long id) {
        DeptManage entity = this.getById(id);
        if(null == entity) {
            return AjaxResultVo.error("数据查询异常~");
        }

        return AjaxResultVo.success(entity);
    }

    @Override
    public AjaxResultVo addList(DeptManageAddDto dto) {

        //删除 之前旧的管理员
        deptManageMapper.delete(new LambdaQueryWrapper<DeptManage>().eq(DeptManage::getDeptIdManage, dto.getDeptIdManage()));
        int result = insertDeptManageBatch(dto);

        return result > 0 ? AjaxResultVo.success() : AjaxResultVo.error("'更新部门管理员失败'");
    }

    public int insertDeptManageBatch(DeptManageAddDto dto)
    {
        if(dto.getDeptIdList().isEmpty()) {
            return 0;
        }
        int rows = 0;
        // 新增管理部门与被管理部门
        List<DeptManage> list = new ArrayList<>();
        for (Long deptId : dto.getDeptIdList())
        {
            DeptManage entity = new DeptManage();
            entity.setDeptIdManage(dto.getDeptIdManage());
            entity.setDeptId(deptId);
            list.add(entity);
        }
        if (list.size() > 0)
        {
            boolean result = this.saveBatch(list);
            return result ? 1 : 0;
        }

        return rows;
    }
}
