package com.java110.store.cmd.store;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.AbstractServiceCmdListener;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.store.StoreAttrDto;
import com.java110.dto.store.StoreDto;
import com.java110.intf.store.IStoreAttrV1InnerServiceSMO;
import com.java110.intf.store.IStoreV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.api.store.ApiStoreDataVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "store.listStores")
public class ListStoresCmd extends AbstractServiceCmdListener {

    @Autowired
    private IStoreV1InnerServiceSMO storeV1InnerServiceSMOImpl;

    @Autowired
    private IStoreAttrV1InnerServiceSMO storeAttrV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
        super.validatePageInfo(reqJson);
        StoreDto storeDto = new StoreDto();
        storeDto.setStoreId(reqJson.getString("storeId"));
        List<StoreDto> storeDtos = storeV1InnerServiceSMOImpl.queryStores(storeDto);

        Assert.listOnlyOne(storeDtos, "非法操作");

        //只有运营可以看所有 商户信息
        if (StoreDto.STORE_TYPE_ADMIN.equals(storeDtos.get(0).getStoreTypeCd())) {
            reqJson.remove("storeId");
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        StoreDto storeDto = BeanConvertUtil.covertBean(reqJson, StoreDto.class);
        storeDto.setUserId("");
        int storeCount = storeV1InnerServiceSMOImpl.queryStoresCount(storeDto);
        List<StoreDto> storeDtos = null;
        List<ApiStoreDataVo> stores = null;
        if (storeCount > 0) {
            storeDtos = storeV1InnerServiceSMOImpl.queryStores(storeDto);
            stores = BeanConvertUtil.covertBeanList(storeDtos, ApiStoreDataVo.class);
            refreshStoreAttr(stores);
        } else {
            stores = new ArrayList<>();
        }
    }


    private void refreshStoreAttr(List<ApiStoreDataVo> stores) {
        StoreAttrDto storeAttrDto = new StoreAttrDto();
        storeAttrDto.setStoreIds(getStoreIds(stores));
        List<StoreAttrDto> storeAttrDtos = storeAttrV1InnerServiceSMOImpl.queryStoreAttrs(storeAttrDto);
        for (ApiStoreDataVo storeDataVo : stores) {
            List<StoreAttrDto> storeAttrs = new ArrayList<StoreAttrDto>();
            for (StoreAttrDto tmpStoreAttrDto : storeAttrDtos) {

                if (storeDataVo.getStoreId().equals(tmpStoreAttrDto.getStoreId())) {
                    storeAttrs.add(tmpStoreAttrDto);
                }

                if (!storeDataVo.getStoreId().equals(tmpStoreAttrDto.getStoreId())) {
                    continue;
                }
                if ("100201903001".equals(tmpStoreAttrDto.getSpecCd())) {
                    storeDataVo.setArtificialPerson(tmpStoreAttrDto.getValue());
                } else if ("100201903003".equals(tmpStoreAttrDto.getSpecCd())) {
                    storeDataVo.setEstablishment(tmpStoreAttrDto.getValue());
                } else if ("100201903005".equals(tmpStoreAttrDto.getSpecCd())) {
                    storeDataVo.setBusinessScope(tmpStoreAttrDto.getValue());
                }
            }
            storeDataVo.setStoreAttrDtoList(storeAttrs);
        }
    }

    /**
     * 查询商户ID
     *
     * @param apiStoreDataVos
     * @return
     */
    private String[] getStoreIds(List<ApiStoreDataVo> apiStoreDataVos) {
        List<String> storeIds = new ArrayList<>();
        for (ApiStoreDataVo storeDataVo : apiStoreDataVos) {
            storeIds.add(storeDataVo.getStoreId());
        }

        return storeIds.toArray(new String[storeIds.size()]);
    }
}

@@ -607,16 +607,48 @@
 rtUtil;%0A
+import com.java110.vo.ResultVo;%0A
 import c
@@ -749,16 +749,108 @@
 towired;
+%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;
 %0A%0Aimport
@@ -1992,22 +1992,17 @@
     int 
-storeC
+c
 ount = s
@@ -2156,14 +2156,9 @@
 if (
-storeC
+c
 ount
@@ -2412,32 +2412,32 @@
  ArrayList%3C%3E();%0A
-
         %7D%0A    %7D%0A
@@ -2421,32 +2421,336 @@
 t%3C%3E();%0A        %7D
+%0A%0A        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger(%22row%22)), count, stores);%0A%0A        ResponseEntity%3CString%3E responseEntity = new ResponseEntity%3CString%3E(resultVo.toString(), HttpStatus.OK);%0A%0A        cmdDataFlowContext.setResponseEntity(responseEntity);
 %0A    %7D%0A%0A%0A    pri

优化 userId bug