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



import cn.hutool.core.lang.Snowflake;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import com.ho.flow.entity.ProcessTemplate;
import com.ho.flow.feignclient.UserFeignClient;
import com.ho.flow.mapper.ProcessTemplateMapper;
import com.ho.flow.service.process.service.ProcessTemplateService;
import com.ho.user.api.entity.SysSubDict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author yule
* @description 针对表【process_template】的数据库操作Service实现
* @createDate 2023-08-17 10:45:32
*/
@Service
@Slf4j
public class ProcessTemplateServiceImpl implements ProcessTemplateService {

    @Autowired
    ProcessTemplateMapper processTemplateMapper;

    @Autowired
    Snowflake snowflake;

    @Autowired
    UserFeignClient userFeignClient;

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

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

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

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

    @Override
    public PageResult<ProcessTemplate> selectPage(ProcessTemplate processTemplate) {
        PageHelper.startPage(processTemplate.getPageNum(),processTemplate.getPageSize());
        List<ProcessTemplate> result = processTemplateMapper.selectList(processTemplate);
        //模版类型
        List<Integer> typeIds = result.stream().map(ProcessTemplate::getType).collect(Collectors.toList());
        //查询模版类型
        DataResult<List<SysSubDict>> dataResult = userFeignClient.getDictByIds(typeIds);
        if (dataResult != null && !dataResult.getData().isEmpty()){
            Map<Integer, List<SysSubDict>> map = dataResult.getData().stream().collect(Collectors.groupingBy(SysSubDict::getId));
            for (ProcessTemplate template : result) {
                if (!map.get(template.getType()).isEmpty()) {
                    List<SysSubDict> sysSubDicts = map.get(template.getType());
                    template.setTypeName(sysSubDicts.get(0).getLabel());
                }
            }
        }
        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 List<ProcessTemplate> selectList(ProcessTemplate processTemplate) {
        return processTemplateMapper.selectList(processTemplate);
    }

    @Override
    public void getBpmn(HttpServletResponse response,Integer id) throws IOException {
        String bpmn = processTemplateMapper.selectBpmnById(id);
        if (!StringUtils.isEmpty(bpmn)) {
            byte[] bytes = bpmn.getBytes(StandardCharsets.UTF_8);
            response.setContentType("text/xml");
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(bytes);
            outputStream.close();
        }
    }


}




