package com.java110.user.cmd.property;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.user.dao.property.IAppVersionV1ServiceDao;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

@Java110Cmd(serviceCode = "appVersion.deleteAppVersion")
public class DeleteAppVersionCmd extends Cmd {

    @Autowired
    private IAppVersionV1ServiceDao appVersionV1ServiceDao;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "avId", "未包含版本ID");
    }

    @Override
    @Java110Transactional
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Map<String, Object> params = new HashMap<>();
        params.put("avId", reqJson.getString("avId"));

        int flag = appVersionV1ServiceDao.deleteAppVersion(params);
        if (flag < 1) {
            throw new CmdException("删除版本信息失败");
        }
        context.setResponseEntity(ResultVo.success());
    }
}
