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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.util.DataUtil;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.framework.web.BasePhpCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.IsBusinessEnum;
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 org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

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

/**
*
* 站点业务
*
* @author zxfei
* @date 2022-01-13
*/
@RestController
@RequestMapping("business")
public class BusinessController extends BaseCRUDJsonBodyMappingController<BusinessService,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 void doListBefore(BusinessEntity query, Map<String, Object> model, Context context) throws AppException {
        query.setIsBusiness(IsBusinessEnum.一级业务.getValue());
        super.doListBefore(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 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();
    }




}