package com.happy.service.impl;

import com.happy.Model.BookingComment;
import com.happy.Model.FileInfo;
import com.happy.Model.Workflow;
import com.happy.dao.ArticleTweetDao;
import com.happy.dao.FileInfoDao;
import com.happy.dao.HouseNumberDao;
import com.happy.dao.WorkflowDao;
import com.happy.dto.IPage;
import com.happy.service.WorkflowService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("WorkflowService")
public class WorkflowImplService implements WorkflowService {
    @Resource
    public WorkflowDao workflowDao;

    @Resource
    public FileInfoDao fileDao;

    @Resource
    public ArticleTweetDao articleTweetDao;

    @Override
    public int insert(Workflow workflow) {
        return workflowDao.insert(workflow);
    }

    @Override
    public Workflow queryById(String id) {
        return workflowDao.queryById(id);
    }

    @Override
    public int update(Workflow workflow) {
        return workflowDao.update(workflow);
    }

    @Override
    public IPage<Workflow> queryList(String sqlx, int page, int rows) {
        IPage<Workflow> iPage = new IPage();
        List<Workflow> workflows = workflowDao.queryList(sqlx,page,rows);
        if (workflows != null){
            workflows.forEach(workflow -> {
                List<FileInfo> fileInfoList;
                if (workflow.getStatus() == 2 && workflow.getType() == 1 || workflow.getType() == 2){
                    fileInfoList = fileDao.queryList("and link_id = '"+workflow.getLinkId()+"'");
                }
                //region
                //2023-10-11 A-jax添加推文流程数据获取逻辑
                else if(workflow.getType() == 3){
                    fileInfoList = articleTweetDao.queryList("and link_id = '"+workflow.getLinkId()+"'");
                }
                //endregion
                else {
                    fileInfoList = fileDao.queryList("and link_id = '"+workflow.getId()+"'");
                }
                workflow.setFileInfoList(fileInfoList);
            });
        }
        int total = workflowDao.queryTotal(sqlx);
        iPage.setPageList(workflows);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }
}
