package com.xjrsoft.module.print.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.MenuType;
import com.xjrsoft.common.enums.YesOrNoEnum;
import com.xjrsoft.module.print.dto.AddPrintSchemaDto;
import com.xjrsoft.module.print.dto.UpdatePrintSchemaDto;
import com.xjrsoft.module.print.entity.PrintSchema;
import com.xjrsoft.module.print.mapper.PrintSchemaMapper;
import com.xjrsoft.module.print.service.IPrintSchemaService;
import com.xjrsoft.module.system.entity.Menu;
import com.xjrsoft.module.system.mapper.MenuMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 打印模板 服务实现类
 * </p>
 *
 * @author tzx
 * @since 2023-05-19
 */
@Service
@AllArgsConstructor
public class PrintSchemaServiceImpl extends MPJBaseServiceImpl<PrintSchemaMapper, PrintSchema> implements IPrintSchemaService {

    public final String componentPath = "/generator/print/menu";

    public final String urlPath = "/print/";

    public final MenuMapper menuMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AddPrintSchemaDto dto) {

        PrintSchema schema = BeanUtil.toBean(dto, PrintSchema.class);

        long schemaId = IdUtil.getSnowflakeNextId();
        schema.setId(schemaId);

        //如果是菜单
        if(schema.getIsMenu() == YesOrNoEnum.YES.getCode()){
            Menu menu = new Menu();
            long menuId = IdUtil.getSnowflakeNextId();
            schema.setMenuId(menuId);

            menu.setId(menuId);
            menu.setCode(dto.getCode());
            menu.setName(dto.getName());
            menu.setParentId(dto.getParentId());
            menu.setTitle(dto.getName());
            menu.setSystemId(1L);
            menu.setIcon(dto.getIcon());
            menu.setPath(urlPath + schemaId);
            menu.setComponent(componentPath);
            menu.setMenuType(MenuType.FUNCTION.getCode());
            menu.setDisplay(YesOrNoEnum.YES.getCode());
            menu.setAllowModify(YesOrNoEnum.YES.getCode());
            menu.setAllowDelete(YesOrNoEnum.YES.getCode());
            menu.setOutLink(YesOrNoEnum.NO.getCode());
            menu.setKeepAlive(YesOrNoEnum.NO.getCode());
            menu.setRemark(dto.getRemark());
            menu.setSortCode(dto.getSortCode());

            menuMapper.insert(menu);
        }


        save(schema);

        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSchema(UpdatePrintSchemaDto dto) {

        PrintSchema schema = BeanUtil.toBean(dto, PrintSchema.class);

        //是否菜单
        if (schema.getIsMenu() == YesOrNoEnum.YES.getCode()) {
            Menu menu = new Menu();
            menu.setCode(schema.getCode());
            menu.setName(schema.getName());
            menu.setParentId(dto.getParentId());
            menu.setTitle(schema.getName());
            menu.setSystemId(1L);
            menu.setIcon(dto.getIcon());
            menu.setId(dto.getMenuId());
            //如果是已经存在菜单  默认是修改原菜单
            if (ObjectUtil.isNotNull(dto.getMenuId()) && !dto.getMenuId().equals(0L)) {
                menuMapper.updateById(menu);
            }
            //如果取消了菜单 菜单数据不在 需要重新新增
            else {
                long menuId = IdUtil.getSnowflakeNextId();
                schema.setMenuId(menuId);
                menu.setId(menuId);
                menu.setPath(urlPath + dto.getId());
                menu.setComponent(componentPath);
                menu.setComponentType(YesOrNoEnum.YES.getCode());
                menu.setMenuType(MenuType.FUNCTION.getCode());
                menu.setDisplay(YesOrNoEnum.YES.getCode());
                menu.setAllowModify(YesOrNoEnum.YES.getCode());
                menu.setAllowDelete(YesOrNoEnum.YES.getCode());
                menu.setOutLink(YesOrNoEnum.NO.getCode());
                menu.setKeepAlive(YesOrNoEnum.NO.getCode());
                menu.setRemark(schema.getRemark());

                menuMapper.insert(menu);
            }

            schema.setMenuId(menu.getId());
        }
        //如果不是菜单 需要把原菜单删除
        else {
            if(ObjectUtil.isNotNull(dto.getMenuId()) && dto.getMenuId() != 0L){
                menuMapper.deleteById(dto.getMenuId());
                schema.setMenuId(0L);
            }
        }



        updateById(schema);

        return Boolean.TRUE;
    }
}
