package com.mortals.xhx.module.matter.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.service.ICacheService;
import com.mortals.framework.util.StringUtils;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.EventObjectType;
import com.mortals.xhx.module.matter.model.EventImplementationEntity;
import com.mortals.xhx.module.matter.service.EventImplementationService;
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.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
*
* 综窗事项列表
*
* @author zxfei
* @date 2024-06-20
*/
@RestController
@RequestMapping("event/implementation")
public class EventImplementationController extends BaseCRUDJsonBodyMappingController<EventImplementationService,EventImplementationEntity,Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private ICacheService cacheService;

    private ExecutorService executorService = Executors.newCachedThreadPool();

    private static String SYNC_TASK_KEY ="syncEventImplementation:run";

    public EventImplementationController(){
        super.setModuleDesc( "综窗事项列表");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "eventObjectType", EventObjectType.getEnumMap());
        super.init(model, context);
    }


    @PostMapping({"sync"})
    @UnAuth
    public Rest<Object> syncEventImplementation() {
        Rest<Object> ret = new Rest();
        Map<String, Object> model = new HashMap();
        String busiDesc = "同步综窗事项";
        int code = 1;
        try {
            String verify = cacheService.get(SYNC_TASK_KEY);
            if(StringUtils.isNotEmpty(verify)&&verify.equals("true")){
                throw new AppException("同步任务正在执行，请勿重复操作");
            }
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    cacheService.setnx(SYNC_TASK_KEY,"true",3600);
                    service.syncEventImplementation();
                }
            });
            cacheService.setnx(SYNC_TASK_KEY,"false",60);
            model.put("message_info","开始执行同步,请稍后查看");
        } catch (Exception var9) {
            code = -1;
            this.doException(this.request, busiDesc, model, var9);
        }
        ret.setCode(code);
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;
    }

    /***
     * 获取差集
     * @param query
     * @return
     */
    @PostMapping({"subList"})
    public Rest<Object> subList(@RequestBody EventImplementationEntity query) {
        Rest<Object> ret = new Rest();
        Map<String, Object> model = new HashMap();
        Context context = this.getContext();
        String busiDesc = "查询" + this.getModuleDesc();

        int code;
        try {
            this.doListBefore(query, model, context);
            PageInfo pageInfo = this.buildPageInfo(query);
            Result<EventImplementationEntity> result = this.getService().findSubList(query, pageInfo, context);
            this.doListAfter(query, result.getList(), context);
            model.put("data", result.getList());
            model.put("pageInfo", result.getPageInfo());
            this.parsePageInfo(model, result.getPageInfo());
            code = this.doListAfter(query, (Map)model, context);
            model.put("message_info", busiDesc + "成功");
            this.recordSysLog(this.request, busiDesc + " 【成功】");
        } catch (Exception var9) {
            code = -1;
            this.doException(this.request, busiDesc, model, var9);
        }

        this.init(model, context);
        ret.setCode(code);
        ret.setData(model);
        ret.setDict(model.get("dict"));
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;
    }

    /***
     * 配置可申请事项
     * @param entity
     * @return
     */
    @PostMapping({"addToApplyMatter"})
    public Rest<Object> addToApplyMatter(@RequestBody EventImplementationEntity entity) {
        Rest<Object> ret = new Rest();
        Map<String, Object> model = new HashMap();
        Context context = this.getContext();
        String busiDesc = "配置可申请事项";
        int code = 1;
        try {
            int result = this.getService().addToApplyMatter(entity,this.getContext());
            model.put("data", result);
            model.put("message_info", busiDesc + "成功");
            this.recordSysLog(this.request, busiDesc + " 【成功】");
        } catch (Exception var9) {
            code = -1;
            this.doException(this.request, busiDesc, model, var9);
        }
        this.init(model, context);
        ret.setCode(code);
        ret.setData(model);
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;
    }
}