package com.jeeplus.finance.projectReportAuditSheet.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.core.service.TreeService;
import com.jeeplus.finance.projectReport.domain.CwProofreadDetail;
import com.jeeplus.finance.projectReportAuditSheet.domain.CwProofreadType;
import com.jeeplus.finance.projectReportAuditSheet.mapper.CwProofreadTypeMapper;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
//import com.jeeplus.sys.utils.UserUtils;
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;

/**
 * @author: 王强
 * @create: 2022-12-08 09:19
 **/
@Service
public class CwProofreadTypeService  extends TreeService<CwProofreadTypeMapper, CwProofreadType> {

    @Resource
    private CwProofreadTypeMapper mapper;

    public List<CwProofreadDetail> prooList(String type) {
        return mapper.prooList(type);
    }

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

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

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

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

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