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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.form.entity.FormTemplate;
import com.xjrsoft.module.form.mapper.FormTemplateMapper;
import com.xjrsoft.module.print.dto.PrintConfigInfoDto;
import com.xjrsoft.module.print.dto.PrintSetConfigDto;
import com.xjrsoft.module.print.entity.PrintConfig;
import com.xjrsoft.module.print.entity.PrintSchema;
import com.xjrsoft.module.print.mapper.PrintConfigMapper;
import com.xjrsoft.module.print.mapper.PrintSchemaMapper;
import com.xjrsoft.module.print.service.IPrintConfigService;
import com.xjrsoft.module.print.vo.PrintConfigInfoVo;
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-23
 */
@Service
@AllArgsConstructor
public class PrintConfigServiceImpl extends ServiceImpl<PrintConfigMapper, PrintConfig> implements IPrintConfigService {

    private final MenuMapper menuMapper;

    private final PrintSchemaMapper printSchemaMapper;

    private final FormTemplateMapper formTemplateMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setConfig(PrintSetConfigDto dto) {

        remove(Wrappers.lambdaQuery(PrintConfig.class).eq(PrintConfig::getMenuId, dto.getMenuId()).eq(PrintConfig::getButtonCode, dto.getButtonCode()));

        PrintConfig printConfig = BeanUtil.toBean(dto, PrintConfig.class);
        save(printConfig);
        return Boolean.TRUE;
    }

    @Override
    public PrintConfigInfoVo getInfo(PrintConfigInfoDto dto) {
        PrintConfigInfoVo printConfigInfoVo = new PrintConfigInfoVo();
        PrintConfig config = getOne(Wrappers.lambdaQuery(PrintConfig.class).eq(PrintConfig::getMenuId, dto.getMenuId()).eq(PrintConfig::getButtonCode, dto.getButtonCode()));
        if (config != null) {
            Long schemaId = config.getSchemaId();
            PrintSchema schema = printSchemaMapper.selectOne(Wrappers.lambdaQuery(PrintSchema.class).eq(PrintSchema::getId,schemaId).select(PrintSchema::getEnabledMark));

            if (ObjectUtil.isNotNull(config)) {
                printConfigInfoVo = BeanUtil.toBean(config, PrintConfigInfoVo.class);
            }

            if (ObjectUtil.isNotNull(schema)) {
                printConfigInfoVo.setEnabledMark(schema.getEnabledMark());
            }
        }
        Menu menu = menuMapper.selectById(dto.getMenuId());

        if (ObjectUtil.isNotNull(menu.getFormId())) {
            FormTemplate formTemplate = formTemplateMapper.selectById(menu.getFormId());
            if (ObjectUtil.isNotNull(formTemplate)) {
                printConfigInfoVo.setFormJson(formTemplate.getFormJson());
                printConfigInfoVo.setFormType(formTemplate.getFormType());
            }
        }

        return printConfigInfoVo;
    }
}
