package com.tykj.workflowcore.workflow_editer.service.impl;


import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
import com.tykj.workflowcore.model_layer.entity.TableInfo;
import com.tykj.workflowcore.model_layer.entity.vo.SearchTableInfoVo;
import com.tykj.workflowcore.model_layer.entity.vo.TableAndColumnInfoVO;
import com.tykj.workflowcore.model_layer.service.ModelService;
import com.tykj.workflowcore.workflow_editer.entity.FormPage;
import com.tykj.workflowcore.workflow_editer.dao.FormPageMapper;
import com.tykj.workflowcore.workflow_editer.service.FormPageService;
import com.tykj.workflowcore.workflow_editer.entity.vo.FormPageVo;
import com.tykj.workflowcore.workflow_editer.entity.vo.PageFormPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:    PageEntityServiceImpl
 * Package:    com.tykj.service.impl
 * Description:
 * Datetime:    2021/3/3   13:16
 *
 * @Author: zsp
 */
@Service
public class FormPageServiceImpl implements FormPageService {

    @Autowired
    private FormPageMapper formPageMapper;
    @Autowired
    private ModelService modelService;
    @Override
    public Integer savePage(@RequestBody FormPageVo inFormPageVo) {
        inFormPageVo.setCreateTime(new Date());
        FormPage formPage = inFormPageVo.toEntity();
        FormPage formPage1 = formPageMapper.save(formPage);
        Integer id = formPage1.getId();
        return id;
    }

    @Override
    public void update(FormPage formPage) {
        formPageMapper.save(formPage);
    }

    @Override
    public FormPageVo getPage(Integer id) {

        FormPage formPage = formPageMapper.findById(id).get();

        return   formPage.toEntityVo();
    }

    @Override
    public void deletePage(Integer id) {
        FormPage formPage = formPageMapper.findById(id).get();
        formPageMapper.delete(formPage);
    }

    @Override
    public Page<FormPage> getAllPages(PageFormPageVo pageFormPageVo) {

        PredicateBuilder<FormPage> and = Specifications.and();
        and.eq(pageFormPageVo.getPageName()!=null,"pageName",pageFormPageVo.getPageName());
        and.eq(pageFormPageVo.getPageDesc()!=null,"pageDesc",pageFormPageVo.getPageDesc());
        and.eq(pageFormPageVo.getCreateTime()!=null,"createTime",pageFormPageVo.getCreateTime());
        Page<FormPage> formPagePage = formPageMapper.findAll(and.build(), pageFormPageVo.getPageable());

        return formPagePage;
    }

    @Override
    public List<TableInfo> findByPageIds(List<Integer> pageIds) {

        PredicateBuilder<FormPage> builder = Specifications.and();
        builder.in("id", pageIds.toArray());
        List<FormPage> tableInfoList = formPageMapper.findAll(builder.build());
        StringBuffer entityIds = new StringBuffer();
        for (int i = 0; i < tableInfoList.size(); i++) {
            String entityId = tableInfoList.get(i).getEntityId();
            entityIds.append(entityId);
            entityIds.append(",");
            if ( i == tableInfoList.size()-1 ){
                entityIds.deleteCharAt(entityIds.length()-1);
            }
        }
        String[] split = entityIds.toString().split(",");
        Integer[] ids =Arrays.stream(split).mapToInt(Integer::parseInt).boxed().toArray(Integer[]::new);
        SearchTableInfoVo searchTableInfoVo = new SearchTableInfoVo();
        searchTableInfoVo.setIds(ids);
        return  modelService.listAllEntities(searchTableInfoVo);
    }

}
