package com.mortals.xhx.module.business.web;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.util.DataUtil;
import com.mortals.framework.web.BaseCRUDJsonPhpController;
import com.mortals.framework.web.BasePhpCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.module.business.model.BusinessEntity;
import com.mortals.xhx.module.business.model.BusinessQuery;
import com.mortals.xhx.module.business.service.BusinessService;
import com.mortals.xhx.module.business.model.BusinessTreeSelect;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.RESULT_KEY;

/**
*
* 站点业务
*
* @author zxfei
* @date 2022-01-13
*/
@RestController
@RequestMapping("business")
public class BusinessController extends BasePhpCRUDJsonMappingController<BusinessService,BusinessForm,BusinessEntity,Long> {

    @Autowired
    private ParamService paramService;

    public BusinessController(){
        super.setFormClass(BusinessForm.class);
        super.setModuleDesc( "站点业务");
    }

    @Override
    protected void init( Map<String, Object> model, Context context) {
        this.addDict(model, "isBusiness", paramService.getParamBySecondOrganize("Business","isBusiness"));
        this.addDict(model, "canorder", paramService.getParamBySecondOrganize("Business","canorder"));
        this.addDict(model, "cantake", paramService.getParamBySecondOrganize("Business","cantake"));
        this.addDict(model, "datashow", paramService.getParamBySecondOrganize("Business","datashow"));
        this.addDict(model, "status", paramService.getParamBySecondOrganize("Business","status"));
        this.addDict(model, "businessType", paramService.getParamBySecondOrganize("Business","businessType"));
        super.init(model, context);
    }




    @Override
    protected int doListAfter( BusinessEntity query, Map<String, Object> model, Context context) throws AppException {
        List<BusinessEntity> businessEntities = (List<BusinessEntity>) model.get(RESULT_KEY);
        if(!ObjectUtils.isEmpty(businessEntities)){
            businessEntities.stream().peek(item->{
                List<BusinessEntity> childs = this.service.find(new BusinessQuery().parentId(item.getId()));
                if(!ObjectUtils.isEmpty(childs)){
                    item.setChildren(childs);
                }else{
                    item.setChildren(null);
                }
            }).count();
            model.put(RESULT_KEY,businessEntities);
        }
        return super.doListAfter(query, model, context);
    }

    /**
     * 添加业务到站点
     */
    @PostMapping(value = "addBusinessToSite")
    public String addBusinessToSite(@RequestBody Map<String, Object> map) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> model = new HashMap<String, Object>();
        String businessIds=(String)map.get("businessIds");
        Long siteId= DataUtil.converObj2Long(map.get("siteId"));
        try {
            this.service.addBusinessToSite(businessIds,siteId,getContext());
            jsonObject.put(KEY_RESULT_DATA, model);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            jsonObject.put(KEY_RESULT_MSG, "添加业务到站点成功！");
        } catch (Exception e) {
            log.error("获取异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return jsonObject.toJSONString();
    }




}