package com.njcn.system.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.system.pojo.param.EventTemplateParam;
import com.njcn.system.service.IEventTreeService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@Slf4j
@Api(tags = "模板字典关联")
@RestController
@AllArgsConstructor
@RequestMapping("/EventTree")
public class EventTreeController extends BaseController {

    private final IEventTreeService iEventTreeService;

    /**
     * 根据模板id查询绑定的树节点
     * @param idList id集合
     */
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    @PostMapping("/selectEventTree")
    @ApiOperation("模板节点树")
    @ApiImplicitParam(name = "idList", value = "模板索引", required = true)
    public HttpResult<Object> selectEventTree(@RequestParam("idList") List<String> idList){
        String methodDescribe = getMethodDescribe("selectEventTree");
        LogUtil.njcnDebug(log, "{}，传入的模板id集合：{}", methodDescribe, idList);
        List<String> res = iEventTreeService.selectEventTree(idList);
        if (res.isEmpty()){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.NO_DATA, res, methodDescribe);
        }else{
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, methodDescribe);
        }
    }

    /**
     * 为模板分配字典节点
     * @param templateRelevancy 模板信息
     */
    @OperateInfo(operateType = OperateType.UPDATE)
    @PostMapping("/assignTemplate")
    @ApiOperation("模板分配节点")
    @ApiImplicitParam(name = "templateRelevancy", value = "模板信息", required = true)
    public HttpResult<Boolean> assignTemplate(@RequestBody @Validated EventTemplateParam.TemplateRelevancy templateRelevancy) {
        String methodDescribe = getMethodDescribe("assignTemplate");
        LogUtil.njcnDebug(log,  "{},传入的模板id：{},传入的集合为：{}", methodDescribe,templateRelevancy);
        boolean result = iEventTreeService.updateTemplate(templateRelevancy);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
