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

import com.alibaba.fastjson.JSONObject;
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.service.ICacheService;
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.keys.RedisCacheKeys;
import com.mortals.xhx.module.base.model.BaseAreaEntity;
import com.mortals.xhx.module.base.model.BaseAreaQuery;
import com.mortals.xhx.module.base.service.BaseAreaService;
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.Map;

/**
 * 区域
 *
 * @author zxfei
 * @date 2022-11-22
 */
@RestController
@RequestMapping("base/area")
public class BaseAreaController extends BaseCRUDJsonBodyMappingController<BaseAreaService, BaseAreaEntity, Long> {

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

    public BaseAreaController() {
        super.setModuleDesc("区域");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "haveSonArea", paramService.getParamBySecondOrganize("BaseArea", "haveSonArea"));
        this.addDict(model, "haveSonDept", paramService.getParamBySecondOrganize("BaseArea", "haveSonDept"));
        this.addDict(model, "haveGetDept", paramService.getParamBySecondOrganize("BaseArea", "haveGetDept"));
        this.addDict(model, "haveGetMatterList", paramService.getParamBySecondOrganize("BaseArea", "haveGetMatterList"));
        this.addDict(model, "areaLevel", paramService.getParamBySecondOrganize("BaseArea", "areaLevel"));
        this.addDict(model, "status", paramService.getParamBySecondOrganize("BaseArea", "status"));
        super.init(model, context);
    }

    /**
     * 同步站点事项数据
     */
    @PostMapping(value = "genSubAreaByAreaName")
    @UnAuth
    public String genSubAreaByAreaName(@RequestBody BaseAreaQuery areaQuery) {
        JSONObject jsonObject = new JSONObject();
        String busiDesc = "同步子区域数据" + this.getModuleDesc();
        try {
            if (ObjectUtils.isEmpty(areaQuery.getName()) && ObjectUtils.isEmpty(areaQuery.getAreaCode())) {
                throw new AppException("当前初始区域编码或名称为空！");
            }
            //编码优先
            BaseAreaEntity baseAreaEntity = null;
            if (!ObjectUtils.isEmpty(areaQuery.getAreaCode())) {
                baseAreaEntity = this.service.selectOne(new BaseAreaQuery().areaCode(areaQuery.getAreaCode()));
            } else if (!ObjectUtils.isEmpty(areaQuery.getName())) {
                baseAreaEntity = this.service.selectOne(new BaseAreaQuery().name(areaQuery.getName()));
            }

            if (ObjectUtils.isEmpty(baseAreaEntity)) {
                throw new AppException(String.format("区域数据未找到，区域名称:%s,区域编码:%s", areaQuery.getName(),areaQuery.getAreaCode()));
            }
            boolean bool = cacheService.setnx(RedisCacheKeys.getSyncAreaLockKey() + areaQuery.getName(), areaQuery.getName(), 60L);
            if (!bool) {
                throw new AppException("当前正在同步区域数据中，请勿重复提交！");
            }

            Rest<String> rest = this.service.genSubAreaByRootName(baseAreaEntity, getContext());
            if(YesNoEnum.NO.getValue()==rest.getCode()){
                throw new AppException("更新子区域失败,"+rest.getMsg());
            }

           // ThreadPool.getInstance().execute(new SyncSubAreaThread(this.service, baseAreaEntity, getContext()));
            recordSysLog(request, busiDesc + " 【成功】");
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            jsonObject.put(KEY_RESULT_MSG, "初始同步区域数据命令下发成功！");
        } 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();
    }


}