package com.ho.flow.service.process.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import com.ho.flow.entity.ProcessTemplate;
import com.ho.flow.mapper.ProcessManagementMapper;
import com.ho.flow.service.process.service.ProcessManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author yule
 * @Date 2023/8/18 9:22
 * @desc ${}
 */
@Service
@Slf4j
public class ProcessManagementServiceImpl implements ProcessManagementService {

    @Autowired
    ProcessManagementMapper processManagement;

    @Autowired
    Snowflake snowflake;

    @Override
    public void add(ProcessTemplate processTemplate) {
        long serialNum = snowflake.nextId();
        processTemplate.setSerialNum(serialNum);
        int i =  processManagement.insert(processTemplate);
    }

    @Override
    public void delete(ProcessTemplate processTemplate) {
        Integer id = processTemplate.getId();
        processManagement.delete(id);
    }

    @Override
    public void edit(ProcessTemplate processTemplate) {
        processManagement.update(processTemplate);
    }

    @Override
    public ProcessTemplate getInfo(Integer id) {
        ProcessTemplate processTemplate = processManagement.selectById(id);
        return processTemplate;
    }

    @Override
    public PageResult<ProcessTemplate> selectPage(ProcessTemplate processTemplate) {
        PageHelper.startPage(processTemplate.getPageNum(),processTemplate.getPageSize());
        List<ProcessTemplate> result = processManagement.selectList(processTemplate);
        PageResult pageResult = new PageResult();
        if (!result.isEmpty() && result != null) {
            PageResult<ProcessTemplate> page = PageUtils.getPageResult(new PageInfo<>(result));
            PageInfo<ProcessTemplate> pageInfo = new PageInfo<>(result);
            pageInfo.setTotal(page.getTotalRows());
            pageInfo.setPages(page.getTotalPages());
            pageResult = PageUtils.getPageResult(pageInfo);
        } else {
            pageResult.setList(new ArrayList());
        }
        return pageResult;
    }

    @Override
    public String getBpmn(Integer id) {
        return processManagement.selectBpmnById(id);
    }
}
