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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BasePhpCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.model.model.ModelQuery;
import com.mortals.xhx.module.model.service.ModelService;
import com.mortals.xhx.module.site.model.SiteEntity;
import com.mortals.xhx.module.site.model.SiteTreeSelect;
import com.mortals.xhx.module.site.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * 站点
 *
 * @author zxfei
 * @date 2022-01-20
 */
@RestController
@RequestMapping("site")
public class SiteController extends BasePhpCRUDJsonMappingController<SiteService, SiteForm, SiteEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private ModelService modelService;

    public SiteController() {
        super.setFormClass(SiteForm.class);
        super.setModuleDesc("站点");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "haveSonArea", paramService.getParamBySecondOrganize("Site", "haveSonArea"));
        this.addDict(model, "workday1", paramService.getParamBySecondOrganize("Site", "workday1"));
        this.addDict(model, "workday2", paramService.getParamBySecondOrganize("Site", "workday2"));
        this.addDict(model, "workday3", paramService.getParamBySecondOrganize("Site", "workday3"));
        this.addDict(model, "workday4", paramService.getParamBySecondOrganize("Site", "workday4"));
        this.addDict(model, "workday5", paramService.getParamBySecondOrganize("Site", "workday5"));
        this.addDict(model, "workday6", paramService.getParamBySecondOrganize("Site", "workday6"));
        this.addDict(model, "workday7", paramService.getParamBySecondOrganize("Site", "workday7"));
        this.addDict(model, "level", paramService.getParamBySecondOrganize("Site", "level"));
        this.addDict(model, "building", paramService.getParamBySecondOrganize("Site", "building"));
        this.addDict(model, "isSite", paramService.getParamBySecondOrganize("Site", "isSite"));
        this.addDict(model, "status", paramService.getParamBySecondOrganize("Site", "status"));
        this.addDict(model, "modelIds", modelService.find(new ModelQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getModelName())));
        super.init(model, context);
    }


    /**
     * 构建站点树
     */
    @GetMapping(value = "siteTree")
    public String siteTree() {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = this.getModuleDesc() + "构建站点树";
        try {
            List<SiteTreeSelect> siteTree = this.service.siteTree(getContext());
            model.put("siteTree", siteTree);
            this.init(model, getContext());
            recordSysLog(request, busiDesc + " 【成功】");
            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();
    }


}