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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
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.module.business.model.BusinessMatterEntity;
import com.mortals.xhx.module.business.model.BusinessMatterQuery;
import com.mortals.xhx.module.business.service.BusinessMatterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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.Map;

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

/**
 * 业务事项关联
 *
 * @author zxfei
 * @date 2022-01-12
 */
@RestController
@RequestMapping("business/matter")
public class BusinessMatterController extends BaseCRUDJsonBodyMappingController<BusinessMatterService, BusinessMatterEntity, Long> {

    @Autowired
    private ParamService paramService;

    public BusinessMatterController() {
        super.setFormClass(BusinessMatterForm.class);
        super.setModuleDesc("业务事项关联");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        super.init(model, context);
    }



    @PostMapping(value = "businesslist")
    public String getListByBusiness(@RequestBody BusinessMatterQuery query) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        Context context = this.getContext();
        String busiDesc = "查询" + this.getModuleDesc();
        int code;
        try {
            PageInfo pageInfo = new PageInfo();
            if (!ObjectUtils.isEmpty(query.getSize()) &&
                    !ObjectUtils.isEmpty(query)) {
                pageInfo.setCurrPage(query.getPage());
                pageInfo.setPrePageResult(query.getSize());
            }

            Result result = this.service.getListByBusiness(query, pageInfo, context);
            model.put(KEY_RESULT_DATA, result.getList());
            model.put(TOTAL, result.getPageInfo().getTotalResult());
            model.put(PER_PAGE, result.getPageInfo().getPrePageResult());
            model.put(CURRENT_PAGE, result.getPageInfo().getCurrPage());
            model.put(LAST_PAGE, result.getPageInfo().getTotalPage());
            code = this.doListAfter(query, model, context);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        this.init(model, context);
        ret.put(KEY_RESULT_CODE, code);
        ret.put(KEY_RESULT_MSG, model.remove(MESSAGE_INFO));
        ret.put(KEY_RESULT_DICT, model.remove(KEY_RESULT_DICT));
        ret.put(KEY_RESULT_DATA, model);
        return ret.toJSONString();
    }


    @PostMapping(value = "matterlist")
    public String getListByMatter(@RequestBody BusinessMatterQuery query) {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        Context context = this.getContext();
        String busiDesc = "查询" + this.getModuleDesc();
        int code;
        try {
            PageInfo pageInfo = new PageInfo();
            if (!ObjectUtils.isEmpty(query.getSize()) &&
                    !ObjectUtils.isEmpty(query)) {
                pageInfo.setCurrPage(query.getPage());
                pageInfo.setPrePageResult(query.getSize());
            }

            Result result = this.service.getListByMatter(query, pageInfo, context);
            model.put(KEY_RESULT_DATA, result.getList());
            model.put(TOTAL, result.getPageInfo().getTotalResult());
            model.put(PER_PAGE, result.getPageInfo().getPrePageResult());
            model.put(CURRENT_PAGE, result.getPageInfo().getCurrPage());
            model.put(LAST_PAGE, result.getPageInfo().getTotalPage());
            code = this.doListAfter(query, model, context);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        this.init(model, context);
        ret.put(KEY_RESULT_CODE, code);
        ret.put(KEY_RESULT_MSG, model.remove(MESSAGE_INFO));
        ret.put(KEY_RESULT_DICT, model.remove(KEY_RESULT_DICT));
        ret.put(KEY_RESULT_DATA, model);
        return ret.toJSONString();
    }



}