package com.hzya.frame.sys.flow.service.impl;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sys.flow.dao.ISysFlowStepAccountDao;
import com.hzya.frame.sys.flow.entity.SysFlowStepAccountEntity;
import com.hzya.frame.sys.flow.entity.SysFlowStepEntity;
import com.hzya.frame.sys.flow.dao.ISysFlowStepDao;
import com.hzya.frame.sys.flow.service.ISysFlowStepAccountService;
import com.hzya.frame.sys.flow.service.ISysFlowStepService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.hzya.frame.basedao.service.impl.BaseService;

import java.util.List;

/**
 * 流程步骤信息(SysFlowStep)表服务实现类
 *
 * @author xiang2lin
 * @since 2025-04-29 10:16:27
 */
@Service(value = "sysFlowStepService")
public class SysFlowStepServiceImpl extends BaseService<SysFlowStepEntity, String> implements ISysFlowStepService {

    private ISysFlowStepDao sysFlowStepDao;
    @Autowired
    private ISysFlowStepAccountService sysFlowStepAccountService;

    @Autowired
    public void setSysFlowStepDao(ISysFlowStepDao dao) {
        this.sysFlowStepDao = dao;
        this.dao = dao;
    }

    /**
     * 保存流程步骤
     *
     * @param object
     * @return
     */
    @Override
    public JsonResultEntity saveFlowStep(JSONObject object) {
        SysFlowStepEntity sysFlowStep = getData("jsonStr", object, SysFlowStepEntity.class);
        try {
            checkParams(sysFlowStep, "save");
        } catch (Exception e) {
            return BaseResult.getFailureMessageEntity(e.getMessage());
        }
        sysFlowStepDao.save(sysFlowStep);
        return BaseResult.getSuccessMessageEntity("保存成功", sysFlowStep);
    }


    /**
     * 更新流程步骤
     *
     * @param object
     * @return
     */
    @Override
    public JsonResultEntity updateFlowStep(JSONObject object) {
        SysFlowStepEntity sysFlowStep = getData("jsonStr", object, SysFlowStepEntity.class);
        try {
            checkParams(sysFlowStep, "update");
        } catch (Exception e) {
            return BaseResult.getFailureMessageEntity(e.getMessage());
        }
        sysFlowStepDao.update(sysFlowStep);
        //保存账户信息
        if (null != sysFlowStep.getAccount()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("jsonStr", JSONObject.toJSONString(sysFlowStep.getAccount()));
            sysFlowStepAccountService.saveAccount(jsonObject);
        }
        return BaseResult.getSuccessMessageEntity("保存成功", sysFlowStep);
    }

    /**
     * 删除流程步骤
     *
     * @param object
     * @return
     */
    @Override
    public JsonResultEntity deleteFlowStep(JSONObject object) {
        SysFlowStepEntity sysFlowStep = getData("jsonStr", object, SysFlowStepEntity.class);
        try {
            checkParams(sysFlowStep, "delete");
            String id = sysFlowStep.getId();
            SysFlowStepEntity step = sysFlowStepDao.get(id);
            //删除流程步骤账户表
            SysFlowStepAccountEntity stepAccount = new SysFlowStepAccountEntity();
            stepAccount.setFlowId(step.getFlowId());
            stepAccount.setStepId(id);
            sysFlowStepAccountService.logicRemoveMultiCondition(stepAccount);
            //删除流程步骤
            sysFlowStepDao.logicRemove(step);
        } catch (Exception e) {
            return BaseResult.getFailureMessageEntity(e.getMessage());
        }
        return BaseResult.getSuccessMessageEntity("删除成功");
    }

    /**
     * 查询列表
     *
     * @param object
     * @return
     */
    @Override
    public JsonResultEntity queryList(JSONObject object) {
        SysFlowStepEntity sysFlowStep = getData("jsonStr", object, SysFlowStepEntity.class);
        try {
            checkParams(sysFlowStep, "queryList");
        } catch (Exception e) {
            return BaseResult.getFailureMessageEntity(e.getMessage());
        }
        List<SysFlowStepEntity> list = sysFlowStepDao.query(sysFlowStep);
        return BaseResult.getSuccessMessageEntity("查询数据成功", list);
    }

    /**
     * 步骤详情
     *
     * @param object
     * @return
     */
    @Override
    public JsonResultEntity getFlowStep(JSONObject object) {
        SysFlowStepEntity sysFlowStep = getData("jsonStr", object, SysFlowStepEntity.class);
        try {
            checkParams(sysFlowStep, "get");
        } catch (Exception e) {
            return BaseResult.getFailureMessageEntity(e.getMessage());
        }
        SysFlowStepEntity entity = sysFlowStepDao.get(sysFlowStep.getId());
        return BaseResult.getSuccessMessageEntity("查询详情成功", entity);
    }

    private void checkParams(SysFlowStepEntity entity, String type) {
        Assert.notNull(entity, "参数不能为空");
        if ("save".equals(type)) {
            Assert.notEmpty(entity.getFlowId(), "flowId不能为空");
            Assert.notNull(entity.getStep(), "步骤号不能为空");
            Assert.notEmpty(entity.getStepType(), "stepType不能为空");
        } else if ("update".equals(type)) {
            Assert.notEmpty(entity.getAppId(), "appId不能为空");
            Assert.notEmpty(entity.getNifiApiId(), "nifiApiId不能为空");
        } else if ("delete".equals(type)) {
            Assert.notEmpty(entity.getId(), "id不能为空");
        }else if ("queryList".equals(type)){
            Assert.notEmpty(entity.getFlowId(),"flowId不能为空");
        }else if ("get".equals(type)){
            Assert.notEmpty(entity.getId(),"id不能为空");
        }
    }
}
