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

import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
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.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.sheet.model.SheetMatterEntity;
import com.mortals.xhx.module.sheet.service.SheetMatterService;
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.MESSAGE_INFO;
import static com.mortals.framework.ap.SysConstains.PAGEINFO_KEY;

/**
*
* 基础事项
*
* @author zxfei
* @date 2022-09-27
*/
@RestController
@RequestMapping("sheet/matter")
public class SheetMatterController extends BaseCRUDJsonBodyMappingController<SheetMatterService,SheetMatterEntity,Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private ISiteFeign siteFeign;

    public SheetMatterController(){
        super.setModuleDesc( "基础事项");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "appoveObjectShow", paramService.getParamBySecondOrganize("SheetMatter","appoveObjectShow"));
        this.addDict(model, "operatScopeShow", paramService.getParamBySecondOrganize("SheetMatter","operatScopeShow"));
        this.addDict(model, "appoveTimeLimitShow", paramService.getParamBySecondOrganize("SheetMatter","appoveTimeLimitShow"));
        this.addDict(model, "handleType", paramService.getParamBySecondOrganize("SheetMatter","handleType"));
        this.addDict(model, "isChargesShow", paramService.getParamBySecondOrganize("SheetMatter","isChargesShow"));
        this.addDict(model, "certificationLevelsShow", paramService.getParamBySecondOrganize("SheetMatter","certificationLevelsShow"));
        this.addDict(model, "windowToTheSceneNum", paramService.getParamBySecondOrganize("SheetMatter","windowToTheSceneNum"));
        this.addDict(model, "isOnlineSubscribeShow", paramService.getParamBySecondOrganize("SheetMatter","isOnlineSubscribeShow"));
        this.addDict(model, "isExpressTakeShow", paramService.getParamBySecondOrganize("SheetMatter","isExpressTakeShow"));
        this.addDict(model, "isProvinceAcquisitionShow", paramService.getParamBySecondOrganize("SheetMatter","isProvinceAcquisitionShow"));
        this.addDict(model, "isApplyProvinceShow", paramService.getParamBySecondOrganize("SheetMatter","isApplyProvinceShow"));
        this.addDict(model, "onlineType", paramService.getParamBySecondOrganize("SheetMatter","onlineType"));
        this.addDict(model, "onlineToTheSceneNum", paramService.getParamBySecondOrganize("SheetMatter","onlineToTheSceneNum"));
        this.addDict(model, "onlineOperatDeep", paramService.getParamBySecondOrganize("SheetMatter","onlineOperatDeep"));
        this.addDict(model, "isExpressTakeOnlineShow", paramService.getParamBySecondOrganize("SheetMatter","isExpressTakeOnlineShow"));
        this.addDict(model, "isDoorTakeShow", paramService.getParamBySecondOrganize("SheetMatter","isDoorTakeShow"));
        this.addDict(model, "eventTypeShow", paramService.getParamBySecondOrganize("SheetMatter","eventTypeShow"));
        this.addDict(model, "performHierarchyShow", paramService.getParamBySecondOrganize("SheetMatter","performHierarchyShow"));
        this.addDict(model, "powerSourceShow", paramService.getParamBySecondOrganize("SheetMatter","powerSourceShow"));
        this.addDict(model, "performDeptTypeShow", paramService.getParamBySecondOrganize("SheetMatter","performDeptTypeShow"));
        this.addDict(model, "goveServiceCenterShow", paramService.getParamBySecondOrganize("SheetMatter","goveServiceCenterShow"));
        this.addDict(model, "isConvenientCenterShow", paramService.getParamBySecondOrganize("SheetMatter","isConvenientCenterShow"));
        this.addDict(model, "terminalHandle", paramService.getParamBySecondOrganize("SheetMatter","terminalHandle"));
        this.addDict(model, "isOnline", paramService.getParamBySecondOrganize("SheetMatter","isOnline"));
        this.addDict(model, "isOnlinePayShow", paramService.getParamBySecondOrganize("SheetMatter","isOnlinePayShow"));
        this.addDict(model, "entrustmentDepartmen", paramService.getParamBySecondOrganize("SheetMatter","entrustmentDepartmen"));
        this.addDict(model, "matterStatus", paramService.getParamBySecondOrganize("SheetMatter","matterStatus"));
        this.addDict(model, "type", paramService.getParamBySecondOrganize("SheetMatter","type"));
        this.addDict(model, "mattertype", paramService.getParamBySecondOrganize("SheetMatter","mattertype"));
        this.addDict(model, "parson", paramService.getParamBySecondOrganize("SheetMatter","parson"));
        this.addDict(model, "lengal", paramService.getParamBySecondOrganize("SheetMatter","lengal"));
        this.addDict(model, "isRecommend", paramService.getParamBySecondOrganize("SheetMatter","isRecommend"));
        super.init(model, context);
    }


    @Override
    protected void doListBefore(SheetMatterEntity query, Map<String, Object> model, Context context) throws AppException {
        if(!ObjectUtils.isEmpty(query.getMatterName())){
            query.setMatterName("%"+query.getMatterName()+"%");
        }
        Rest<SitePdu> rest = siteFeign.info(query.getSiteId());
        if(rest.getCode()== YesNoEnum.YES.getValue()){
            query.setSiteId(null);
            query.setAreaCode(rest.getData().getAreaCode());
        }
        super.doListBefore(query, model, context);
    }


    @PostMapping(value = "sublist")
    @UnAuth
    public Rest<Object> sublist(@RequestBody SheetMatterEntity query) {
        Rest<Object> ret = new Rest<>();
        Map<String, Object> model = new HashMap<>();
        Context context = this.getContext();
        String busiDesc = "查询差集" + this.getModuleDesc();
        int code=VALUE_RESULT_SUCCESS;
        try {
            PageInfo pageInfo = buildPageInfo(query);
            Result<SheetMatterEntity> result = this.getService().findSubList(query, pageInfo, context);
            model.put(KEY_RESULT_DATA, result.getList());
            model.put(PAGEINFO_KEY, result.getPageInfo());
            parsePageInfo(model, result.getPageInfo());
            model.put(MESSAGE_INFO, busiDesc + "成功");
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        this.init(model, context);
        ret.setCode(code);
        ret.setData(model);
        ret.setMsg(model.get(MESSAGE_INFO) == null ? "" : model.remove(MESSAGE_INFO).toString());
        return ret;
    }
}