package com.java110.user.cmd.property;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
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.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

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

@Java110Cmd(serviceCode = "appVersion.listAppVersions")
public class ListAppVersionsCmd extends Cmd {

    @Autowired
    private IAppVersionV1ServiceDao appVersionV1ServiceDao;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) {
        super.validatePageInfo(reqJson);
    }

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

        int count = appVersionV1ServiceDao.queryAppVersionsCount(params);
        List<Map> list = null;
        if (count > 0) {
            int page = reqJson.getIntValue("page");
            int row = reqJson.getIntValue("row");
            params.put("page", (page - 1) * row);
            params.put("row", row);
            list = appVersionV1ServiceDao.queryAppVersions(params);
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, list);
        context.setResponseEntity(new ResponseEntity<>(resultVo.toString(), HttpStatus.OK));
    }
}
