/**
 * 文件：AreaController.java 版本：1.0.0 日期： Copyright &reg; All right reserved.
 */

package com.mortals.xhx.base.system.area.web;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.ap.SysConstains;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.code.YesNo;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.area.model.AreaEntity;
import com.mortals.xhx.base.system.area.service.AreaService;
import com.mortals.xhx.common.code.AreaLevelEnum;

/**
 * <p>
 * Title: 区域信息
 * </p>
 * <p>
 * Description: AreaController
 * </p>
 * <p>
 * Copyright: Copyright &reg;
 * </p>
 * <p>
 * Company:
 * </p>
 * @author
 * @version 1.0.0
 */
@RestController
@RequestMapping("area")
public class AreaController extends BaseCRUDJsonMappingController<AreaService, AreaForm, AreaEntity, Long> {

    public AreaController() {
        super.setFormClass(AreaForm.class);
        super.setModuleDesc("区域信息");
    }

    @Override
    protected void init(HttpServletRequest request, HttpServletResponse response, AreaForm form,
            Map<String, Object> model, Context context) {
        this.addDict(model, "level", IBaseEnum.getEnumMap(AreaLevelEnum.class));
        this.addDict(model, "status", IBaseEnum.getEnumMap(YesNo.class));
        super.init(request, response, form, model, context);
    }

    @Override
    protected void doListBefore(HttpServletRequest request, HttpServletResponse response, AreaForm form, Map<String, Object> model, Context context) throws AppException {
        if (form.getQuery().getStatus() == null) {
            form.getQuery().setStatus(YesNo.YES.getValue());
        }
        if(StringUtils.isEmpty(form.getQuery().getName())){
            form.getQuery().setLevel(AreaLevelEnum.PROVINCE.getValue());
        }
    }

    /**
     * 根据parentId查询区域信息
     */
    @PostMapping(value="getAreaByParentId")
    public String getAreaByParentId(HttpServletRequest request, HttpServletResponse response, AreaForm form) {
        JSONObject ret = new JSONObject();
        Context context = getContext();
        try{
            List<JSONObject> list = new ArrayList<>();
            PageInfo pageInfo = form.getPageInfo();
            Result<AreaEntity> result = service.find(form.getQuery(), form.getPageInfo(), context);
            pageInfo = result.getPageInfo();
            List<AreaEntity> areaList = result.getList();
            for(AreaEntity area : areaList){
                JSONObject json = new JSONObject();
                json.put("id", area.getId());
                json.put("parentId", area.getParentId());
                json.put("name", area.getName());
                json.put("level", area.getLevel());
                json.put("code", area.getCode());
                json.put("contactsPeople", area.getContactsPeople());
                json.put("contactsMobile", area.getContactsMobile());
                json.put("remark", area.getRemark());
                list.add(json);
            }

            JSONObject data = new JSONObject();
            data.put(SysConstains.RESULT_KEY, list);
            data.put(SysConstains.PAGEINFO_KEY, pageInfo);
            init(request, response, form, data, context);

            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA, data);
        }catch(Exception e){
            log.error("根据parentId查询区域信息错误", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, e.getMessage());
        }
        return ret.toJSONString();
    }

    @PostMapping(value = "getAllAreaList")
    public String getAllAreaList(HttpServletRequest request, HttpServletResponse response, AreaForm form) {
        try{
            return super.list(form);
        }catch(Exception e){
            log.error("查询所有区域信息列表错误",e);
            JSONObject ret = new JSONObject();
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }
}