package com.jeeplus.assess.reimbursement.reimbursementType.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.domain.BaseEntity;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
//import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.assess.reimbursement.reimbursementType.domain.ReimbursementTypeInfo;
import com.jeeplus.assess.reimbursement.reimbursementType.mapper.ReimbursementTypeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ReimbursementTypeService {

    @Resource
    private ReimbursementTypeMapper mapper;

    public List<ReimbursementTypeInfo> list(ReimbursementTypeInfo info) {
        LambdaQueryWrapper<ReimbursementTypeInfo> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(info.getName())) {
            wrapper.like(ReimbursementTypeInfo::getName, info.getName());
        }
        if (info.getSort() != null) {
            wrapper.like(ReimbursementTypeInfo::getSort, info.getSort());
        }
        wrapper.eq(BaseEntity::getDelFlag, 0);
        wrapper.orderByAsc(ReimbursementTypeInfo::getSort);
        return mapper.selectList(wrapper);
    }

    public String save(ReimbursementTypeInfo info) {
        // parentId未传值，则表示一级菜单
        if(StringUtils.isEmpty(info.getParentId())) {
            info.setParentId("0");
        }
        // 判断名称是否已存在
        Integer isExist = mapper.checkNameIsExist(info.getName(), info.getParentId());
        if (isExist > 0) {
            return "false";
        }
        // 保存数据
        if (StringUtils.isNotEmpty(info.getId())) {
            return update(info);
        }
        return add(info);
    }

    /**
     * 新增
     * @param info
     * @return
     */
    public String add(ReimbursementTypeInfo info) {
        // 获取当前登录人信息
        UserDTO userDto = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        // 生成id值
        String id = UUID.randomUUID().toString().replace("-", "");
        info.setId(id);
        info.setCreateBy(userDto.getId());
        info.setCreateDate(new Date());
        info.setUpdateBy(userDto.getId());
        info.setUpdateDate(new Date());
        info.setDelFlag(0);
        // 生成序号/层级
        getNo(info);
        mapper.insert(info);
        return "操作完成";
    }

    /**
     * 修改
     * @param info
     * @return
     */
    public String update(ReimbursementTypeInfo info) {
        // 获取当前登录人信息
        UserDTO userDto = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        info.setUpdateBy(userDto.getId());
        info.setUpdateDate(new Date());
        mapper.updateById(info);
        return "操作完成";
    }

    /**
     * 生成序号/层级
     * @param info
     * @return
     */
    public ReimbursementTypeInfo getNo(ReimbursementTypeInfo info) {
        ReimbursementTypeInfo parentInfo = null;
        if (!"0".equals(info.getParentId())) {
            //根据parentId查询父节点信息
            LambdaQueryWrapper<ReimbursementTypeInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseEntity::getDelFlag, 0);
            wrapper.eq(BaseEntity::getId, info.getParentId());
            parentInfo = mapper.selectOne(wrapper);
        }
        //查询序号
        Integer no = mapper.getNo(info.getParentId());
        // 该层级下有数据，正常返回序号值
        if (no != null) {
            info.setSort(no);
        } else {
            // 父节点存在，根据父节点序号生成；不存在，则表示无上级层级，从1开始
            if (parentInfo != null) {
                String s = parentInfo.getSort() + "01";
                info.setSort(Integer.parseInt(s));
            } else {
                info.setSort(1);
            }
        }
        // 生成层级
        //查询层级
        Integer level = mapper.getLevel(info.getParentId());
        if (level != null) {
            info.setLevel(level.toString());
        } else {
            // 父节点存在，根据父节点level生成，不存在，则表示无上级层级，从1开始
            if (parentInfo != null) {
                Integer i = Integer.parseInt(parentInfo.getLevel())+1;
                info.setLevel(i.toString());
            } else {
                info.setLevel("1");
            }
        }
        return info;
    }
}
