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

import com.mortals.framework.exception.AppException;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;

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

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

    @Autowired
    private ParamService paramService;

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

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, BusinessForm form, 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(request, response, form, model, context);
    }


    @Override
    protected int doListAfter(HttpServletRequest request, HttpServletResponse response, BusinessForm form, Map<String, Object> model, Context context) throws AppException {
        List<BusinessEntity> businessEntities = (List<BusinessEntity>) model.get(RESULT_KEY);
        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(request, response, form, model, context);
    }

    /**
     * 添加站点
     */
    @PostMapping(value = "addBusinessToSite")
    public String addBusinessToSite(@RequestParam(value = "businessIds") String businessIds,@RequestParam(value = "siteId") Long siteId) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> model = new HashMap<String, Object>();
        try {
            this.service.addBusinessToSite(businessIds,siteId,getContext());
            jsonObject.put(KEY_RESULT_DATA, model);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        } 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();
    }

}