package com.ytpm.middle.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ytpm.general.RepMessage;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.middle.dao.MiddleDeptMapper;
import com.ytpm.middle.model.YtMiddleDept;
import com.ytpm.middle.param.MiddleDeptListParam;
import com.ytpm.middle.param.MiddleDeptParam;
import com.ytpm.middle.service.DeptService;
import com.ytpm.middle.view.DropDownVO;
import com.ytpm.middle.view.MiddleDeptVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    private MiddleDeptMapper deptMapper;

    /**
     * 部门信息列表
     */
    @Override
    public ResultTable<MiddleDeptVO> queryList(MiddleDeptListParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        return ResultTable.resultTableOk(new PageInfo<>(deptMapper.queryList(param)));
    }

    /**
     * 新增部门信息
     */
    @Override
    public Result<String> addOne(MiddleDeptParam param) {
        YtMiddleDept old = deptMapper.selectByName(param.getDeptName());
        if(Objects.nonNull(old)){
            return Result.resultErr(RepMessage.OBJECT_ALREADY_EXIST);
        }
        YtMiddleDept dept = new YtMiddleDept();
        BeanUtils.copyProperties(param, dept);
        dept.setDefaultParam(param.getCreateUserId());
        deptMapper.insertOne(dept);
        return Result.resultOk(RepMessage.SAVE_SUCCESS);
    }

    /**
     * 查询部门树形菜单
     */
    @Override
    public Result<DropDownVO> getDeptTree() {
        List<DropDownVO> all = deptMapper.getAllDeptList();
        DropDownVO parent = all.stream().filter(s -> Objects.isNull(s.getParentId())).collect(Collectors.toList()).get(0);
        List<DropDownVO> next = all.stream().filter(s -> Objects.nonNull(s.getParentId())).collect(Collectors.toList());
        recursionChildOption(parent,next);
        return Result.resultObjOk(parent);
    }

    /**
     * 修改部门信息
     */
    @Override
    public Result<String> updateOne(MiddleDeptParam param) {
        YtMiddleDept old = deptMapper.selectPrimary(param.getDeptId());
        if(Objects.isNull(old)){
            return Result.resultErr(RepMessage.OBJECT_NOT_EXIST);
        }
        YtMiddleDept dept = new YtMiddleDept();
        BeanUtils.copyProperties(param, dept);
        deptMapper.updateById(dept);
        return Result.resultOk(RepMessage.MODIFY_SUCCESS);
    }

    /**
     * 删除部门信息
     *
     */
    @Override
    public Result<String> deleteOne(Integer deptId,String operatorId) {
        YtMiddleDept old = deptMapper.selectPrimary(deptId);
        if (Objects.isNull(old)) {
            return Result.resultErr(RepMessage.OBJECT_NOT_EXIST);
        }
        deptMapper.updateChildren(deptId,operatorId);
        YtMiddleDept dept = new YtMiddleDept();
        dept.setDeptId(deptId);
        dept.setAvailable(0);
        dept.setUpdateParam(operatorId);
        deptMapper.updateById(dept);
        return Result.resultOk(RepMessage.MODIFY_SUCCESS);
    }

    /**
     * 递归处理部门列表
     */
    private void recursionChildOption(DropDownVO parent, List<DropDownVO> next) {
        List<DropDownVO> collect = next.stream().filter(s -> Objects.equals(s.getParentId(), parent.getOptionVal())).collect(Collectors.toList());
        if(CollUtil.isEmpty(collect))return;
        parent.setChildren(collect);
        for (DropDownVO vo : next) {
            recursionChildOption(vo,next);
        }
    }
}
