package com.jeeplus.test.cw.reimbursementApproval.approvalType.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeeplus.core.domain.BaseEntity;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.cw.reimbursementApproval.approvalType.domain.CwReimbursementTypeInfo;
import com.jeeplus.test.cw.reimbursementApproval.approvalType.mapper.CwReimbursementTypeMapper;
import org.springframework.stereotype.Service;

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

/**
 * @author: 王强
 * @create: 2022-11-25 09:13
 **/
@Service
public class CwReimbursementTypeService {

    @Resource
    private CwReimbursementTypeMapper mapper;

    public List<CwReimbursementTypeInfo> list(CwReimbursementTypeInfo info) {
        LambdaQueryWrapper<CwReimbursementTypeInfo> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(info.getName())) {
            wrapper.like(CwReimbursementTypeInfo::getName, info.getName());
        }
        if (info.getSort() != null) {
            wrapper.like(CwReimbursementTypeInfo::getSort, info.getSort());
        }
//        wrapper.eq(CwReimbursementTypeInfo::getBusinessType, "1");

        wrapper.eq(BaseEntity::getDelFlag, 0);
        wrapper.orderByAsc(CwReimbursementTypeInfo::getSort);
        return mapper.selectList(wrapper);
    }

    public List<CwReimbursementTypeInfo> bxList(CwReimbursementTypeInfo info) {
        LambdaQueryWrapper<CwReimbursementTypeInfo> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(info.getName())) {
            wrapper.like(CwReimbursementTypeInfo::getName, info.getName());
        }
        if (ObjectUtil.isNotEmpty(info)) {
            if (StringUtils.isNotBlank(info.getLike())){
                wrapper.likeRight(CwReimbursementTypeInfo::getSort,info.getLike());
            }
        }
        if (info.getSort() != null) {
            wrapper.like(CwReimbursementTypeInfo::getSort, info.getSort());
        }
        wrapper.eq(CwReimbursementTypeInfo::getBusinessType, "1");

        wrapper.eq(BaseEntity::getDelFlag, 0);
        wrapper.orderByAsc(CwReimbursementTypeInfo::getSort);
        return mapper.selectList(wrapper);
    }

    public List<CwReimbursementTypeInfo> cgList(CwReimbursementTypeInfo info) {
        LambdaQueryWrapper<CwReimbursementTypeInfo> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(info.getName())) {
            wrapper.like(CwReimbursementTypeInfo::getName, info.getName());
        }
        if (info.getSort() != null) {
            wrapper.like(CwReimbursementTypeInfo::getSort, info.getSort());
        }
        wrapper.eq(CwReimbursementTypeInfo::getBusinessType, "2");

        wrapper.eq(BaseEntity::getDelFlag, 0);
        wrapper.orderByAsc(CwReimbursementTypeInfo::getSort);
        return mapper.selectList(wrapper);
    }

    public String save(CwReimbursementTypeInfo info) {
        // parentId未传值，则表示一级菜单
        if(StringUtils.isEmpty(info.getParentId())) {
            info.setParentId("0");
        }
        // 保存数据
        if (StringUtils.isNotEmpty(info.getId())) {
            return update(info);
        }
        return add(info);
    }

    /**
     * 新增
     * @param info
     * @return
     */
    public String add(CwReimbursementTypeInfo info) {
        // 判断名称是否已存在
        Integer isExist = mapper.checkNameIsExist(info.getName(), info.getParentId());
        if (isExist > 0) {
            return "false";
        }
        // 获取当前登录人信息
        UserDTO userDto = UserUtils.getCurrentUserDTO();
        // 生成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(CwReimbursementTypeInfo info) {
        // 获取当前登录人信息
        UserDTO userDto = UserUtils.getCurrentUserDTO();
        info.setUpdateBy(userDto.getId());
        info.setUpdateDate(new Date());
        //父级
        if (info.getParentId().equals("0")){
            //修改所有子集的业务类型
            List<String> childId = mapper.getChildId(info.getId());
            childId.forEach(chidId->{
                //修改子级的子级
                mapper.updateChildBusType(chidId,info.getBusinessType());
            });
            //修改子级
            mapper.updateChildBusType(info.getId(),info.getBusinessType());
        }

        mapper.updateById(info);
        return "操作完成";
    }

    /**
     * 生成序号/层级
     * @param info
     * @return
     */
    public CwReimbursementTypeInfo getNo(CwReimbursementTypeInfo info) {
        CwReimbursementTypeInfo parentInfo = null;
        if (!"0".equals(info.getParentId())) {
            //根据parentId查询父节点信息
            LambdaQueryWrapper<CwReimbursementTypeInfo> 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;
    }
}
